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

然后在角色的脚本里添加

	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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值