Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Game

An object to boot the game.

Hierarchy

Index

Properties

_configLoaded

_configLoaded: boolean = false

_frameTime

_frameTime: number | null = null

_gfxDevice

_gfxDevice: GFXDevice | 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: {}

Type declaration

_rendererInitialized

_rendererInitialized: boolean = false

_sceneInfos

_sceneInfos: ISceneInfo[] = []

canvas

canvas: HTMLCanvasElement | null = null

The canvas of the game.

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.

container

container: HTMLDivElement | null = null

The container of game canvas.

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.

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.

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.

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

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.

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

Static Readonly 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.

Static EVENT_RESTART

EVENT_RESTART: string = "game_on_restart"

Event triggered when game restart

Static Readonly 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.

Static RENDER_TYPE_CANVAS

RENDER_TYPE_CANVAS: number = 0

Web Canvas 2d API as renderer backend.

Static RENDER_TYPE_OPENGL

RENDER_TYPE_OPENGL: number = 2

OpenGL API as renderer backend.

Static RENDER_TYPE_WEBGL

RENDER_TYPE_WEBGL: number = 1

WebGL API as renderer backend.

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 loadRenderPipeline

  • loadRenderPipeline(restart: Function | null): void

Private runMainLoop

  • _runMainLoop(): void

Private safeEmit

  • safeEmit(event: any): void

Private setAnimFrame

  • _setAnimFrame(): void

Private setRenderPipeline

Private stTime

  • stTime(callback: any): number

addPersistRootNode

  • addPersistRootNode(node: { _persistNode: boolean; parent: any; uuid: any }): 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: { _persistNode: boolean; parent: any; uuid: any }

      The node to be made persistent

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

    Returns void

emit

  • emit(type: EventType, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void
  • Trigger an event directly with the event name and necessary arguments.

    Parameters

    • type: EventType

      event type

    • Optional arg0: any
    • Optional arg1: any
    • Optional arg2: any
    • Optional arg3: any
    • Optional arg4: any

    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(type: string, callback?: Function, target?: undefined | object): boolean
  • Checks whether there is correspond event listener registered on the given event.

    Parameters

    • type: string

      Event type.

    • Optional callback: Function

      Callback function when event triggered.

    • Optional target: undefined | object

      Callback callee.

    Returns boolean

init

isPaused

  • isPaused(): boolean

isPersistRootNode

  • isPersistRootNode(node: { _persistNode: any }): boolean
  • Check whether the node is a persistent root node.

    Parameters

    • node: { _persistNode: any }

      The node to be checked

      • _persistNode: any

    Returns boolean

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.

    example

    import { log } from 'cc'; // register fire eventListener var callback = eventTarget.on('fire', function () { log("fire in the hole"); }, target); // remove fire event listener eventTarget.off('fire', callback, target); // remove all fire event listeners eventTarget.off('fire');

    Type parameters

    • TFunction: Function

    Parameters

    • type: EventType

      A string representing the event type being removed.

    • Optional callback: TFunction

      The callback to remove.

    • Optional thisArg: any

    Returns void

on

  • on(type: string, callback: Function, target?: undefined | object, once?: undefined | false | true): 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

      The target (this object) to invoke the callback, can be null

    • Optional once: undefined | false | true

      After the first invocation, whether the callback should be unregistered.

    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

      The target (this object) to invoke the callback, can be null

    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 director.pause which only pause the game logic execution.

    Returns void

removeAll

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

    Parameters

    • typeOrTarget: string | object

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

    Returns void

removePersistRootNode

  • removePersistRootNode(node: { _persistNode: boolean; uuid: string }): void
  • Remove a persistent root node.

    Parameters

    • node: { _persistNode: boolean; uuid: string }

      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

step

  • step(): void

targetOff

  • targetOff(typeOrTarget: 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

    • typeOrTarget: string | object

      The target to be searched for all related listeners

    Returns void

Generated using TypeDoc