CocosCreator中,让某Node从A点缓动至B点

使用方式

  1. 外部需实例化和销毁类(NodeAttrSwitchCtr)对象,实例化时需传入要控制的Node。
  2. 外部可调用前述对象的switch方法让Node开始运动(位置、旋转、大小的变化),需传入NodeAttr。

代码

NodeAttrSwitchCtr.ts

import { _decorator, Component, Node, CurveRange, CCFloat, Vec3, Quat } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NodeAttr')
export class NodeAttr
{ 
    @property({ type: Node, visible: true, displayName: '目标Node' })
    public refNode: Node;
    @property({ type: CCFloat, visible: true, displayName: '过程时长(秒)' })
    public duration: number = 1;
    @property({ type: CurveRange, visible: true, displayName: '变化曲线' })
    public curve: CurveRange = new CurveRange();
}

// 控制某node的属性变化(有过程地变化)
@ccclass('NodeAttrSwitchCtr')
export class NodeAttrSwitchCtr
{ 
    private _node: Node;
    private _curAttr: NodeAttr;
    private get _isInChange(): boolean { return this._curAttr != null; }
    
    private static readonly timeInterval = 0.02;
    private _intervalId: number;

    public constructor(node: Node)
    { 
        this._node = node;
        this._curAttr = null;
        this.endSwitch();
    }

    public myDestroy(): void
    { 
        this._node = null;
        this.endSwitch();
    }

    public switch(attr:NodeAttr): void
    { 
        this.endSwitch();
        this._curAttr = attr;

        const cnt = Math.floor(this._curAttr.duration * 1000 / NodeAttrSwitchCtr.timeInterval);
        let curTime = 0;

        const beginPos = new Vec3(this._node.getWorldPosition());
        const beginRot = new Quat(this._node.getWorldRotation());
        const beginScale = new Vec3(this._node.getWorldScale());
        const endPos = new Vec3(this._curAttr.refNode.getWorldPosition());
        const endRot = new Quat(this._curAttr.refNode.getWorldRotation());
        const endScale = new Vec3(this._curAttr.refNode.getWorldScale());
        let pos: Vec3 = new Vec3();
        let rot: Quat = new Quat();
        let scale: Vec3 = new Vec3();
        let ratio = 0;

        this._intervalId = setInterval(() =>
        { 
            if (curTime < this._curAttr.duration)
            { 
                ratio = this._curAttr.curve.spline.evaluate(curTime / this._curAttr.duration);
                Vec3.lerp(pos, beginPos, endPos, ratio);
                Quat.lerp(rot, beginRot, endRot, ratio);
                Vec3.lerp(scale, beginScale, endScale, ratio);
                this._node.setWorldPosition(pos);
                this._node.setWorldRotation(rot);
                this._node.setWorldScale(scale);
                curTime += NodeAttrSwitchCtr.timeInterval;
            }
            else
            { 
                this.endSwitch();
            }

        }, NodeAttrSwitchCtr.timeInterval);
        // console.info('开始切换');
    }

    private endSwitch(): void
    { 
        if (this._isInChange)
        { 
            clearInterval(this._intervalId);
            this._node.setWorldPosition(this._curAttr.refNode.getWorldPosition());
            this._node.setWorldRotation(this._curAttr.refNode.getWorldRotation());
            this._node.setWorldScale(this._curAttr.refNode.getWorldScale());
            this._curAttr = null;
            // console.info('结束切换');
        }
    }
}

你可能感兴趣的:(CocosCreator中,让某Node从A点缓动至B点)