Search code examples
c++cocos2d-xcocos2d-x-3.0

Cocos2d-x 3.0 sequence of rotation with different anchor point


I'm trying to make a sequence which has 3 RotateBy actions.

1st RotateBy action with ANCHOR_MIDDLE_TOP

2nd RotateBy action with ANCHOR_MIDDLE

3rd RotateBy action with ANCHOR_MIDDLE_BOTTOM

But, I don't know how to run this sequence in the following order

  1. mySprite->setAnchorPoint(Point::ANCHOR_MIDDLE_TOP);
  2. rotate mySprite 90 degree
  3. mySprite->setAnchorPoint(Point::ANCHOR_MIDDLE);
  4. rotate mySprite 90 degree
  5. mySprite->setAnchorPoint(Point::ANCHOR_MIDDLE_BOTTOM);
  6. rotate mySprite 90 degree

And Sequence::create takes only actions.


Solution

  • You can use CallFunc as an action, and call a function that changes the anchor point of your sprite. Something like this:

    cocos2d::Sequence::create(cocos2d::RotateBy::create(1.0f, 90.0f),  
        cocos2d::CallFunc::create(MySprite::changeAnchorPoint),
        cocos2d::RotateBy::create(1.0f, 90.0f),  
        cocos2d::CallFunc::create(MySprite::changeAnchorPoint),
        cocos2d::RotateBy::create(1.0f, 90.0f),  
        cocos2d::CallFunc::create(MySprite::changeAnchorPoint));
    

    Edit: to send a parameter you should be able to write something like this:

    cocos2d::CallFunc::create([mySprite]() { 
        mySprite->setAnchorPoint(Point::ANCHOR_MIDDLE_TOP); 
    });
    

    Note: CallFunc wants a function with no parameters, but you can get around that by capturing your object you'd like to use in the function using the c++11 lambda syntax.