• 动作(Action)
    • By 和 To 的区别

    动作(Action)

    动作(Action) 的功能就和字面含义一样,它通过改变一个 Node 对象的属性,让它表现出某种动作。动作对象能实时的改变 Node 的属性,任何一个对象只要它是 Node 的子类都能被改变。比如,你能通过动作对象把一个精灵从一个位置移动到另一个位置。

    通过 MoveToMoveBy 方法:

    C++

    1. // Move sprite to position 50,10 in 2 seconds.
    2. auto moveTo = MoveTo::create(2, Vec2(50, 10));
    3. mySprite1->runAction(moveTo);
    4. // Move sprite 20 points to right in 2 seconds
    5. auto moveBy = MoveBy::create(2, Vec2(20,0));
    6. mySprite2->runAction(moveBy);

    By 和 To 的区别

    你能注意到,每一个动作都会有两个方法 ByTo。两种方法方便你在不同的情况使用,By 算的是相对于节点对象的当前位置,To 算的是绝对位置,不考虑当前节点对象在哪。如果你想动作的表现是相对于 Node 当前位置的,就用 By,相对的想让动作的表现是按照坐标的绝对位置就用 To。看一个例子:

    C++

    1. auto mySprite = Sprite::create("mysprite.png");
    2. mySprite->setPosition(Vec2(200, 256));
    3. // MoveBy - lets move the sprite by 500 on the x axis over 2 seconds
    4. // MoveBy is relative - since x = 200 + 200 move = x is now 400 after the move
    5. auto moveBy = MoveBy::create(2, Vec2(500, mySprite->getPositionY()));
    6. // MoveTo - lets move the new sprite to 300 x 256 over 2 seconds
    7. // MoveTo is absolute - The sprite gets moved to 300 x 256 regardless of
    8. // where it is located now.
    9. auto moveTo = MoveTo::create(2, Vec2(300, mySprite->getPositionY()));
    10. // Delay - create a small delay
    11. auto delay = DelayTime::create(1);
    12. auto seq = Sequence::create(moveBy, delay, moveTo, nullptr);
    13. mySprite->runAction(seq);

     动作(Action)  - 图1

    原文: http://docs.cocos.com/cocos2d-x/manual/zh/actions/