SystemEvent Class
Extends EventTarget
Module: cc
The System event, it currently supports keyboard events and accelerometer events.
You can get the SystemEvent instance with cc.systemEvent.
Examples
cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
Index
Methods
setAccelerometerEnabledwhether enable accelerometer eventsetAccelerometerIntervalset accelerometer interval valuehasEventListenerChecks whether the EventTarget object has any callback registered for a specific type of event.onRegister an callback of a specific event type on the EventTarget.offRemoves the listeners previously registered with the same type, callback, target and or useCapture,...targetOffRemoves all callbacks previously registered with the same target (passed as parameter).onceRegister an callback of a specific event type on the EventTarget,...dispatchEventSend an event with the event object.removeAllRemoves all callbacks registered in a certain event type or all callbacks registered with a certain targetemitTrigger an event directly with the event name and necessary arguments.
Details
Methods
setAccelerometerEnabled
whether enable accelerometer event
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/system-event.js:93 |
Parameters
isEnableBoolean
setAccelerometerInterval
set accelerometer interval value
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/system-event.js:115 |
Parameters
intervalNumber
hasEventListener
Checks whether the EventTarget object has any callback registered for a specific type of event.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/event/event-target.js:69 |
Parameters
typeString The type of event.
on
Register an callback of a specific event type on the EventTarget.
This type of event should be triggered via emit.
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/event/event-target.js:77 |
Parameters
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
Examples
eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, node);
off
Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:119 |
Parameters
typeString A string representing the event type being removed.callbackFunction The callback to remove.targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
Examples
// register fire eventListener
var callback = eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
targetOff
Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:163 |
Parameters
targetObject The target to be searched for all related listeners
once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:182 |
Parameters
typeString A string representing the event type to listen for.callbackFunction The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
Examples
eventTarget.once('fire', function () {
cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent
Send an event with the event object.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:208 |
Parameters
eventEvent
removeAll
Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/callbacks-invoker.js:235 |
Parameters
emit
Trigger an event directly with the event name and necessary arguments.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/callbacks-invoker.js:309 |
Parameters
keyString event typearg1Any First argumentarg2Any Second argumentarg3Any Third argumentarg4Any Fourth argumentarg5Any Fifth argument
Examples
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);