Cocos Creator之Tween
  RlWeLU85QNwT 2023年11月02日 67 0

一, 测试引擎版本 2.4.9

二, Tween是在Action的基础上进行的封装.

三, 代码

import callFunc = cc.callFunc;

const {ccclass, property} = cc._decorator;

@ccclass
export default class Demo_Tween extends cc.Component {
@property(cc.Node)
private img: cc.Node;

private _labTitle: cc.Label;

private _tween: cc.Tween;
private _showObj: { num: number };

protected onLoad(): void {
this._labTitle = cc.find("labTitle", this.node).getComponent(cc.Label);
}

protected start(): void {
// this.moveToAnyBy();
// this.bezierTween();
// this.anyObjTween();
// this.easingTween();
// this.progressTween();
// this.progressAllTween();
// this.insertOtherTween();
this.sequenceTween();
// this.parallelAndCallbackTween();
// this.repeatTween();
// this.repeatForeverTween();
// this.unionTween();
// this.delayTween();
}

//To移动到指定位置 , By在target的基础上进行变动
private moveToAnyBy(): void {
this._tween = cc.tween(this.img);//获得缓动目标
this._tween.to(1, {
position: cc.v2(100, 100), angle: -90
}).by(1, {scale: 2});
this._tween.start();
}

//贝塞尔曲线
private bezierTween(): void {
this._tween = cc.tween(this.img);
this._tween.bezierTo(3, cc.v2(0, cc.winSize.height / 2), cc.v2(300, -cc.winSize.height / 2), cc.v2(300, 100));
this._tween.start();
}

//支持任意对象的任意属性
private anyObjTween(): void {
this._showObj = {num: 0};
this._tween = cc.tween(this._showObj);
this._tween.to(1, {num: 100});
this._tween.start();
}

//变速 easing
private easingTween(): void {
this._tween = cc.tween(this.img);
this._tween.to(1, {scale: 2, position: cc.v2(100, 100), angle: -90}, cc.easeIn(3.0));
this._tween.start();
}

//自定义的progress对单个属性
private progressTween(): void {
this._tween = cc.tween(this.img);
this._tween.to(1, {
scale: 2,
position: {
value: cc.v3(100, 200, 0),
progress: (start, end, current, t) => {
return start.lerp(end, t, current);
}
}
});
this._tween.start();
}

//自定义的progress对所有属性
private progressAllTween(): void {
this._tween = cc.tween(this.img);
this._tween.to(1, {scale: 2, rotation: 90}, {
progress: (start, end, current, ratio) => {
return start + (end - start) * ratio;
}
});
this._tween.start();
}

//插入其他缓动到队列(逐一执行action)
private insertOtherTween(): void {
let scale: cc.Tween = cc.tween(this.img).to(1, {scale: 2});
let rotation: cc.Tween = cc.tween(this.img).to(1, {angle: -90});
let move: cc.Tween = cc.tween(this.img).to(1, {position: cc.v3(100, 100, 0)});
this._tween = cc.tween(this.img);
this._tween.then(scale).then(rotation).then(move);
this._tween.start();
}

//逐一执行, 相当于上面的then
private sequenceTween(): void {
let scale: cc.Tween = cc.tween(this.img).to(1, {scale: 2});
let rotation: cc.Tween = cc.tween(this.img).to(1, {angle: -90});
let move: cc.Tween = cc.tween(this.img).to(1, {position: cc.v3(100, 100, 0)});
this._tween = cc.tween(this.img);
this._tween.sequence(scale, rotation, move);
this._tween.call(() => {
cc.log("ok");
});
this._tween.start();
}

//并行执行缓动及回调 (逐步执行)
private parallelAndCallbackTween(): void {
this._tween = cc.tween(this.img);
this._tween.parallel(
this._tween.to(1, {scale: 2}),
this._tween.to(2, {position: cc.v2(100, 200)})
);
this._tween.call(() => {
cc.log(`%cAll tweens finished.`, `color:#A00`);
});
this._tween.start();
}

//重复执行
private repeatTween(): void {
this._tween = cc.tween(this.img);
this._tween.by(1, {scale: 1});
this._tween.repeat(10); // ===> 最后为11
this._tween.start();
}

//无限重复
private repeatForeverTween(): void {
this._tween = cc.tween(this.img);
this._tween.by(1, {scale: 1.1});
this._tween.repeatForever();
this._tween.start();
}

//repeat/repeatForever 函数只会将前一个 action 作为作用对象,如果希望重复多个 action 话,可是使用 union(将之前所有的 action 整合为一个 action)
private unionTween(): void {
this._tween = cc.tween(this.img);
this._tween.by(2, {angle: -90});
this._tween.by(1, {scale: 2});
this._tween.union();//还是逐步执行
this._tween.repeat(10);
this._tween.start();
}

//延迟执行
private delayTween(): void {
this._tween = cc.tween(this.img);
this._tween.delay(1);//先延迟1S
this._tween.to(1, {scale: 2});
this._tween.delay(2);
this._tween.to(1, {scale: 1});
this._tween.start();
}

//#region
//暂停本节点上所有正在运行的动作(这里指的是img节点)
private stopAllInNode(): void {
this.img.stopAllActions();
// cc.Tween.stopAllByTarget(this.img);
}

//恢复运行本节点上所有暂停的动作(这里指的是img节点)
private resumeAllInNode(): void {
this.img.resumeAllActions();
}

//停止tween
private stopTween(): void {
if (this._tween) {
this._tween.stop();
}
}

//#endregion

protected update(dt: number) {
if (!this._showObj) return;
this._labTitle.string = `num: ${this._showObj.num.toFixed()}`;
}

}
import callFunc = cc.callFunc;

