zoukankan      html  css  js  c++  java
  • Cocos2d-x 学习笔记(11.3) JumpBy JumpTo

    1. JumpBy JumpTo

    JumpBy,边跳边平移,不只做垂直向上的抛物动作,同时还在向终点平移。JumpTo是JumpBy的子类。

    1.1 成员变量 create方法

    JumpBy:

        Vec2           _startPosition; // startWithTarget getPosition设置
        Vec2           _delta; // create 参数position设置
        float           _height; // create参数
        int             _jumps; // create参数
        Vec2           _previousPos; // startWithTarget getPosition设置

    JumpTo:

        Vec2           _startPosition; // startWithTarget getPosition设置
        Vec2           _delta; // _endPosition减_startPosition
        float           _height; // create参数
        int             _jumps; // create参数
        Vec2           _previousPos; // startWithTarget getPosition设置
        Vec2 _endPosition; // create 用参数position设置

    JumpBy::create(float duration, const Vec2& position, float height, int jumps)

    JumpTo::create(float duration, const Vec2& position, float height, int jumps)

    1.2 startWithTarget

    JumpBy:

    ActionInterval::startWithTarget(target);
    _previousPos = _startPosition = target->getPosition();

    JumpTo:

    JumpBy::startWithTarget(target);
    _delta.set(_endPosition.x - _startPosition.x, _endPosition.y - _startPosition.y);

    1.3 update

    JumpBy:

    首先计算float frac:

    float frac = fmodf( t * _jumps, 1.0f );

    t * _jumps是目前总跳跃次数完成的进度,与1求余的值是正在进行的这次跳跃完成的进度。

    下一步计算float y:

    float y = _height * 4 * frac * (1 - frac);

    y = _height * 4 * frac * (1 - frac)是以frac为X,y为Y建立的二次函数方程。frac为1时,一次跳跃动作完成。frac为0.5时,跳跃动作运动到最高点。

    接下来:

    y += _delta.y * t; // _delta是create设置的终点坐标
    float x = _delta.x * t;

    将当前进度的平移y的坐标加当前一次跳跃进度的y坐标,作为本次y终值。

    x只要考虑X方向平移的进度,跳跃与X方向无关。

    接下来就简单了:

    Vec2 currentPos = _target->getPosition();
    Vec2 diff = currentPos - _previousPos;
     _startPosition = diff + _startPosition;
    Vec2 newPos = _startPosition + Vec2(x,y);
    _target->setPosition(newPos);
    _previousPos = newPos;

    和Move的差不多,只是newPos在之前已经计算好了。这里也用到了宏定义判断CC_ENABLE_STACKABLE_ACTIONS。

    JumpTo:

    使用父类JumpBy的update方法,不同之处只是To的_delta在startWithTarget中计算的positioin,By直接使用了我们设置的positioin。

  • 相关阅读:
    Lazarus教程 中文版后续给出
    QBASIC教程
    Object Pascal中文手册 经典教程
    Pascal 基础教程
    Delphi中的关键字与保留字
    Pascal数据结构与算法
    Pascal小游戏 贪吃蛇
    Pascal小游戏 俄罗斯方块怀旧版
    Pascal ASCII和文本的转换
    IDEA安装问题解决
  • 原文地址:https://www.cnblogs.com/deepcho/p/cocos2dx-action-jumpby-jumpto.html
Copyright © 2011-2022 走看看