CCJumpBy on a sprite

旧街凉风 提交于 2019-12-12 03:30:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!