iphoneioscocos2d-iphoneccrotateby

Cocos2d Rocking Animation with CCRotateBy?


I'm new to Cocos2d and am after some help.

I have a sprite that I want to rotate 45 degrees to the left, center itself again and then rotate about 45 degree to the right. Sort of like a rocking motion.

I want this done when I touch the actual sprite, not just anywhere on the screen.

As it is right now, when I touch anywhere on the screen it rotates, 360 degrees (360 degrees for testing) but every time I tap the screen I get a new sprite.

My question is one, how do I set it up so when I touch the sprite it will perform the rocking motion that I am after? Without having a new sprite pop up each and every time I tap the screen.


Solution

  • You can check which sprite the UITouch is on (by checking whether the point of touch is in each sprite's rect) and start the animation in ccTouchesBegan or ccTouchesEnded according to your requirements. You can have a look at the "TouchesTest" example included with the cocos2d.

    You may implement the animation like this:

    CCAction *action = [CCSequence actions:
                        [CCRotateBy actionWithDuration:0.25 angle:-45],
                        [CCRotateBy actionWithDuration:0.5 angle:90],
                        [CCRotateBy actionWithDuration:0.25 angle:-45],
                        nil];
    [theSprite runAction:action];