The Scene class encapsulates game functionally into stages or levels, providing the required independence in the different parts of the Game: like for example the Intro screen and the Game Level 1.
The Game flow is composed by many Scenes, but only one is active at the same time. In order to control which scene is presented the Director's push and pop scene are required:
# When the Game starts start_game_scene = StartGameScene.new director.push_scene start_game_scene # After the User presses start game_level_scene = GameLevelScene.new director.push_scene game_level_scene # After the User wins win_cut_scene = WinCutScene.new director.push_scene win_cut_scene
The previous example illustrates a typical Game flow, presenting first a Menu Scene, later the Game Scene and finally an Ending Scene.
The scenes can be composed by one or more layers, in order to accomplish this is required to subclass the Scene in the following way:
class GameScene < Joybox::Core::Scene end
It will provide two methods that will be called when the Scene is presented and dismissed:
# Called when the Scene is presented on the screen def on_enter end # Called when the Scene is dismissed from the screen def on_exit end
And finally using the on enter method, the Layers can be initialized and added to the scene:
def on_enter background_layer = BackgroundLayer.new self.add_child background_layer space_ship_layer = SpaceShipLayer.new self.add_child space_ship_layer end
For more information about presenting Scenes, see: Director documentation page.
For more information about Layers, visit: Layer documentation page
Returns a new instance of a Scene class:
scene = Scene.new
Adds a new child to the Scene instance:
The Layers are the recommended classes to be children of a Scene, but it supports also Sprites, Labels, etc.
Push a new child into a Scene instance:
scene << layer
The << and add_child methods are equivalent.
Adds multiple children to the Layer instance:
children = [game_layer, background_layer] scene.add_children children
Runs an action into the Scene instance:
scene.run_action Blink.by times: 10
Stop a Scenes's running action:
blink = Blink.by times: 10 scene.run_action blink scene.stop_action blink
Stop All Actions
Stops all actions running on the Scenes's instance:
Number of Running Actions
Returns the number of actions running on the Scene:
scene.run_action Blink.by times: 10 # The following is 1 scene.number_of_running_actions
Retrieves if the Scene is running any action:
scene.run_action Blink.by times: 10 # The following is true scene.running_actions?
Starts receiving the Game Loop calls on the Scene instance:
schedule_update do |dt| end
dt - Float
The delta time between Game Loop calls.
Do not execute big time consuming operations on the Game Loop, it will affect drastically the performance of the game.