In this section, we will introduce the global system events of Cocos Creator.
Global system events are irrelevant with the node hierarchy, so they are dispatched globally by cc.systemEvent
, currently supported:
- Keyboard
- DeviceMotion
If you are searching for any information about touch or mouse events, please refer to Node System Events documentation.
** ATTENTION: We strongly discourage any usage of cc.eventManager
in Cocos Creator, all its functionality can be achieved via
cc.systemEvent and cc.Node's event API. We no longer guarantee the API of cc.eventManager, it could be refactored any time in the future. **
You can use cc.systemEvent.on(type, callback, target)
to register Keyboard and DeviceMotion event listeners.
Event types included:
- cc.SystemEvent.EventType.KEY_DOWN
- cc.SystemEvent.EventType.KEY_UP
- cc.SystemEvent.EventType.DEVICEMOTION
- Type:
cc.SystemEvent.EventType.KEY_DOWN
和cc.SystemEvent.EventType.KEY_UP
- Call Back: Custom Event: callback(event);
- Call Back Parameter:
- KeyCode: API Reference
- Event: API Reference
cc.Class({
extends: cc.Component,
onLoad: function () {
// add key down and key up event
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
},
destroy () {
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
},
onKeyDown: function (event) {
switch(event.keyCode) {
case cc.macro.KEY.a:
console.log('Press a key');
break;
}
},
onKeyUp: function (event) {
switch(event.keyCode) {
case cc.macro.KEY.a:
console.log('release a key');
break;
}
}
});
- Type:
cc.SystemEvent.EventType.DEVICEMOTION
- Call Back: Custom Event: callback(event);
- Call Back Parameter:
- Event: API reference
cc.Class({
extends: cc.Component,
onLoad () {
// open Accelerometer
cc.systemEvent.setAccelerometerEnabled(true);
cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
},
destroy () {
cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
},
onDeviceMotionEvent (event) {
cc.log(event.acc.x + " " + event.acc.y);
},
});
You can go to example-cases cases03_gameplay/01_player_control
(This includes the keyboard, accelerometer, single point touch, multitouch examples
).