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
ObjectInitial properties
Item Index
Methods
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:
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
StringEvent
-
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:
FPS
getScreen
()
HTMLElement
Returns the game screen which is appended to the container.
Returns:
Screen element
getScreenHeight
()
Number
Returns the height of the screen.
Returns:
Height
getScreenWidth
()
Number
Returns the width of the screen.
Returns:
Width
off
-
event
-
listener
Unsubscribes from an event.
Parameters:
-
event
StringEvent
-
listener
FunctionEvent listener
on
-
event
-
listener
Subscribes to an event. The event handler will be called with this instance as context.
Parameters:
-
event
StringThe event to subscribe
-
listener
FunctionEvent 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
Stringnew cursor style
start
-
[scene]
Starts the game. Initializes the game screen, the input system, and the game loop.
Parameters:
-
[scene]
Grape.Scene optionalInitial scene, which overrides the initialScene property.
startScene
-
scene
Starts a new scene in the game.
Parameters:
-
scene
SceneThe 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.