Game 类型
继承于 EventTarget
模块: cc
包含游戏主体信息并负责驱动游戏的游戏对象。
索引
属性(properties)
- EVENT_HIDE- String游戏进入后台时触发的事件。
- EVENT_SHOW- String游戏进入前台运行时触发的事件。
- EVENT_RESTART- String调用restart后,触发事件。
- EVENT_GAME_INITED- StringEvent triggered after game inited, at this point all engine objects and game scripts are loaded
- EVENT_ENGINE_INITED- StringEvent triggered after engine inited, at this point you will be able to use all engine classes....
- RENDER_TYPE_CANVAS- NumberWeb Canvas 2d API as renderer backend
- RENDER_TYPE_WEBGL- NumberWebGL API as renderer backend
- RENDER_TYPE_OPENGL- NumberOpenGL 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 参数)注册的所有事件监听器。
- dispatchEvent通过事件对象派发事件
- removeAll移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
- emit通过事件名发送自定义事件
Details
属性(properties)
EVENT_HIDE
游戏进入后台时触发的事件。 请注意,在 WEB 平台,这个事件不一定会 100% 触发,这完全取决于浏览器的回调行为。 在原生平台,它对应的是应用被切换到后台事件,下拉菜单和上拉状态栏等不一定会触发这个事件,这取决于系统行为。
| meta | description | 
|---|---|
| 类型 | String | 
| 定义于 | cocos2d/core/CCGame.js:47 | 
示例
cc.game.on(cc.game.EVENT_HIDE, function () {
    cc.audioEngine.pauseMusic();
    cc.audioEngine.pauseAllEffects();
});
EVENT_SHOW
游戏进入前台运行时触发的事件。 请注意,在 WEB 平台,这个事件不一定会 100% 触发,这完全取决于浏览器的回调行为。 在原生平台,它对应的是应用被切换到前台事件。
| meta | description | 
|---|---|
| 类型 | String | 
| 定义于 | cocos2d/core/CCGame.js:64 | 
EVENT_RESTART
调用restart后,触发事件。
| meta | description | 
|---|---|
| 类型 | String | 
| 定义于 | cocos2d/core/CCGame.js:77 | 
EVENT_GAME_INITED
Event triggered after game inited, at this point all engine objects and game scripts are loaded
| meta | description | 
|---|---|
| 类型 | String | 
| 定义于 | cocos2d/core/CCGame.js:86 | 
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
| meta | description | 
|---|---|
| 类型 | String | 
| 定义于 | cocos2d/core/CCGame.js:94 | 
RENDER_TYPE_CANVAS
Web Canvas 2d API as renderer backend
| meta | description | 
|---|---|
| 类型 | Number | 
| 定义于 | cocos2d/core/CCGame.js:105 | 
RENDER_TYPE_WEBGL
WebGL API as renderer backend
| meta | description | 
|---|---|
| 类型 | Number | 
| 定义于 | cocos2d/core/CCGame.js:112 | 
RENDER_TYPE_OPENGL
OpenGL API as renderer backend
| meta | description | 
|---|---|
| 类型 | Number | 
| 定义于 | cocos2d/core/CCGame.js:119 | 
frame
游戏画布的外框,container 的父容器。
| meta | description | 
|---|---|
| 类型 | Object | 
| 定义于 | cocos2d/core/CCGame.js:146 | 
container
游戏画布的容器。
| meta | description | 
|---|---|
| 类型 | HTMLDivElement | 
| 定义于 | cocos2d/core/CCGame.js:153 | 
canvas
游戏的画布。
| meta | description | 
|---|---|
| 类型 | HTMLCanvasElement | 
| 定义于 | cocos2d/core/CCGame.js:160 | 
renderType
游戏的渲染器类型。
| meta | description | 
|---|---|
| 类型 | Number | 
| 定义于 | cocos2d/core/CCGame.js:168 | 
config
当前的游戏配置,包括:
- debugMode(debug 模式,但是在浏览器中这个选项会被忽略)
"debugMode" 各种设置选项的意义。0 - 没有消息被打印出来。 <br/> 1 - cc.error,cc.assert,cc.warn,cc.log 将打印在 console 中。 <br/> 2 - cc.error,cc.assert,cc.warn 将打印在 console 中。 <br/> 3 - cc.error,cc.assert 将打印在 console 中。 <br/> 4 - cc.error,cc.assert,cc.warn,cc.log 将打印在 canvas 中(仅适用于 web 端)。 <br/> 5 - cc.error,cc.assert,cc.warn 将打印在 canvas 中(仅适用于 web 端)。 <br/> 6 - cc.error,cc.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 端:0 - 通过引擎自动选择。 <br/> 1 - 强制使用 canvas 渲染。 2 - 强制使用 WebGL 渲染,但是在部分 Android 浏览器中这个选项会被忽略。 <br/>- scenes
“scenes” 当前包中可用场景。
注意:请不要直接修改这个对象,它不会有任何效果。
| meta | description | 
|---|---|
| 类型 | Object | 
| 定义于 | cocos2d/core/CCGame.js:176 | 
方法
onStart
当引擎完成启动后的回调函数。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:238 | 
setFrameRate
设置游戏帧率。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:249 | 
参数列表
- frameRateNumber
getFrameRate
获取设置的游戏帧率(不等同于实际帧率)。
| meta | description | 
|---|---|
| 返回 | Number | 
| 定义于 | cocos2d/core/CCGame.js:266 | 
step
执行一帧游戏循环。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:276 | 
pause
暂停游戏主循环。包含:游戏逻辑,渲染,事件处理,背景音乐和所有音效。这点和只暂停游戏逻辑的 cc.director.pause 不同。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:285 | 
resume
恢复游戏主循环。包含:游戏逻辑,渲染,事件处理,背景音乐和所有音效。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:305 | 
isPaused
判断游戏是否暂停。
| meta | description | 
|---|---|
| 返回 | Boolean | 
| 定义于 | cocos2d/core/CCGame.js:323 | 
restart
重新开始游戏
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:333 | 
end
退出游戏
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:363 | 
on
注册 game 的特定事件类型回调。这种类型的事件应该被 emit 触发。
| meta | description | 
|---|---|
| 返回 | Function | 
| 定义于 | cocos2d/core/CCGame.js:413 | 
参数列表
- 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 arg1
- arg2Any arg2
- arg3Any arg3
- arg4Any arg4
- arg5Any arg5
 
