• SystemEvent 类型
    • 索引
      • 方法
  • Details
    • 方法
      • setAccelerometerEnabled
        • 参数列表
      • setAccelerometerInterval
        • 参数列表
      • hasEventListener
        • 参数列表
      • on
        • 参数列表
      • 示例
      • off
        • 参数列表
      • 示例
      • targetOff
        • 参数列表
      • once
        • 参数列表
      • 示例
      • emit
        • 参数列表
      • 示例
      • dispatchEvent
        • 参数列表

    SystemEvent 类型

    继承于 EventTarget

    模块: cc父模块: cc

    系统事件,它目前支持按键事件和重力感应事件。你可以通过 cc.systemEvent 获取到 SystemEvent 的实例。参考示例:

    1. cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
    2. cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);

    索引

    方法
    • setAccelerometerEnabled 是否启用加速度计事件
    • setAccelerometerInterval 设置加速度计间隔值
    • hasEventListener 检查事件目标对象是否有为特定类型的事件注册的回调。
    • on 注册事件目标的特定事件类型回调。
    • off 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
    • targetOff 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。
    • once 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
    • emit 通过事件名发送自定义事件
    • dispatchEvent 通过事件对象派发事件

    Details

    方法

    setAccelerometerEnabled

    是否启用加速度计事件

    metadescription
    定义于cocos2d/core/event/system-event.js:100
    参数列表
    • isEnable Boolean
    setAccelerometerInterval

    设置加速度计间隔值

    metadescription
    定义于cocos2d/core/event/system-event.js:110
    参数列表
    • interval Number
    hasEventListener

    检查事件目标对象是否有为特定类型的事件注册的回调。

    metadescription
    返回Boolean
    定义于cocos2d/core/event/event-target.js:68
    参数列表
    • type String The type of event.
    on

    注册事件目标的特定事件类型回调。这种类型的事件应该被 emit 触发。

    metadescription
    返回Function
    定义于cocos2d/core/event/event-target.js:76
    参数列表
    • type String A string representing the event type to listen for.
    • callback Function The callback that will be invoked when the event is dispatched.
    1. The callback is ignored if it is a duplicate (the callbacks are unique).
    • arg1 Any arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 Any arg5
      • target Object The target (this object) to invoke the callback, can be null
    示例
    1. eventTarget.on('fire', function () {
    2. cc.log("fire in the hole");
    3. }, node);
    off

    删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。

    metadescription
    定义于cocos2d/core/event/event-target.js:116
    参数列表
    • type String A string representing the event type being removed.
    • callback Function The callback to remove.
    • target Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
    示例
    1. // register fire eventListener
    2. var callback = eventTarget.on('fire', function () {
    3. cc.log("fire in the hole");
    4. }, target);
    5. // remove fire event listener
    6. eventTarget.off('fire', callback, target);
    7. // remove all fire event listeners
    8. eventTarget.off('fire');
    targetOff

    在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。

    metadescription
    定义于cocos2d/core/event/event-target.js:150
    参数列表
    • target Object The target to be searched for all related listeners
    once

    注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。

    metadescription
    定义于cocos2d/core/event/event-target.js:163
    参数列表
    • type String A string representing the event type to listen for.
    • callback Function The callback that will be invoked when the event is dispatched.
    1. The callback is ignored if it is a duplicate (the callbacks are unique).
    • arg1 Any arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 Any arg5
      • target Object The target (this object) to invoke the callback, can be null
    示例
    1. eventTarget.once('fire', function () {
    2. cc.log("this is the callback and will be invoked only once");
    3. }, node);
    emit

    通过事件名发送自定义事件

    metadescription
    定义于cocos2d/core/event/event-target.js:200
    参数列表
    • type String event type
    • arg1 Any First argument
    • arg2 Any Second argument
    • arg3 Any Third argument
    • arg4 Any Fourth argument
    • arg5 Any Fifth argument
    示例
    1. eventTarget.emit('fire', event);
    2. eventTarget.emit('fire', message, emitter);
    dispatchEvent

    通过事件对象派发事件

    metadescription
    定义于cocos2d/core/event/event-target.js:220
    参数列表
    • event Event