美文网首页
获取键盘输入

获取键盘输入

作者: 代瑶 | 来源:发表于2022-04-17 20:37 被阅读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);
        }
    }
}

相关文章

网友评论

      本文标题:获取键盘输入

      本文链接:https://www.haomeiwen.com/subject/mhnlertx.html