经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Cocos2d-x 学习笔记(11.3) JumpBy JumpTo
来源:cnblogs  作者:深潮  时间:2019/7/29 8:51:32  对本文有异议

1. JumpBy JumpTo

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

1.1 成员变量 create方法

JumpBy:

  1. Vec2 _startPosition; // startWithTarget getPosition设置
  2. Vec2 _delta; // create 参数position设置
  3. float _height; // create参数
  4. int _jumps; // create参数
  5. Vec2 _previousPos; // startWithTarget getPosition设置

JumpTo:

  1. Vec2 _startPosition; // startWithTarget getPosition设置
  2. Vec2 _delta; // _endPosition减_startPosition
  3. float _height; // create参数
  4. int _jumps; // create参数
  5. 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:

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

JumpTo:

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

1.3 update

JumpBy:

首先计算float frac:

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

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

下一步计算float y:

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

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

接下来:

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

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

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

接下来就简单了:

  1. Vec2 currentPos = _target->getPosition();
  2. Vec2 diff = currentPos - _previousPos;
  3. _startPosition = diff + _startPosition;
  4. Vec2 newPos = _startPosition + Vec2(x,y);
  5. _target->setPosition(newPos);
  6. _previousPos = newPos;

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

JumpTo:

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

原文链接:http://www.cnblogs.com/deepcho/p/cocos2dx-action-jumpby-jumpto.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号