0

I have a sprite :

ombreoeuf1 = [CCSprite spriteWithFile:@"mangeurcentremieu3_03.png" ];
ombreoeuf1.position = ccp(240,160);
[self addChild:ombreoeuf1];

And I would like to rotate it constantly around an anchor point. How can I do it?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
greg rock
  • 239
  • 6
  • 14

2 Answers2

1

You can first set anchor point by setting the property anchorPoint, for example:

[ombreoeuf1 setAnchorPoint:ccp(0,0)]

and then set rotation (in degrees) by setting another property rotation:

[ombreoeuf1 setRotation:90]

anchorPoint and rotation are both properties of CCNode class, which is the parent of CCSprite.

Update

According to your comments, it seems that what you want is a rotating sprite which never stops? Here is an example which let the sprite rotate 10 degrees per 0.1 seconds:

[sprite runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:0.1 angle:10]]];
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • but this, is a simple rotation. I want a rotation animation that is infinite – greg rock Apr 20 '12 at 08:46
  • @gregrock I have updated my answer. Please check whether it's what you want? – Hailei Apr 20 '12 at 08:56
  • final question : I would like to know if with this method my sprite can detect touch event while it is rotating – greg rock Apr 20 '12 at 09:04
  • @gregrock Maybe these posts are helpful? (1) http://stackoverflow.com/questions/3273945/cocos2d-detect-touch-on-rotated-sprite (2) http://stackoverflow.com/questions/7634330/how-to-detect-touch-on-rotated-sprite-in-cocos2d – Hailei Apr 20 '12 at 09:21
0

All transformations of CCNode subclasses are done relatively to the anchor point. During all of your transformations the anchorPoint will have the same position. For example, if you will place sprite with anchorPoint (0.f, 0.f) to the position (0.f, 0.f), the left-bottom corner of the screen, then set it's scale, for example, to 5.f, after transforming it will stay at the left-bottom corner, just wil become larger. So all rotations automatically will be done relatively to the anchor point.

Just one more thing. CCSprite has anchorPoint (0.5f, 0.5f) by default and some content size, so you just have to set it to another to see changes in transformations. If you want to do it with CCNode, you have to set it's relativeToAnchorPoint property to YES and set contentSize manually.

You can use CCRepeatForever action for this. For example,

id rotateAction = [CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration: yourDuration 
                                                                             angle: anyAngleForGivenTime]];
Morion
  • 10,495
  • 1
  • 24
  • 33