Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Game

An object to boot the game.

class

game

static

Hierarchy

Index

Properties

_callbackTable

_callbackTable: ICallbackTable = createMap(true)

_configLoaded

_configLoaded: boolean = false

_frameTime

_frameTime: number | null = null

_gfxDevice

_gfxDevice: WebGL2GFXDevice | WebGLGFXDevice | null = null

_inited

_inited: boolean = false

_intervalId

_intervalId: number | null = null

_isCloning

_isCloning: boolean = false

_lastTime

_lastTime: Date | null = null

_paused

_paused: boolean = true

persistRootNodes

persistRootNodes: object

Type declaration

_rendererInitialized

_rendererInitialized: boolean = false

_sceneInfos

_sceneInfos: ISceneInfo[] = []

canvas

canvas: HTMLCanvasElement | null = null

The canvas of the game.

property

canvas

collisionMatrix

collisionMatrix: never[] = []

config

config: IGameConfig

The current game configuration, please be noticed any modification directly on this object after the game initialization won't take effect.

property

config

container

container: HTMLDivElement | null = null

The container of game canvas.

property

container

eventTargetOn

eventTargetOn: on = super.on

eventTargetOnce

eventTargetOnce: once = super.once

frame

frame: Object | null = null

The outer frame of the game canvas; parent of game container.

property

frame

groupList

groupList: any[] = []

onStart

onStart: Function | null = null

Callback when the scripts of engine have been load.

method

onStart

renderType

renderType: number = -1

The renderer backend of the game.

property

renderType

Static EVENT_ENGINE_INITED

EVENT_ENGINE_INITED: string = "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. In cocos creator 3d, EVENT_RENDERER_INITED is a new event, look up define for details.

property

EVENT_ENGINE_INITED

constant

Static EVENT_GAME_INITED

EVENT_GAME_INITED: string = "game_inited"

Event triggered after game inited, at this point all engine objects and game scripts are loaded

property

EVENT_GAME_INITED

constant

Static EVENT_HIDE

EVENT_HIDE: string = "game_on_hide"

Event triggered when game hide to background.
Please note that this event is not 100% guaranteed to be fired on Web platform,
on native platforms, it corresponds to enter background event, os status bar or notification center may not trigger this event.

property

EVENT_HIDE

example
cc.game.on(Game.EVENT_HIDE, function () {
    cc.audioEngine.pauseMusic();
    cc.audioEngine.pauseAllEffects();
});

Static EVENT_RENDERER_INITED

EVENT_RENDERER_INITED: string = "renderer_inited"

Event triggered after renderer inited, at this point you will be able to use all gfx renderer feature.

property

EVENT_RENDERER_INITED

readonly

Static EVENT_SHOW

EVENT_SHOW: string = "game_on_show"

Event triggered when game back to foreground
Please note that this event is not 100% guaranteed to be fired on Web platform,
on native platforms, it corresponds to enter foreground event.

property

EVENT_SHOW

constant

Static RENDER_TYPE_CANVAS

RENDER_TYPE_CANVAS: number = 0

Web Canvas 2d API as renderer backend.

property

RENDER_TYPE_CANVAS

constant

Static RENDER_TYPE_OPENGL

RENDER_TYPE_OPENGL: number = 2

OpenGL API as renderer backend.

property

RENDER_TYPE_OPENGL

constant

Static RENDER_TYPE_WEBGL

RENDER_TYPE_WEBGL: number = 1

WebGL API as renderer backend.

property

RENDER_TYPE_WEBGL

constant

Accessors

inited

  • get inited(): boolean

Methods

Private ctTime

  • ctTime(id: number | undefined): void

Private determineRenderType

  • _determineRenderType(): void

Private initConfig

Private initDevice

  • _initDevice(): void

Private initEngine

  • _initEngine(): void

Private initEvents

  • _initEvents(): void

Private runMainLoop

  • _runMainLoop(): void

Private setAnimFrame

  • _setAnimFrame(): void

Private stTime

  • stTime(callback: any): number

