Ios cocos2d中精灵的触摸动作
我想在我的精灵被触摸时做一个动作,这是动作方法:Ios cocos2d中精灵的触摸动作,ios,ipad,cocos2d-iphone,css-sprites,Ios,Ipad,Cocos2d Iphone,Css Sprites,我想在我的精灵被触摸时做一个动作,这是动作方法: -(void) spriteEffect { CCSprite *actionEffect = avatar; id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:1]; id sequence = [CCSequence actions: jump, nil]; [actionEffect runAction:s
-(void) spriteEffect
{
CCSprite *actionEffect = avatar;
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:1];
id sequence = [CCSequence actions: jump, nil];
[actionEffect runAction:sequence];
}
现在,我的问题是,我不知道如何使触摸动作与精灵相连接;我应该用这个吗
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
Msp位于指定了.plist的.png图像中
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1)
{
// Add Your Action
}
}
或
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SpriteThouch)];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gr];
和调用方法spriteTouch
-(void)SpriteThouch
{
// code here
}
我使用CCNode+SFgestureRecognitors.h(https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers)向我的精灵添加手势 因此,您需要做的就是:
- 导入CCNode+SFgestureRecognitors.h
- 添加精灵:CCSprite*按钮=[CCSprite sprite WithCGImage:
- 为sprite添加代码 button.isTouchEnabled=是 UITapGestureRecognizer*点击=[[UITapGestureRecognizer alloc]initWithTarget:目标操作:选择器] [按钮添加手势识别器:点击]
itemFromNormalImage:selectedImage:target:selector:
正如您所看到的,您可以传递菜单项的图像(即精灵图像),传递目标(通常是执行操作的self),当然还有方法(选择器),当触摸菜单项时将调用该方法
如果你需要更多的帮助,请告诉我……我选择精灵来玩游戏,我只想在他们被触摸时添加一个动作,让他们更漂亮,但当他们被选中时不会,看看这张图片,你会意识到,想象你选择了Luigi,Luigi出现在下面,我想添加一个动作,让Luigi在触摸时跳起来我使用“UITapGestureRecognitzer”它告诉我CCSprite可能不会响应“AddGestureRecognitzer”。首先创建CCSprite的对象,然后初始化,然后在添加之后..可能可以解决您的问题:)谢谢:)很高兴听到好的建议;但是我已经初始化了CCSprite,我称之为avatar,方法中的一个。:/我应该在哪里使用/pu在initWithMenuScene中的UITapGestureRecognitizer?[[CCSprite SharedDirectory]openGLView]AddGestureRecognitizer:gr];问题是我正在使用一个.png和一个.plist,其中所有的精灵都在那里,所以我不能对每个图像都使用一个.png;我必须一次移动所有的精灵…你说所有的精灵都在那里是什么意思?我有一个.png,其中所有的精灵都在那里,类似这样的东西:ohhhh…是的,现在我明白了。嗯…你可以阅读每一个精灵并为每个人创建一个菜单项。我认为菜单是解决你问题的方法,因为你只需要按下一些按钮就可以了?选择精灵来玩游戏,我只想在他们被触摸时添加一个动作,只是为了让他们更漂亮,但当他们被选中时,你不会发现,看看这张图片,你会意识到你选择了Luigi,Luigi appe下面是ars,我想添加一个动作,让luigi跳起来——