mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-05-08 22:57:36 +08:00
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
|
|
import { _decorator, Component, Node, input, Input, EventKeyboard, KeyCode, game } from 'cc';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
@ccclass('InputManager')
|
|
export class InputManager extends Component {
|
|
// [1]
|
|
// dummy = '';
|
|
|
|
// [2]
|
|
// @property
|
|
// serializableDummy = 0;
|
|
|
|
onLoad() {
|
|
|
|
}
|
|
|
|
start() {
|
|
|
|
}
|
|
|
|
onEnable() {
|
|
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
|
input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
|
|
|
|
}
|
|
|
|
onDisable() {
|
|
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
|
input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
|
|
}
|
|
|
|
onKeyDown(event: EventKeyboard) {
|
|
switch (event.keyCode) {
|
|
case KeyCode.KEY_A: {
|
|
console.log('key a');
|
|
//left
|
|
game.emit("rule_horizontal_push", 1)
|
|
} break;
|
|
case KeyCode.KEY_D: {
|
|
console.log('key d');
|
|
game.emit("rule_horizontal_push", -1)
|
|
} break;
|
|
case KeyCode.KEY_W: {
|
|
game.emit("rule_vertical_push", 1)
|
|
} break;
|
|
case KeyCode.KEY_S: {
|
|
game.emit("rule_vertical_push", -1)
|
|
} break;
|
|
}
|
|
}
|
|
|
|
onKeyUp(event: EventKeyboard) {
|
|
switch (event.keyCode) {
|
|
case KeyCode.KEY_A: {
|
|
console.log('key a');
|
|
//left
|
|
game.emit("rule_horizontal_push", 0)
|
|
} break;
|
|
case KeyCode.KEY_D: {
|
|
console.log('key d');
|
|
game.emit("rule_horizontal_push", 0)
|
|
} break;
|
|
case KeyCode.KEY_W: {
|
|
game.emit("rule_vertical_push", 0)
|
|
} break;
|
|
case KeyCode.KEY_S: {
|
|
game.emit("rule_vertical_push", 0)
|
|
} break;
|
|
}
|
|
}
|
|
|
|
// update(deltaTime: number) {
|
|
|
|
// }
|
|
}
|
|
|
|
|