cocos 简单的一个控制图像移动可以旋转

标题cocos 控制图像人物移动可以旋转

我这里是修改系统自带的测试项目
cocos 简单的一个控制图像移动可以旋转_第1张图片

cc.Class({
    extends: cc.Component,

    properties: {
        sheep: {
            default: null,
            type: cc.Node
        },
    },

    // use this for initialization
    onLoad () {

        this.left = false;
        this.right = false;
        this.up = false;
        this.down = false;

        // set initial move direction
        this.speed = 100;

        //add keyboard input listener to call turnLeft and turnRight
        //开启监听按键的事件
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onDestroy () {
    //关闭监听
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onKeyDown (event) {
        const animationComponent = this.sheep.getComponent(cc.Animation)//获取组件
        switch(event.keyCode) {
            case cc.macro.KEY.d:
            //判断动画播放状态如果 不在播放(!)取反 执行代码打开播放,如果在播放不做处理
                if(!animationComponent.getAnimationState('sheep_run').isPlaying){ 
                    animationComponent.play('sheep_run');
                    this.sheep.scaleX = 1;//设置初始方向
                    this.sheep.rotation = 0;//设置旋转的角度
                }
                this.right = true;
                break;
 
            case cc.macro.KEY.a:
                if(!animationComponent.getAnimationState('sheep_run').isPlaying){ 
                    animationComponent.play('sheep_run');
                    this.sheep.scaleX = 1;
                    this.sheep.rotation = 0;
                }
                this.sheep.scaleX = -1;
                this.left = true;
                break;
 
            case cc.macro.KEY.w:
                if(!animationComponent.getAnimationState('sheep_run').isPlaying){ 
                    animationComponent.play('sheep_run');
                    this.sheep.scaleX = 1;
                    this.sheep.rotation = 90;
                }
                this.up = true;
                break;
 
            case cc.macro.KEY.s:
                if(!animationComponent.getAnimationState('sheep_run').isPlaying){ 
                    animationComponent.play('sheep_run');
                    this.sheep.scaleX = 1;
                    this.sheep.rotation = -90;
                }
                this.down = true;
                break;
        }
    },
    onKeyUp(event)
     {
     //获取组件
        const animationComponent = this.sheep.getComponent(cc.Animation)
        switch(event.keyCode) {

            case cc.macro.KEY.d:
                animationComponent.stop();
                this.right = false;
                break;
 
            case cc.macro.KEY.a:
                animationComponent.stop();
                this.left = false;
                break;
    
            case cc.macro.KEY.w:
                animationComponent.stop();
                this.up = false;
                break;
    
            case cc.macro.KEY.s:
                animationComponent.stop();
                this.down = false;
                break;
        }
     },
    // called every frame
    update (dt) {
        if(this.right)
        {
           this.sheep.scaleX = -1;
           this.sheep.x += this.speed*dt;
        }
        else if(this.left)
        {
           this.sheep.scaleX = 1;
           this.sheep.x-=this.speed*dt;
        }
       if(this.up)
       {
           this.sheep.y+=this.speed*dt;
       }
       else if(this.down)
       {
           this.sheep.y-=this.speed*dt
       }
    },

});

你可能感兴趣的:(cocos 简单的一个控制图像移动可以旋转)