const {ccclass, property} = cc._decorator;

@ccclass
export default class Demo_Tween extends cc.Component {
@property(cc.Node)
private img: cc.Node;

private _labTitle: cc.Label;

private _tween: cc.Tween;
private _showObj: { num: number };

protected onLoad(): void {
this._labTitle = cc.find("labTitle", this.node).getComponent(cc.Label);
}

protected start(): void {
// this.moveToAnyBy();
// this.bezierTween();
// this.anyObjTween();
// this.easingTween();
// this.progressTween();
// this.progressAllTween();
// this.insertOtherTween();
this.sequenceTween();
// this.parallelAndCallbackTween();
// this.repeatTween();
// this.repeatForeverTween();
// this.unionTween();
// this.delayTween();
}

//To移动到指定位置 , By在target的基础上进行变动
private moveToAnyBy(): void {
this._tween = cc.tween(this.img);//获得缓动目标
this._tween.to(1, {
position: cc.v2(100, 100), angle: -90
}).by(1, {scale: 2});
this._tween.start();
}

//贝塞尔曲线
private bezierTween(): void {
this._tween = cc.tween(this.img);
this._tween.bezierTo(3, cc.v2(0, cc.winSize.height / 2), cc.v2(300, -cc.winSize.height / 2), cc.v2(300, 100));
this._tween.start();
}

//支持任意对象的任意属性
private anyObjTween(): void {
this._showObj = {num: 0};
this._tween = cc.tween(this._showObj);
this._tween.to(1, {num: 100});
this._tween.start();
}

//变速 easing
private easingTween(): void {
this._tween = cc.tween(this.img);
this._tween.to(1, {scale: 2, position: cc.v2(100, 100), angle: -90}, cc.easeIn(3.0));
this._tween.start();
}

//自定义的progress对单个属性
private progressTween(): void {
this._tween = cc.tween(this.img);
this._tween.to(1, {
scale: 2,
position: {
value: cc.v3(100, 200, 0),
progress: (start, end, current, t) => {
return start.lerp(end, t, current);
}
}
});
this._tween.start();
}

//自定义的progress对所有属性
private progressAllTween(): void {
this._tween = cc.tween(this.img);
this._tween.to(1, {scale: 2, rotation: 90}, {
progress: (start, end, current, ratio) => {
return start + (end - start) * ratio;
}
});
this._tween.start();
}

//插入其他缓动到队列(逐一执行action)
private insertOtherTween(): void {
let scale: cc.Tween = cc.tween(this.img).to(1, {scale: 2});
let rotation: cc.Tween = cc.tween(this.img).to(1, {angle: -90});
let move: cc.Tween = cc.tween(this.img).to(1, {position: cc.v3(100, 100, 0)});
this._tween = cc.tween(this.img);
this._tween.then(scale).then(rotation).then(move);
this._tween.start();
}

//逐一执行, 相当于上面的then
private sequenceTween(): void {
let scale: cc.Tween = cc.tween(this.img).to(1, {scale: 2});
let rotation: cc.Tween = cc.tween(this.img).to(1, {angle: -90});
let move: cc.Tween = cc.tween(this.img).to(1, {position: cc.v3(100, 100, 0)});
this._tween = cc.tween(this.img);
this._tween.sequence(scale, rotation, move);
this._tween.call(() => {
cc.log("ok");
});
this._tween.start();
}

//并行执行缓动及回调 (逐步执行)
private parallelAndCallbackTween(): void {
this._tween = cc.tween(this.img);
this._tween.parallel(
this._tween.to(1, {scale: 2}),
this._tween.to(2, {position: cc.v2(100, 200)})
);
this._tween.call(() => {
cc.log(`%cAll tweens finished.`, `color:#A00`);
});
this._tween.start();
}

//重复执行
private repeatTween(): void {
this._tween = cc.tween(this.img);
this._tween.by(1, {scale: 1});
this._tween.repeat(10); // ===> 最后为11
this._tween.start();
}

//无限重复
private repeatForeverTween(): void {
this._tween = cc.tween(this.img);
this._tween.by(1, {scale: 1.1});
this._tween.repeatForever();
this._tween.start();
}

//repeat/repeatForever 函数只会将前一个 action 作为作用对象,如果希望重复多个 action 话,可是使用 union(将之前所有的 action 整合为一个 action)
private unionTween(): void {
this._tween = cc.tween(this.img);
this._tween.by(2, {angle: -90});
this._tween.by(1, {scale: 2});
this._tween.union();//还是逐步执行
this._tween.repeat(10);
this._tween.start();
}

//延迟执行
private delayTween(): void {
this._tween = cc.tween(this.img);
this._tween.delay(1);//先延迟1S
this._tween.to(1, {scale: 2});
this._tween.delay(2);
this._tween.to(1, {scale: 1});
this._tween.start();
}

//#region
//暂停本节点上所有正在运行的动作(这里指的是img节点)
private stopAllInNode(): void {
this.img.stopAllActions();
// cc.Tween.stopAllByTarget(this.img);
}

//恢复运行本节点上所有暂停的动作(这里指的是img节点)
private resumeAllInNode(): void {
this.img.resumeAllActions();
}

//停止tween
private stopTween(): void {
if (this._tween) {
this._tween.stop();
}
}

//#endregion

protected update(dt: number) {
if (!this._showObj) return;
this._labTitle.string = `num: ${this._showObj.num.toFixed()}`;
}

}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论