0
votes

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
could you show more code? is the sprite part of a scene? show how you create it and add to a scene. - sergio
well, what is avatar? where do you create it and add it to your scene? - sergio
avatar is an array of sprites,here: -(BOOL)isAvatarSelected:(int)idToSelect -(void)selectAvatar:(CCMenuItemImage*)sender - Vergmort
runAction can be executed on a single sprite, not on a array of sprites... this explain why the action is not working. - sergio
Great job! is there a way to do this in all my sprites? - Vergmort

1 Answers

0
votes

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];
   }

}