事件
import { _decorator, Component, Node, SystemEvent, systemEvent, System, EventAcceleration, EventKeyboard, Touch, EventTouch, SystemEventType, macro } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('GameMgr')
export class GameMgr extends Component {
public start(): void { // 在初始化这里,我们就可以监听我们的事件;
// 监听触摸事件
systemEvent.on(SystemEventType.TOUCH_START, this.onTouchStart, this);
systemEvent.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
systemEvent.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
systemEvent.on(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this);
// end
// 监听我们的键盘事件
systemEvent.on(SystemEventType.KEY_DOWN, this.onKeyDown, this);
systemEvent.on(SystemEventType.KEY_UP, this.onKeyUp, this);
// end
// 重力感应
systemEvent.on(SystemEventType.DEVICEMOTION, this.onDeviceMotion, this);
// end
}
onDestroy(): void {
// 当我们开始监听的时候,一定要思考什么时候释放;
systemEvent.off(SystemEventType.TOUCH_START);
// systemEvent.on(SystemEventType.TOUCH_START, this.onTouchStart, this);
systemEvent.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
systemEvent.off(SystemEventType.TOUCH_END, this.onTouchEnd, this);
systemEvent.off(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this);
// end
// 监听我们的键盘事件
systemEvent.off(SystemEventType.KEY_DOWN, this.onKeyDown, this);
systemEvent.off(SystemEventType.KEY_UP, this.onKeyUp, this);
// end
// 重力感应
systemEvent.off(SystemEventType.DEVICEMOTION, this.onDeviceMotion, this);
// end
}
private onDeviceMotion(event: EventAcceleration): void {
}
private onKeyDown(event: EventKeyboard): void {
switch(event.keyCode) {
case macro.KEY.s: // S
console.log("S 建被按下了");
break;
case 87: // w
break;
case 65: // A
break;
case 68: // D
break;
case macro.KEY.space:
console.log("空格按键按下了");
break;
}
}
private onKeyUp(event: EventKeyboard): void {
switch(event.keyCode) {
case 83: // S
break;
case 87: // w
break;
case 65: // A
break;
case 68: // D
break;
}
}
private onTouchStart(touch: Touch, event: EventTouch): void {
// this --->target;
console.log("onTouchStart", touch, touch.getLocation());
}
private onTouchEnd(touch: Touch, event: EventTouch): void {
// this --->target;
console.log("onTouchEnd");
}
private onTouchMove(touch: Touch, event: EventTouch): void {
console.log("onTouchMove");
console.log("getDelta", touch, touch.getDelta());
}
private onTouchCancel(touch: Touch, event: EventTouch): void {
console.log("onTouchCancel");
}
}
最后更新于