Cocos(四)--动画系统(sheep)

动画编辑器

大致步骤: 

1.创建动画

2.创建属性轨道

3.添加关键帧

4.Animation设置

5.运行

双击补间动画可以修改动画播放的属性


给动画插入事件注意几个点



使用动画编辑器制作帧动画:

步骤1:新建sprite节点,改名sheep

步骤2:选中sheep节点

步骤3:点开编辑模式,添加spriteFrame属性

步骤4:将资源管理中的图片拖拽到时间轴上

步骤5:保存关闭,将创建好的clip文件付给sheep的Animation组件中的Default clip


详细代码:

cc.Class({
    extends: cc.Component,

    properties: {
        sheep:cc.Node,
        animation:cc.Animation,
        
        isMoveLeft:false,
        isMoveRight:false,
        isJump:false,
        isIdle:false,  //默认状态
        scaleX:1,     //角色目前方向
        jumodt:2,     //跳跃单位距离
    },

    onLoad () {
        cc.systemEvent.on("keydown",this.onKeyDown,this);
        cc.systemEvent.on("keyup",this.onKeyUp,this);
        this.animation=this.getComponent(cc.Animation);
    },
    onDestroy()
    {
        cc.systemEvent.off("keydown",this.onKeyDown,this);
        cc.systemEvent.off("keyup",this.onKeyUp,this);
    },
    onKeyDown(event)
    {
        if(event.keyCode==cc.macro.KEY.a)
        {
            this.isMoveLeft=true;
        }
        else if(event.keyCode==cc.macro.KEY.d)
        {
            this.isMoveRight=true;
        }
        
        else if(event.keyCode==cc.macro.KEY.space)
        {
            this.isJump=true;
        }
    },
    onKeyUp(event)
    {
        if(event.keyCode==cc.macro.KEY.a)
        {
            this.isMoveLeft=false;
        }
        else if(event.keyCode==cc.macro.KEY.d)
        {
            this.isMoveRight=false;
        }
        else if(event.keyCode==cc.macro.KEY.space)
        {
            this.isJump=false;
        }
    },

    update (dt) 
    {
        var moveDir=this.isMoveLeft ?-1 : this.isMoveRight ? 1:0;
        if(moveDir !=0)
        {
            const moveSpeed=100;
            var dist=moveSpeed * dt * moveDir;
            this.node.x += dist;
            this.node.scaleX=moveDir<0 ? 1 : -1;
        

         if(!this.isPlaying("run"))
            {
             this.animation.play("run");
             }
             this.isIdle = false;
        }
        else
        {
            if(!this.isIdle)
            {
                this.isIdle = true;
                this.animation.play("jump");
            }
        }
    },
    isPlaying(animName)
    {
        var animState=this.animation.getAnimationState(animName);
        return animState && animState.isPlaying;
    },
    
});

其中有一个特殊符号 “?”,如:

var moveDir=this.isMoveLeft ?-1 : this.isMoveRight ? 1:0; 

其大致和以下代码意思类似

if (moveDir=this.isMoveLeft)

      moveDir=-1;

else(moveDir=this.isMoveRight ) 

        moveDir=1;

else

        moveDir=0;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

靖簳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值