Skip to content
Franklin Webber edited this page Nov 24, 2012 · 12 revisions

Scene

A scene is one of three major concepts in Metro. A scene represents screens, transitions, or stages within the game. A Scene itself coordinates the many actors, events, animations, and actions that are currently on screen.

Overview

A Scene is similar to a Rails Controller.

Scenes are stored in the 'scenes' directory. The template game that Metro generates provides you with several scenes which are subclasses of GameScene. GameScene is a subclass of Metro::Scene. All scenes within Metro must have Metro::Scene as an ancestor.

This is again very similar to how Rails will generate a controller named ApplicationController and all new controllers will be subclasses of it.

An important suggestion is to use the inheritance with care. It is often times much easier to manage inheritance that is wide and shallow (e.g. Lots of subclasses of GameScene) instead of deep.

Creating a Scene

Scenes are not required to reside in the scenes directory. However, it is strongly encouraged to maintain the organization of your code.

Many scenes may also share a single file. However, it is strongly encouraged that you maintain one scene per file.

Manual Creation

A new scene can be created manually simply by saving ruby code that defines a scene to a file.

class DungeonScene < GameScene

end
  • Scene classes often use a common naming convention. Selecting a unique name as a prefix and the word Scene as a suffix. In the above example the word 'Dungeon' is the unique name.

  • The name of the file often matches the scene class name. Except the file name is usually a snake cased version of the CamelCased class name. This is a common convention within Ruby.

Generator

The metro command-line tool provides a generator to help create a new scene that abides by the conventions defined above.

$ metro generate scene new DungeonScene
$ metro generate scene new dungeon_scene
$ metro generate scene new dungeon

Any of the above specified commands will generate the a file at the path scenes/dungeon_scene.rb which contains a class named DungeonScene.

Scene Name

Each scene has a name. By default the name of a scene is a snaked cased version of the unique name prefix of the scene class.

The scene name of the the previously example is dungeon:

class FirstMazeScene < GameScene ; end

The following scene name would first_maze.

Scene names are used throughout your game as references to scenes to prevent the spread of scene classes spread all throughout the code.

  • The first scene of your game that is loaded, specified in your game configuration, uses the name of the scene.

  • The scene name, by default, determines the default view file that it uses.

  • Scene Transitions take the scene name as a parameter.

The name of a scene can be overridden. A custom scene name can be explicitly defined.

The following example defines a custom scene name, which overrides the usual default:

class FirstMazeScene < GameScene
  scene_name :first_labyrinth
end

Methods and Events

Defining Models and Actors

Using a View

Defining Sound

Events

Animations

Removing Models

Transitioning To New Scenes

Preparing for Transition (To and From)

Clone this wiki locally