0

I am doing a small game, in this game i have two menuitem buttons one is for Jumping and one is for rolling, i implemented jumping functionality using ccjump but am unable to implement rolling functionality, for this i used ccrotate function it works partially, can anyone help me regarding this issue... Thanks in advance

Here is my code for rolling

id action= [CCRotateBy actionWithDuration: 0.2 angle:-130];
[mysprite runAction:action];

Can anyone give me exact solution????????

6
  • what do you mean 'partially' ? Can you confirm that the above code actually gets executed when you select the menu button for rolling ?
    – YvesLeBorg
    Dec 27, 2012 at 4:15
  • Thanks YvesLeBorg, partially in the sense when i hit the rolling menu button the sprite gets rotated by -130 degrees and come back to original position, instead of this rotation i want to roll that sprite
    – mahesh
    Dec 27, 2012 at 4:25
  • then, we are probably not looking at the right lines of code. This should work every time. You must be changing the rotation of the sprite somewhere else in the code, after this action completes.
    – YvesLeBorg
    Dec 27, 2012 at 4:38
  • or else is it possible to replace sprite with another sprite for some fraction of seconds and then get back to original sprite after that action completes???? if possible how can i do this??
    – mahesh
    Dec 27, 2012 at 4:44
  • 1
    Are you talking about a rotation and a movement at the same time? Should be easy to do, just combine a rotate and move action with a CCSpawn so they start at the same time.
    – Sylvan
    Dec 27, 2012 at 8:48

1 Answer 1

1

Ok to simulate the effect of a sprite "rolling" you have to combine a rotation and a translation. In order to be convincing, you have to get the right amount of translation so that the body doesn't appear to be rotating out of sync with the translation, like either spinning too fast or too slow for the amount moved. You can determine this distance moved either by trial and error, or you can calculate it fairly precisely if the sprite is nearly circular.

For a perfect circle (the simplest case), as it rolls along the ground, the distance rolled is equal to the radius multiplied by the radians rotated. So for a full rotation, the horizontal distance moved would be 2*pi*r. For a partial rotation, in your case 130 degrees, the horizontal distance traveled would be -130 * (pi/180) * sprite.contentSize.width/2 for a circular sprite. The pi/180 is to convert from degrees to radians. For a non-perfect circle, this formula will get you close but you might have to tweak it to get the look right. So to recap, along with some sample code, your approach might go something like this:

float radius = mySprite.contentSize.width/2;
float radians = CC_DEGREES_TO_RADIANS(-130);
float distance = radians * radius;

CCRotateBy *rotateAction = [CCRotateBy actionWithDuration:0.2 angle: -130];
CCMoveBy *moveAction = [CCMoveBy actionWithDuration:0.2 position:ccp(distance, 0)];
CCSpawn *spawnAction = [CCSpawn actionOne:rotateAction two:moveAction];

[mySprite runAction:spawnAction];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.