Crafty.scene()

Events

SceneChange [Data = { oldScene:String, newScene:String }]
just before a new scene is initialized
SceneDestroy [Data = { newScene:String }]
just before the current scene is destroyed
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

This is equivalent to calling Crafty.defineScene.

public void Crafty.scene(String sceneName[, Data])
sceneName

Name of scene to play

Data

The init function of the scene will be called with this data as its parameter. Can be of any type other than a function.

This is equivalent to calling Crafty.enterScene.

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.

You can optionally specify an arugment that will be passed to the scene's init function.

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

Example

Crafty.defineScene("loading", function() {
    Crafty.background("#000");
    Crafty.e("2D, DOM, Text")
          .attr({ w: 100, h: 20, x: 150, y: 120 })
          .text("Loading")
          .css({ "border": "1px solid red"})
          .textColor("#FFFFFF");
});

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

// An example of an init function which accepts arguments, in this case an object.
Crafty.defineScene("square", function(attributes) {
    Crafty.background("#000");
    Crafty.e("2D, DOM, Color")
          .attr(attributes)
          .color("red");

});

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

Crafty.enterScene("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.enterScene("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.

Crafty.enterScene("square", {x:10, y:10, w:20, h:20});

This will clear the stage, set the background black, and create a red square with the specified position and dimensions. ```