user218404
user218404

Reputation:

How do I animate a sprite using cocos2d?

I have a sprite which will move right,left and jump. I need to add the action to a animated sprite ie an animated sprite should jump, turn right and left. Can anyone please tell me how to do it with sample code.

Upvotes: 0

Views: 3081

Answers (1)

Oscar Gomez
Oscar Gomez

Reputation: 18488

This is quite simple with cocos2d code here:

Sprite *mySprite = [Sprite spriteWithFile@"mySprite.png"];
[mySprite setPosition:ccp(x,y)];
[self addChild:mySprite]; //This displays the Sprite in your layer

Now for the sequence you intend to do...

id moveRight = [MoveBy actionWithDuration:2 position ccp(x+k,y) //Where k is how much to the right you want it to go.
id moveLeft = [MoveBy actionWithDuration:2 position ccp(x-k,y)];
id jump = [JumpBy actionWithDuration:1 position:ccp (x,y) height:1 jumps:1];
id sequence = [Sequence actions:moveRight,moveLeft,jump,nil];
[mySprite runAction:sequence];

Hope that's clear.

-Oscar

Upvotes: 5

Related Questions