cocoscreator3.X 动态创建帧动画


import { _decorator, Component, Node, resources, path, SpriteFrame ,SpriteAtlas,Animation,AnimationClip, Enum} from 'cc';
import { Joystick,CheckTouch } from './Joystick';
const { ccclass, property } = _decorator;
export let _animation:Animation;                //动画组建
export let speed=1;                             //速度
export let skill=1;                             //默认技能
export let RoleStatus:boolean=false;
@ccclass('role')
export class role extends Component {

    nAnim:Animation=null;
    start() {
      
    }
    onLoad(){
        _animation=this.node.getChildByName("role").addComponent(Animation);
        this.nAnim=_animation;
    }
    update(deltaTime: number) {
       
    }
     //设置武器偏移量
    setWuqiOffSet(x:number,y:number,k:number){
        this.node.setPosition(x,y/2)
    }
    //设置角色偏移量
    setRoleOffSet(x:number,y:number,k:number){
        this.node.setPosition(x,y)
    }
    //加载角色动画资源
    onloadRoleRes(){
        resources.load("animation/role/1", SpriteAtlas, (err: any, spFrame: SpriteAtlas) => {
            if (err && !spFrame) {
                console.log("加载失败")
                return;
            }
            // _animation =this.node.getChildByName("role").addComponent(Animation);//添加动画组建
            let frames:SpriteFrame[] =spFrame.getSpriteFrames();//this.sprAtlas.getSpriteFrames();//data.getSpriteFrames();
            var clip = AnimationClip.createWithSpriteFrames(frames, frames.length);//创建动画帧图
            frames.forEach((v,k) => {
            var x=v['_offset']['x'];
            var y=v['_offset']['y'];
            console.log( speed/frames.length*k)
            // 添加帧事件
            clip.events.push({
                frame: 1/frames.length*k,         // 秒
                func: "setRoleOffSet",// 回调函数名称
                params: [x.toString(), y.toString(),k.toString()]    // 回调参数
            });
            });
            clip.name = "role";
            clip.speed = speed;//速度
            clip.sample = 1;// 每秒播放帧数
            clip.wrapMode = 0;//模式
            _animation.addClip(clip);
            _animation.defaultClip=clip
            RoleStatus=true;
            _animation.play();
            //监听播放结束
            _animation.on(Animation.EventType.FINISHED, (msg) => {
                RoleStatus=false;
            }, this);
        });
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值