0
\$\begingroup\$

In cocos2d-x, I move a sprite with ccbezierTo like this:

// use for ccbezierTo
bezier.controlPoint_1 = ccp(m_fish->getPositionX() + 200, visibleSize.height/2 + 300);
bezier.controlPoint_2 = ccp(m_fish->getPositionX() + 400, visibleSize.height/2 - 300);
bezier.endPosition = ccp(m_fish->getPositionX() + 600,visibleSize.height/2);

bezier1.controlPoint_1 = ccp(m_fish->getPositionX() + 800, visibleSize.height/2 + 300);
bezier1.controlPoint_2 = ccp(m_fish->getPositionX() + 1000, visibleSize.height/2 - 300);
bezier1.endPosition = ccp(m_fish->getPositionX() + 1200,visibleSize.height/2);


bezierForward = CCBezierTo::create(6, bezier);
nextBezier = CCBezierTo::create (6,bezier1);

m_fish->runAction(CCSequence::create( bezierForward, nextBezier, NULL));

How can I make my sprite rotate while moving it with CCBezierTo?

\$\endgroup\$
5
  • \$\begingroup\$ This could help gamedev.stackexchange.com/questions/20527/… or stackoverflow.com/questions/9990443/… \$\endgroup\$ – Heckel Jun 4 '14 at 12:32
  • \$\begingroup\$ Thanks i solved, i use this code: CGPoint oldpos = [[self target] position]; [super update:time]; CGPoint newpos = [[self target] position]; float angle = atan2(-newpos.y + oldpos.y, newpos.x - oldpos.x) * 180 / M_PI; angle += 90; // my sprites start pointing up [[self target] setRotation:angle]; \$\endgroup\$ – user1609578 Jun 5 '14 at 3:03
  • \$\begingroup\$ Can you write it as an answer and mark it "accepted" please? It will be easier for people having the same problem to find an answer \$\endgroup\$ – Heckel Jun 5 '14 at 4:37
  • \$\begingroup\$ Sure, i use this link gamedev.stackexchange.com/questions/20527/… \$\endgroup\$ – user1609578 Jun 5 '14 at 8:23
  • \$\begingroup\$ I mean, can you write an answer explaining how you solved it ? Not only a comment. You would then be able to mark your answer as "accepted" so people would know it worked for you. \$\endgroup\$ – Heckel Jun 5 '14 at 9:42
0
\$\begingroup\$

i found a solution on this site. Use this very useful code:

 CGPoint oldpos = [[self target] position];
    [super update:time];
    CGPoint newpos = [[self target] position];
    float angle = atan2(-newpos.y + oldpos.y, newpos.x - oldpos.x) * 180 / M_PI;
    angle += 90; // my sprites start pointing up
    [[self target] setRotation:angle];

and sprite can rotate when move with ccbezierTo.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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