获取键盘输入

import { property, EditType } from "@egret/core"
import { component } from "@egret/ecs"
import { Application, Behaviour } from "@egret/engine"
import { InputCode, InputManager } from "@egret/input";

@component()
export class InputTest extends Behaviour {
    @property(EditType.Float)
    public moveSpeed: float = 10;

    @property(EditType.Float)
    public routateSpeed: float = 50;

    onUpdate(dt) {
        console.log("进入update"); 
        const inputManager = Application.instance.globalEntity.getComponent(InputManager);

        if (inputManager.getInput(InputCode.KeyW).isHold) {
            this.entity.transform.position = this.entity.transform.position.add(
                this.entity.transform.getForward().multiplyScalar(dt * this.moveSpeed)
            );
        }

        if (inputManager.getInput(InputCode.KeyS).isHold) {
            this.entity.transform.position = this.entity.transform.position.subtract(
                this.entity.transform.getForward().multiplyScalar(dt * this.moveSpeed)
            );
        }

        if (inputManager.getInput(InputCode.KeyA).isHold) {
            this.entity.transform.rotate(0, -dt * this.routateSpeed, 0);
        }
        if (inputManager.getInput(InputCode.KeyD).isHold) {
            this.entity.transform.rotate(0, dt * this.routateSpeed, 0);
        }
    }
}

你可能感兴趣的:(获取键盘输入)