Crafty.scene()

事件

SceneChange [Data = { oldScene:String, newScene:String }]
新场景初始化之前触发
SceneDestroy [Data = { newScene:String }]
当前场景销毁之前触发
public void Crafty.scene(String sceneName, Function init[, Function uninit])
sceneName

场景名称

init

场景播放时要执行的函数

uninit

下一个场景播放前被执行, 在 2D 实体销毁之后。

等价于调用Crafty.defineScene.

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

播放场景的名称

Data

初始化场景的参数,可以除函数外的任何类型。

等价于调用Crafty.enterScene

在舞台上制作场景的方法是,通过ID或者函数注册一个场景。

播放场景,只需要通过ID就可以。当场景被播放时,之前创建的所有2D 组件都会被销毁,视口也会被重置。

你可以指定一个参数传递给场景的初始化方法。

如果你希望实体在场景中持久化(即:不被销毁),你只需添加组件Persist

例子

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");

});

这里定义(不是播放)了两个场景,接下来将会讨论。

Crafty.enterScene("loading");

这个命令将会清除舞台上的所有 2D 实体 (除了添加了 Persist 组件实体)。然后会将背景设置为黑色并且显示“Loading”。

Crafty.enterScene("UFO_dance");

这条命令会清除舞台,销毁所有 2D 实体 (除了添加了 Persist 组件的实体)。然后设置背景为灰色并且创建一个 UFO 实体。最后, 通过命令 Crafty.scene(scene_name) 进入下一场景(如果有), 然后游戏将会发消息给服务器。

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

这会清除舞台,设置背景为黑色,然后创建一个指定位置和大小的红色方块。