rotate a sprite around an anchor point

混江龙づ霸主 提交于 2019-12-25 03:34:27

问题


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?


回答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]]];



回答2:


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


来源:https://stackoverflow.com/questions/10234746/rotate-a-sprite-around-an-anchor-point

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