Skip to content
Franklin Webber edited this page Apr 27, 2013 · 3 revisions

Scene Transitions

Overview

Scenes can transition from one scene to another scene or even itself. A scene defines a helper method transition_to that takes a scene instance or a the name of a scene.

Traditional Transitions

This is an example of moving to the title scene when the escape key is pressed:

class PreTitleScene < GameScene
  draws :title, :logo, :galaxy, :background_space1, :background_space2

  event :on_up, KbEscape do
    transition_to :title
  end
end

class TitleScene < GameScene

end

Transition Events

After a scene transition has been initiated the original scene, the scene being transitioned away from, receives the message prepare_transition_to. The new scene, the scene that is being transitioned to, receives the message prepare_transition_from.

class PreTitleScene < GameScene
  draws :title, :logo, :galaxy, :background_space1, :background_space2

  event :on_up, KbEscape do
    transition_to :title
  end

  def prepare_transition_to(new_scene)
    puts "Leaving this scene to go to #{new_scene}"
  end
end

class TitleScene < GameScene

  def prepare_transition_from(old_scene)
    puts "Coming to this scene from #{old_scene}"
  end

end

Transferring actors from a previous scene

Actors defined in a previous scene can be directly transferred to a new scene. This makes it possible to not have to redefine the previously defined properties.

This is an example of moving to the title scene where the title, logo, and galaxy:

class PreTitleScene < GameScene
  draws :title, :logo, :galaxy

  event :on_up, KbEscape do
    transition_to :title
  end
end

class TitleScene < GameScene
  draw :title, model: 'metro::ui::label', from: :previous_scene
  draw :logo, model: 'metro::ui::image', from: :previous_scene
  draw :galaxy, model: 'metro::ui::image', from: :previous_scene
end

Fade Transition

A transition can automatically be faded from the current scene to the next scene. A fade transition by default draws a white scene that transitions to a black scene over the specified interval.

NOTE: This transition is not very subtle when the current scene is not white. For the best result, it is best to transition the scene manually.

class PreTitleScene < GameScene
  draws :title, :logo, :galaxy, :background_space1, :background_space2

  event :on_up, KbEscape do
    transition_to :title, with: :fade, interval: 30.ticks
  end
end

Moving into Edit Mode

Metro provides the ability to edit scenes. This functionality does not work with all scenes. Transitioning a scene to edit mode allows you to move around visible UI elements (e.g. label, image, and menus).

This is an example of providing edit support to all scenes when the keyboard button E is pressed:

class GameScene

  event :on_up, KbE do |event|
    transition_to scene_name, with: :edit
  end

end
Clone this wiki locally