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

本文介绍如何使用Cocos2d-x游戏引擎控制游戏角色的移动与旋转,通过监听键盘输入来改变角色的方向和位置,同时触发角色动画。文章详细展示了如何在Cocos2d-x中实现响应键盘事件,调整角色的scaleX和rotation属性,以及如何根据角色移动状态控制动画的播放和停止。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

我这里是修改系统自带的测试项目
在这里插入图片描述

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
       }
    },

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值