- targetObject The target (this object) to invoke the callback, can be null
once
注册 game 的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:444 | 
参数列表
- 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 arg1
- arg2Any arg2
- arg3Any arg3
- arg4Any arg4
- arg5Any arg5
 
- targetObject The target (this object) to invoke the callback, can be null
prepare
准备引擎,请不要直接调用这个函数。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:473 | 
参数列表
- cbFunction
run
运行游戏,并且指定引擎配置和 onStart 的回调。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:500 | 
参数列表
- configObject Pass configuration object or onStart function
- onStartFunction function to be executed after game initialized
addPersistRootNode
声明常驻根节点,该节点不会被在场景切换中被销毁。
目标节点必须位于为层级的根节点,否则无效。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:514 | 
参数列表
- nodeNode The node to be made persistent
removePersistRootNode
取消常驻根节点。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/CCGame.js:550 | 
参数列表
- nodeNode The node to be removed from persistent node list
isPersistRootNode
检查节点是否是常驻根节点。
| meta | description | 
|---|---|
| 返回 | Boolean | 
| 定义于 | cocos2d/core/CCGame.js:564 | 
参数列表
- nodeNode The node to be checked
hasEventListener
检查事件目标对象是否有为特定类型的事件注册的回调。
| meta | description | 
|---|---|
| 返回 | Boolean | 
| 定义于 | cocos2d/core/event/event-target.js:69 | 
参数列表
- typeString The type of event.
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:119 | 
参数列表
- 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
示例
// 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
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:163 | 
参数列表
- targetObject The target to be searched for all related listeners
dispatchEvent
通过事件对象派发事件
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:208 | 
参数列表
- eventEvent
removeAll
移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:235 | 
参数列表
emit
通过事件名发送自定义事件
| meta | description | 
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:309 | 
参数列表
- keyString event type
- arg1Any First argument
- arg2Any Second argument
- arg3Any Third argument
- arg4Any Fourth argument
- arg5Any Fifth argument
示例
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);
