美文网首页
CocosCreator中,让某Node从A点缓动至B点

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

作者: 全新的饭 | 来源:发表于2022-11-15 14:34 被阅读0次

使用方式

  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点

    使用方式 外部需实例化和销毁类(NodeAttrSwitchCtr)对象,实例化时需传入要控制的Node。 外部可...

  • 猜拳游戏主函数

    运行于cocoscreator 中,挂载在主canvas //必须创建一个按钮作为点击node

  • 【CocosCreator】计算手指围绕某点转了几圈

    https://blog.csdn.net/passerma/article/details/106587518 ...

  • CocosCreator-项目资源优化工具-AssetClean

    AssetCleanerForCocosCreator 简介:一个基于Node.js的CocosCreator项目...

  • 《厦门之行》之一

    7月8号,早四点多起床,五点东至汽车安庆站,6:51动车至铜陵北。三个小时的等待以后,转乘深圳北列车。B座位处在中...

  • 脚步声

    至悲至喜 来 去 至闲至汲 缓 急 至动至静 响 息 声声入禅理

  • 小说其实很简单

    在我看来,短篇小说也罢,长篇小说也罢,都是由三部分构成的:叙事,将故事从A点推至B点,最终推至Z点,故事结束;描写...

  • Unity中,飞金币效果

    需求 从a点生成一些金币飞往b点。a、b可以是3D世界中的点,也可以是UI中的点。 用途:获得金币后,出现金币效果...

  • 动点

    1.昨天两个同学开心地跑来跟我说,其他班同学问了个问题,让我们帮解答,我们求得是:1秒。有兴趣的你看看是不是? 如...

  • Cocos Creator 扩展缓动

    前言 Cocos Creator 中内置的缓动中,缺少了一些常用的缓动,比如 ExponentialOutIn,C...

网友评论

      本文标题:CocosCreator中,让某Node从A点缓动至B点

      本文链接:https://www.haomeiwen.com/subject/zsudxdtx.html