问题
These are my first Cocos2D projects, I'm trying to make a sprite jump in the same place when touched, but I can't make it response because I don't know how to set touch actions on sprites. Here is the code :
-(void) spriteEffect
{
CCSprite *actionEffect = avatar;
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:2];
id sequence = [CCSequence actions: jump, nil];
[actionEffect runAction:sequence];
return yes
}
Should I use a
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
Thanks!
回答1:
Your comment that avatar
is an array of sprites helps clarifying why you don't see any effects. Try to do something like:
-(void) spriteEffect
{
CCSprite *actionEffect = <get a sprite from avatar array>;
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:2];
[actionEffect runAction:jump];
}
I don't know what kind of array avatar
is, so I can't provide syntax for accessing its elements. If avatar
is an NSArray
, you can make all of your sprites jump by using:
-(void) spriteEffect
{
foreach (CCSprite* actionEffect in avatar) {
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:2];
[actionEffect runAction:jump];
}
}
来源:https://stackoverflow.com/questions/14462623/ccjumpby-on-a-sprite