laya commercial 3d game development
Goals of this section:
Receive user input
Use of encapsulation framework
Create a new directory scripts\Example
Create a new script
Example01_InPut.ts export class Example01_InPut extends Laya.Script {
onAwake () {
//订阅鼠标(触摸)键盘事件
Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.onMouseDown)
Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onMouseUp)
//键盘事件
Laya.stage.on(Laya.Event.KEY_DOWN, this, this.onKeyDown)
Laya.stage.on(Laya.Event.KEY_UP, this, this.onKeyUp);
}
onKeyDown(e: Laya.Event) {
console.log('onKeyDownEvent', e.keyCode);
}
onKeyUp(e: Laya.Event) {
console.log('onKeyUpEvent', e.keyCode);
}
onMouseDown() {
console.log('p_onMouseDown');
}
onMouseUp() {
console.log('p_onMouseUp');
}
}
Main.ts
onConfigLoaded(): void { let node = new Laya.Node(); Laya.stage.addChild(node); //User input case node.addComponent(Example01_InPut);
}
F8 f5
run the program, log keyboard and mouse information
In order to cater to the usage habits of u3d developers, a class library similar to u3d is encapsulated
Frame use
素材\LayaIde\framework\UnityEngine
Put it in the scirpt directory
Create a new Example02_InPutFramework.ts
export class Example02_InPutFramework extends Laya.Script {
onUpdate() {
if (Input.mouseButton0down) console.log(‘mouseButton0down’)
if (Input.GetKeyDown(Laya.Keyboard.F)) console.log(‘f’)
}
}
Mian.ts
onConfigLoaded(): void { let node = new Laya.Node(); Laya.stage.addChild(node); //User input case// node.addComponent( Example02_InPut); //User input case framework integration node .addComponent(UnityEnagine) node.addComponent(Example02_InPutFramework);
F8 f5
The framework decides whether to enable keyboard events according to the current platform. To support keyboard events, chorme adds 9:16 desktop devices
Refresh
Conclusion: This lesson learned the basic use of keyboard and mouse events,
and also encapsulated a custom mouse and keyboard module. It is
used to be closer to u3d developers.