API Docs for: 1.0.1

Grape.Layer Class

A layer contains instances, systems, and other layers. When any event is emitted to the layer, it is emitted to the systems and layers of the layer too. When you want to subscribe to the layer events with a GameObject (which is added to the layer) you can use the gameObject.onGlobal() function or the global-event keyword.

Constructor

Grape.Layer

(
  • opts
)

Parameters:

  • opts Object

    Initial properties

Methods

add

(
  • instance
)
Grape.GameObject

Adds an instance to the layer and emits it's "add" event.

Parameters:

Returns:

Grape.GameObject:

The added instance

addLayer

(
  • [name]
  • layer
)

Adds a sub-layer to the current layer.

Parameters:

  • [name] String optional

    Layer name

  • layer Grape.Layer

    Sub-layer

addSystem

(
  • [name]
  • system
)

Adds a system to the layer. All events are emitted to all added systems.

Parameters:

  • [name] String optional

    Name

  • system Grape.System

    The system

addView

(
  • name
  • view
)

Adds a view to the layer. A synonym for addSystem.

Parameters:

createResultContainer

() Grape.GameObjectArray

Inherited from Grape.TagContainer but overwritten in js\grape\game\layer.js:153

Overrides the TagContainer's method so getByTag calls produce GameObjectArray results instead of Array.

emit

(
  • event
  • payload
)

Emits an event to the instance: calls all event listeners subscribed to this event, or the 'any' event.

Parameters:

  • event String

    Event

  • payload

    An object passed as parameter to all event listeners.

get

(
  • classes
  • [descendants=false]
)
GameObjectArray

Inherited from Grape.TagContainer but overwritten in js\grape\game\layer.js:184

Gets the instances of one or more class in the current layer (sub-layers not included).

Parameters:

  • classes Class | Array

    Class or classes

  • [descendants=false] Boolean optional

    Get the descendants of that class or just instances of the class itself

Returns:

GameObjectArray:

Instances

getByTag

() Grape.GameObjectArray

Returns the instances with the given tag. Instances are indexed by tags.

Returns:

Grape.GameObjectArray:

Instances with the tag

getGame

() Grape.Game

Returns the current game.

Returns:

Grape.Game:

The game

getScene

() Grape.Scene

Returns the root layer.

Returns:

Grape.Scene:

The root layer

getSystem

(
  • name
)
Grape.System

Returns a system with the given name. Views are also considered as systems.

Parameters:

  • name String

    System name

Returns:

Grape.System:

System

off

(
  • event
  • listener
)

Unsubscribes from an event.

Parameters:

  • event String

    Event

  • listener Function

    Event listener

on

(
  • event
  • listener
)

Subscribes to an event. The event handler will be called with this instance as context.

Parameters:

  • event String

    The event to subscribe

  • listener Function

    Event listener

remove

(
  • instance
)

Removes an instance from the layer and emits it's "remove" event.

Parameters:

  • instance Object

removeLayer

(
  • name
)

Removes a layer.

Parameters:

  • name String | Grape.Layer

    Name of the layer or the layer itself

removeSystem

(
  • system
)

Removes a system from the layer.

Parameters:

  • system String | Grape.System

    The name of the system or the system itself.

removeView

(
  • name
)

Removes a view from the layer. An alias for removeSystem.

Parameters:

  • name String | Grape.View

    View name or the view itself

Events

add (instance)

This event is emitted to the instance when it is added to the layer. The parameter is the layer.

any

Emitted when any event is emitted. The parameters are the event and the payload.

remove (instance)

Emitted to the instance when it is removed from the layer.

start (system)

Emitted to a system when it is added to a running layer or when the layer is started with a system added before.

stop (system)

Emitted to a system when it is removed from the running layer or when the layer is stopped.