Cocos Creator之自定义Action
自定义Action
学习自定义Action的最好方法是去查看Cocos Creator中常用动作的写法。比如cc.MoveTo
继承了cc.MoveBy
,而cc.MoveBy
则进一步继承了cc.ActionInterval
。
可以从cocos-creator的引擎源码找到cc.MoveBy的相关定义:
cc.MoveBy = cc.Class({
name: 'cc.MoveBy',
extends: cc.ActionInterval,
ctor:function (duration, deltaPos, deltaY) {
this._positionDelta = cc.v2(0, 0);
this._startPosition = cc.v2(0, 0);
this._previousPosition = cc.v2(0, 0);
deltaPos !== undefined && cc.MoveBy.prototype.initWithDuration.call(this, duration, deltaPos, deltaY);
}
...
})
如果需要自定义Action只需要按照自己的需求仿照cc.MoveBy的定义写一个就行了。如实现cc.ActionInterval
类的initWithDuration
、startWithTarget
、update
方法。「sprite 圆形运动,圆周运动」给出了自定义一个圆周运动的三种方案,其中第一种方案就是集成cc.ActionInterval,自定义Action的方法。这里不再赘述。
特殊需求 - 用TypeScript自定义Action
因为自己做cocos creator时用的ts语言,在写法上与之前提到的略有不同。目的是实现精灵绕某一点旋转一定的角度。
需求
- 根据精灵自身旋转角度和给定半径确定精灵圆周运动的圆心。
- 在精灵圆周运动过程中自动调整精灵自身的旋转角度。
方案
这里给出实现的源码,并做相关解释。
const {
ccclass, property} = cc._decorator;
export default class ArcMove extends cc.ActionInterval {
nam