API Docs for: 1.0.1

Grape.Game Class

A class that represents a game. You can create and run multiple games in a single page.

Constructor

Grape.Game

(
  • opts
)

Parameters:

  • opts Object

    Initial properties

Methods

createGameLoop

() Grape.GameLoop

This method is called once when the game is created. If you want to use a custom game loop for your game, you can override this method.

Returns:

Grape.GameLoop:

The game loop

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.

frame

()

Called by the game loop in each frame.

getRequiredFps

() Number

This method is called by the game loop to determine what is the required FPS (Frames Per Second) for the game. By default, this is decided by the "fps" property of the current scene.

Returns:

Number:

FPS

getScene

() Grape.Scene

Returns the current scene.

Returns:

getScreen

() HTMLElement

Returns the game screen which is appended to the container.

Returns:

HTMLElement:

Screen element

getScreenHeight

() Number

Returns the height of the screen.

Returns:

Number:

Height

getScreenWidth

() Number

Returns the width of the screen.

Returns:

Number:

Width

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

render

()

Called by the game loop when at least one frame was executed since the last screen update.

setCursor

(
  • cursor
)

Sets the cursor style for the screen.

Parameters:

  • cursor String

    new cursor style

start

(
  • [scene]
)

Starts the game. Initializes the game screen, the input system, and the game loop.

Parameters:

  • [scene] Grape.Scene optional

    Initial scene, which overrides the initialScene property.

startScene

(
  • scene
)

Starts a new scene in the game.

Parameters:

  • scene Scene

    The scene

stop

()

Stops the game.

Properties

container

String | HTMLElement

The dom element which serves as the screen of the game. The size of the container is not manipulated by the engine, therefore you should set the size of it. The engine handles when this "screen size" changes and updates the displayed views. The container can be an id of a html element or a html element itself.

Default: document.body

initialScene

Function

The scene which starts when (re)starting the game. It should be a constructor or a function returning with the scene, not an instantiated scene.

Default: Grape.Scene

Events

any

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

frame

Fired in each frame

frame (scene)

Fired to the actual scene in each frame

renderLayer (scene)

Fired to the current scene in each render frame

start (scene)

Emitted to the scene when started. The parameter is the game.

stop

Fired when the stop() method is called.

stop (scene)

Fired to the scene when the scene is stopped.