craftyjs.github.com

Crafty.scene

Events

SceneChange [Data: { oldScene:String, newScene:String }]
when a scene is played

public void Crafty.scene(String sceneName, Function init[, Function uninit])

sceneName
Name of the scene to add
init
Function to execute when scene is played
uninit
Function to execute before next scene is played, after entities with 2D are destroyed

public void Crafty.scene(String sceneName)

sceneName
Name of scene to play

Method to create scenes on the stage. Pass an ID and function to register a scene.

To play a scene, just pass the ID. When a scene is played, all previously-created entities with the 2D component are destroyed. The viewport is also reset.

If you want some entities to persist over scenes (as in, not be destroyed) simply add the component Persist.

Example

Crafty.scene("loading", function() {
    Crafty.background("#000");
    Crafty.e("2D, DOM, Text")
          .attr({ w: 100, h: 20, x: 150, y: 120 })
          .text("Loading")
          .css({ "text-align": "center"})
          .textColor("#FFFFFF");
});

Crafty.scene("UFO_dance",
             function() {Crafty.background("#444"); Crafty.e("UFO");},
             function() {...send message to server...});

This defines (but does not play) two scenes as discussed below.

Crafty.scene("loading");

This command will clear the stage by destroying all 2D entities (except those with the Persist component). Then it will set the background to black and display the text "Loading".

Crafty.scene("UFO_dance");

This command will clear the stage by destroying all 2D entities (except those with the Persist component). Then it will set the background to gray and create a UFO entity. Finally, the next time the game encounters another command of the form Crafty.scene(scene_name) (if ever), then the game will send a message to the server.