• Game 类型
    • 索引
      • 属性(properties)
      • 方法
  • Details
    • 属性(properties)
      • EVENT_HIDE
      • 示例
      • EVENT_SHOW
      • EVENT_GAME_INITED
      • EVENT_ENGINE_INITED
      • RENDER_TYPE_CANVAS
      • RENDER_TYPE_WEBGL
      • RENDER_TYPE_OPENGL
      • frame
      • container
      • canvas
      • renderType
      • config
      • setFrameRate
        • 参数列表
      • getFrameRate
      • step
      • pause
      • resume
      • isPaused
      • restart
      • end
      • on
        • 参数列表
      • once
        • 参数列表
      • prepare
        • 参数列表
      • run
        • 参数列表
      • addPersistRootNode
        • 参数列表
      • removePersistRootNode
        • 参数列表
      • isPersistRootNode
        • 参数列表
      • hasEventListener
        • 参数列表
      • off
        • 参数列表
      • 示例
      • targetOff
        • 参数列表
      • emit
        • 参数列表
      • 示例
      • dispatchEvent
        • 参数列表

    Game 类型

    继承于 EventTarget

    模块: cc

    包含游戏主体信息并负责驱动游戏的游戏对象。

    索引

    属性(properties)
    • EVENT_HIDE String 游戏进入后台时触发的事件。
    • EVENT_SHOW String 游戏进入前台运行时触发的事件。
    • EVENT_GAME_INITED String Event triggered after game inited, at this point all engine objects and game scripts are loaded
    • EVENT_ENGINE_INITED String Event triggered after engine inited, at this point you will be able to use all engine classes….
    • RENDER_TYPE_CANVAS Number Web Canvas 2d API as renderer backend
    • RENDER_TYPE_WEBGL Number WebGL API as renderer backend
    • RENDER_TYPE_OPENGL Number OpenGL API as renderer backend
    • frame Object 游戏画布的外框,container 的父容器。
    • container HTMLDivElement 游戏画布的容器。
    • canvas HTMLCanvasElement 游戏的画布。
    • renderType Number 游戏的渲染器类型。
    • config Object "debugMode" 各种设置选项的意义。
    方法
    • onStart 当引擎完成启动后的回调函数。
    • setFrameRate 设置游戏帧率。
    • getFrameRate 获取设置的游戏帧率(不等同于实际帧率)。
    • step 执行一帧游戏循环。
    • pause 暂停游戏主循环。
    • resume 恢复游戏主循环。
    • isPaused 判断游戏是否暂停。
    • restart 重新开始游戏
    • end 退出游戏
    • on 注册 game 的特定事件类型回调。
    • once 注册 game 的特定事件类型回调,回调会在第一时间被触发后删除自身。
    • prepare 准备引擎,请不要直接调用这个函数。
    • run 运行游戏,并且指定引擎配置和 onStart 的回调。
    • addPersistRootNode 声明常驻根节点,该节点不会被在场景切换中被销毁。
    • removePersistRootNode 取消常驻根节点。
    • isPersistRootNode 检查节点是否是常驻根节点。
    • hasEventListener 检查事件目标对象是否有为特定类型的事件注册的回调。
    • off 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
    • targetOff 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。
    • emit 通过事件名发送自定义事件
    • dispatchEvent 通过事件对象派发事件

    Details

    属性(properties)

    EVENT_HIDE
    游戏进入后台时触发的事件。请注意,在 WEB 平台,这个事件不一定会 100% 触发,这完全取决于浏览器的回调行为。在原生平台,它对应的是应用被切换到后台事件,下拉菜单和上拉状态栏等不一定会触发这个事件,这取决于系统行为。
    metadescription
    类型String
    定义于cocos2d/core/CCGame.js:48
    示例
    1. cc.game.on(cc.game.EVENT_HIDE, function () {
    2. cc.audioEngine.pauseMusic();
    3. cc.audioEngine.pauseAllEffects();
    4. });
    EVENT_SHOW
    Event triggered when game back to foregroundPlease note that this event is not 100% guaranteed to be fired on Web platform,on native platforms, it corresponds to enter foreground event.游戏进入前台运行时触发的事件。请注意,在 WEB 平台,这个事件不一定会 100% 触发,这完全取决于浏览器的回调行为。在原生平台,它对应的是应用被切换到前台事件。
    metadescription
    类型String
    定义于cocos2d/core/CCGame.js:65
    EVENT_GAME_INITED
    Event triggered after game inited, at this point all engine objects and game scripts are loaded
    metadescription
    类型String
    定义于cocos2d/core/CCGame.js:78
    EVENT_ENGINE_INITED
    Event triggered after engine inited, at this point you will be able to use all engine classes.It was defined as EVENT_RENDERER_INITED in cocos creator v1.x and renamed in v2.0
    metadescription
    类型String
    定义于cocos2d/core/CCGame.js:86
    RENDER_TYPE_CANVAS
    Web Canvas 2d API as renderer backend
    metadescription
    类型Number
    定义于cocos2d/core/CCGame.js:97
    RENDER_TYPE_WEBGL
    WebGL API as renderer backend
    metadescription
    类型Number
    定义于cocos2d/core/CCGame.js:104
    RENDER_TYPE_OPENGL
    OpenGL API as renderer backend
    metadescription
    类型Number
    定义于cocos2d/core/CCGame.js:111
    frame
    游戏画布的外框,container 的父容器。
    metadescription
    类型Object
    定义于cocos2d/core/CCGame.js:138
    container
    游戏画布的容器。
    metadescription
    类型HTMLDivElement
    定义于cocos2d/core/CCGame.js:145
    canvas
    游戏的画布。
    metadescription
    类型HTMLCanvasElement
    定义于cocos2d/core/CCGame.js:152
    renderType
    游戏的渲染器类型。
    metadescription
    类型Number
    定义于cocos2d/core/CCGame.js:160
    config
    当前的游戏配置,包括: - debugMode(debug 模式,但是在浏览器中这个选项会被忽略) "debugMode" 各种设置选项的意义。

    1. 0 - 没有消息被打印出来。 <br/>
      1 - cc.errorcc.assertcc.warncc.log 将打印在 console 中。 <br/>
      2 - cc.errorcc.assertcc.warn 将打印在 console 中。 <br/>
      3 - cc.errorcc.assert 将打印在 console 中。 <br/>
      4 - cc.errorcc.assertcc.warncc.log 将打印在 canvas 中(仅适用于 web 端)。 <br/>
      5 - cc.errorcc.assertcc.warn 将打印在 canvas 中(仅适用于 web 端)。 <br/>
      6 - cc.errorcc.assert 将打印在 canvas 中(仅适用于 web 端)。 <br/>


    - showFPS(显示 FPS) 当 showFPS 为 true 的时候界面的左下角将显示 fps 的信息,否则被隐藏。 - exposeClassName 暴露类名让 Chrome DevTools 可以识别,如果开启会稍稍降低类的创建过程的性能,但对对象构造没有影响。 - frameRate (帧率) “frameRate” 设置想要的帧率你的游戏,但真正的FPS取决于你的游戏实现和运行环境。 - id "gameCanvas" Web 页面上的 Canvas Element ID,仅适用于 web 端。 - renderMode(渲染模式) “renderMode” 设置渲染器类型,仅适用于 web 端:

    1. 0 - 通过引擎自动选择。 <br/>
      1 - 强制使用 canvas 渲染。
      2 - 强制使用 WebGL 渲染,但是在部分 Android 浏览器中这个选项会被忽略。 <br/>


    - scenes “scenes” 当前包中可用场景。 注意:请不要直接修改这个对象,它不会有任何效果。
    metadescription
    类型Object
    定义于cocos2d/core/CCGame.js:168



    #### 方法



    ##### onStart


    当引擎完成启动后的回调函数。

    metadescription
    定义于cocos2d/core/CCGame.js:230
    setFrameRate

    设置游戏帧率。

    metadescription
    定义于cocos2d/core/CCGame.js:241
    参数列表
    • frameRate Number
    getFrameRate

    获取设置的游戏帧率(不等同于实际帧率)。

    metadescription
    返回Number
    定义于cocos2d/core/CCGame.js:258
    step

    执行一帧游戏循环。

    metadescription
    定义于cocos2d/core/CCGame.js:268
    pause

    暂停游戏主循环。包含:游戏逻辑,渲染,事件处理,背景音乐和所有音效。这点和只暂停游戏逻辑的 cc.director.pause 不同。

    metadescription
    定义于cocos2d/core/CCGame.js:277
    resume

    恢复游戏主循环。包含:游戏逻辑,渲染,事件处理,背景音乐和所有音效。

    metadescription
    定义于cocos2d/core/CCGame.js:297
    isPaused

    判断游戏是否暂停。

    metadescription
    返回Boolean
    定义于cocos2d/core/CCGame.js:314
    restart

    重新开始游戏

    metadescription
    定义于cocos2d/core/CCGame.js:324
    end

    退出游戏

    metadescription
    定义于cocos2d/core/CCGame.js:351
    on

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

    metadescription
    返回Function
    定义于cocos2d/core/CCGame.js:395
    参数列表
    • 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
    once

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

    metadescription
    定义于cocos2d/core/CCGame.js:425
    参数列表
    • 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
    prepare

    准备引擎,请不要直接调用这个函数。

    metadescription
    定义于cocos2d/core/CCGame.js:453
    参数列表
    • cb Function
    run

    运行游戏,并且指定引擎配置和 onStart 的回调。

    metadescription
    定义于cocos2d/core/CCGame.js:480
    参数列表
    • config Object Pass configuration object or onStart function
    • onStart Function function to be executed after game initialized
    addPersistRootNode

    声明常驻根节点,该节点不会被在场景切换中被销毁。目标节点必须位于为层级的根节点,否则无效。

    metadescription
    定义于cocos2d/core/CCGame.js:494
    参数列表
    • node Node The node to be made persistent
    removePersistRootNode

    取消常驻根节点。

    metadescription
    定义于cocos2d/core/CCGame.js:530
    参数列表
    • node Node The node to be removed from persistent node list
    isPersistRootNode

    检查节点是否是常驻根节点。

    metadescription
    返回Boolean
    定义于cocos2d/core/CCGame.js:544
    参数列表
    • node Node The node to be checked
    hasEventListener

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

    metadescription
    返回Boolean
    定义于cocos2d/core/event/event-target.js:68
    参数列表
    • type String The type of event.
    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
    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