Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2D help: How to rotate a sprite continuously and generate child sprites in the rotating sprite

I am new to cocos2D can anyone suggest an easy solution to the problem?

I have a windmill with 8 bars on the windmill with each bar separated by 45 degrees, where half of the windmill is on screen and half is outside the screen. I want to rotate the windmill and make it rotate forever. Also I want to attach a child at the end of the windmill bars and it will rotate along with the windmill. An easy solution would be appreciated.

EDIT

I was able to rotate the windmill with, I wrote this piece of code:

windmill = [CCSprite spriteWithFile:@"Chorki.png"];
windmill.position = CGPointMake(winSize.width*0.02f, winSize.height*0.56f);
windmill.scale = 0.55f;
[self addChild:windmill z:0];
CCRotateBy *rot = [CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:5 angle: 360]];
[windmill runAction:rot];

Now please help me on how to add child sprite at the end of each windmill bars, there are 8 bars in total and each bar is separated by 45 degrees.

like image 420
Shahnawaz Avatar asked Dec 27 '22 05:12

Shahnawaz


2 Answers

To make a CCSprite to rotate forever you could use something like

[windmill runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:5.0 angle:360]]];

then add any CCSprite as a child it will rotate too. The position that you assign to a child is based on his parent, take that in account.

EDIT 1

Well, if you know the radius of the circle you can make some maths and get the position that you want. But also, if you don't need to calculate the positions, just try & error to get what you want. Try with the windmill without rotation.

like image 166
Sebastián Castro Avatar answered Dec 29 '22 18:12

Sebastián Castro


You declare your action, "rot" as an instance of CCRotateBy yet when you allocate it, you begein with [CCRepeatForever[...]]; If you wanted you could do

CCRotateBy *rot = [CCRotateBy actionWithDuration:5 angle:360];

[windmill runAction:[CCRepeatForever actionWithAction:rot]];

like image 39
oneiric Avatar answered Dec 29 '22 20:12

oneiric