获取键盘输入

2022-04-17  本文已影响0人  代瑶
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);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读