• cocos 3.x 2D角色键盘移动


    版本3.6.1

    先创建节点 master

    在这里插入图片描述

    添加组件 RigidBody2DBoxCollider2D自定义脚本

    在这里插入图片描述

    简单的封装下移动事件

    import { _decorator, input, Input } from 'cc';
    
    let instance: AxInput = null!;
    
    export class AxInput {
        private _pressd_map: any = {};
        private _just_pressd_map: any = {};
        private _just_released_map: any = {};
    
        // 标记按键是否按下
        private set_pressed_status(k: number, status: boolean) {
            if (status != false && !!this._pressd_map[k]) {
                return;
            }
    
            this._pressd_map[k] = status;
            this._just_pressd_map[k] = status;
        }
    
        // 标记按键松开状态
        private set_released_status(k: number, status: boolean) {
            this._just_released_map[k] = status;
        }
    
        // 获取按键按压状态
        is_action_pressed(k: number) {
            return !!this._pressd_map[k];
        }
    
        // 获取按键按压状态(仅一次)
        is_action_just_pressed(k: number) {
            let pressed_status = this._just_pressd_map[k];
            this._just_pressd_map[k] = false;
            return !!pressed_status;
        }
    
        // 获取按键松开状态(仅一次)
        is_action_just_released(k: number) {
            let released_status = this._just_released_map[k];
            this._just_released_map[k] = false;
            return !!released_status;
        }
    
        constructor() {
    
            input.on(Input.EventType.KEY_DOWN, (e)=>{
                this.set_pressed_status(e.keyCode, true);
                this.set_released_status(e.keyCode, false);
            }, this);
            
            input.on(Input.EventType.KEY_UP, (e)=>{
                this.set_pressed_status(e.keyCode, false);
                this.set_released_status(e.keyCode, true);
            }, this);
    
        }
    
        static get instance() {
            if (!instance) {
                instance = new AxInput();
            }
    
            return instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    然后在角色的脚本里添加

    	import { _decorator, Component,tween,Vec2, input, Input, EventKeyboard, KeyCode,TiledMap,Node,RigidBody2D,macro,PhysicsSystem2D,v2,instantiate,Prefab,Label,Vec3,PHYSICS_2D_PTM_RATIO,EPhysics2DDrawFlags } from 'cc';
    const { ccclass, property } = _decorator;
    
    import { AxInput } from './AxInput';
    const EventKey = AxInput.instance;
    
    @ccclass('master')
    export class master extends Component {
        
        onLoad(){
            PhysicsSystem2D.instance.enable = true
            PhysicsSystem2D.instance.gravity = v2();
            
        }
        start() {
    
        }
        update(deltaTime: number) {
            
            let speed = 8;
            let rb = this.node.getComponent(RigidBody2D);
            let lv = rb!.linearVelocity;
            if (EventKey.is_action_pressed(KeyCode.KEY_W)) {
                lv.y = speed;
            } else if (EventKey.is_action_pressed(KeyCode.KEY_S)) {
                lv.y = -speed;
            } else if (EventKey.is_action_pressed(KeyCode.KEY_A)) {
                lv.x = -speed;
            } else if (EventKey.is_action_pressed(KeyCode.KEY_D)) {
                lv.x = speed;
            }else {
                lv = new Vec2(0, 0);
            }
    
            rb!.linearVelocity = lv;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    电脑键盘各按键的作用及常用的快捷键总结
    tf-vsn网络
    Vue:表单双绑、组件
    Java · 数组 · 作为方法的参数 · 作为方法的返回值 · 二分查找 · 冒泡排序
    企业人事管理系统
    图形学-几何-网格操作
    Spring boot easyexcel 实现复合数据导出、按模块导出
    flask+python快速搭建
    Anaconda 踩过的一些坑
    OpenSergo & ShardingSphere 社区共建微服务视角的数据库治理标准
  • 原文地址:https://blog.csdn.net/c347087870/article/details/127867710