addPersistRootNode

  • addPersistRootNode(node: object): void
  • Add a persistent root node to the game, the persistent node won't be destroyed during scene transition.
    The target node must be placed in the root level of hierarchy, otherwise this API won't have any effect.

    Parameters

    • node: object

      The node to be made persistent

      • _persistNode: boolean
      • parent: any
      • uuid: any

    Returns void

emit

  • emit(key: string, ...args: any[]): void
  • Trigger an event directly with the event name and necessary arguments.

    Parameters

    • key: string

      event type

    • Rest ...args: any[]

      Arguments when the event triggered

    Returns void

end

  • end(): void

getFrameRate

  • getFrameRate(): number
  • Get frame rate set for the game, it doesn't represent the real frame rate.

    Returns number

    frame rate

hasEventListener

  • hasEventListener(key: string, callback?: Function, target?: Object | null): boolean
  • Checks whether there is correspond event listener registered on the given event

    Parameters

    • key: string

      Event type

    • Optional callback: Function

      Callback function when event triggered

    • Default value target: Object | null = null

      Callback callee

    Returns boolean

init

isPaused

  • isPaused(): boolean

isPersistRootNode

  • isPersistRootNode(node: object): any
  • Check whether the node is a persistent root node.

    Parameters

    • node: object

      The node to be checked

      • _persistNode: any

    Returns any

off

  • off(type: string, callback?: Function, target?: Object): void
  • 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.

    example

    // 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');

    Parameters

    • type: string

      A string representing the event type being removed.

    • Optional callback: Function

      The callback to remove.

    • Optional target: Object

      The target (this object) to invoke the callback, if it's not given, only callback without target will be removed

    Returns void

on

  • on(type: string, callback: Function, target?: undefined | object): any
  • Register an callback of a specific event type on the game object.
    This type of event should be triggered via emit.

    Parameters

    • type: string

      A string representing the event type to listen for.

    • callback: Function

      The callback that will be invoked when the event is dispatched.
      The callback is ignored if it is a duplicate (the callbacks are unique).

    • Optional target: undefined | object

    Returns any

    • Just returns the incoming callback so you can save the anonymous function easier.

once

  • once(type: string, callback: Function, target?: undefined | object): void
  • Register an callback of a specific event type on the game object,
    the callback will remove itself after the first time it is triggered.

    Parameters

    • type: string

      A string representing the event type to listen for.

    • callback: Function

      The callback that will be invoked when the event is dispatched.
      The callback is ignored if it is a duplicate (the callbacks are unique).

    • Optional target: undefined | object

    Returns void

pause

  • pause(): void
  • Pause the game main loop. This will pause:
    game logic execution, rendering process, event manager, background music and all audio effects.
    This is different with cc.director.pause which only pause the game logic execution.

    Returns void

removeAll

  • removeAll(keyOrTarget?: string | Object): void
  • Removes all callbacks registered in a certain event type or all callbacks registered with a certain target

    Parameters

    • Optional keyOrTarget: string | Object

      The event type or target with which the listeners will be removed

    Returns void

removePersistRootNode

  • removePersistRootNode(node: object): void
  • Remove a persistent root node.

    Parameters

    • node: object

      The node to be removed from persistent node list

      • _persistNode: boolean
      • uuid: string

    Returns void

restart

  • restart(): void

resume

  • resume(): void
  • Resume the game from pause. This will resume:
    game logic execution, rendering process, event manager, background music and all audio effects.

    Returns void

run

  • run(onStart: Function | null, legacyOnStart?: Function | null): void
  • Run game with configuration object and onStart function.

    Parameters

    • onStart: Function | null

      function to be executed after game initialized

    • Optional legacyOnStart: Function | null

    Returns void

setFrameRate

  • setFrameRate(frameRate: number): void

Private setRenderPipeline

step

  • step(): void

targetOff

  • targetOff(keyOrTarget?: string | Object): void
  • 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.

    Parameters

    • Optional keyOrTarget: string | Object

    Returns void

Generated using TypeDoc