Egret Pro学习之路(一)输入

新建一个TS文件,命名为Test.ts,写上以下内容
import { Application, Behaviour } from "@egret/engine";
import { component } from "@egret/ecs/dist";
@component()
export class test extends Behaviour {
}
导入InputCode, InputManager模块
import { InputCode, InputManager } from "@egret/input";
在test类中写上以下内容,onUpdate方法每帧都会调用一次,onStart方法开始的时候会调用一次,InputManager为输入管理器,用来处理键盘按键的按下抬起等。
private _inputManager: InputManager;
public onUpdate() {
}
public onStart() {
}
在onStart方法中写上以下内容
this._inputManager = Application.instance.globalEntity.getComponent(InputManager)!;
在onUpdate方法中写上以下内容
if (this._inputManager.getInput(InputCode.KeyW).isDown) {
console.log("按下W键");
}
else if (this._inputManager.getInput(InputCode.KeyW).isUp) {
console.log("抬起W键");
}
else if (this._inputManager.getInput(InputCode.KeyW).isHold) {
console.log("长按W键");
}
然后在Main.ts中写上以下内容
import { test } from "./Test";
这样我们就可以在Egret Pro里添加上我们写的这个组件了。

运行效果如下

参考资料
Egret Pro官方文档:http://pro.egret.com/docs/guide/engine-features-input/