How crafty works with multiple scenes?

87 views
Skip to first unread message

Deep R. Ode

unread,
May 17, 2013, 4:27:54 AM5/17/13
to craf...@googlegroups.com
If I have many scenes, for say 25, would crafty load them all at initialization or crafty will load them when scenes are called...? 

Steve Byrnes

unread,
May 17, 2013, 8:11:11 AM5/17/13
to craf...@googlegroups.com
The second one. It doesn't do anything in advance. The code is
actually really simple

https://github.com/craftyjs/Crafty/blob/develop/src/drawing.js#L255

When you call Crafty.scene("scene A", function_A), it stores the
reference to function_A, filed internally under the name "scene A".
Then when (or if) you call Crafty.scene("scene A"), it destroys all
the 2D entities (unless they have Persist), then resets the viewport,
and then runs function_A(). If you never call Crafty.scene("scene A"),
then function_A() is never run.

--Steve

On Fri, May 17, 2013 at 4:27 AM, Deep R. Ode <ashde...@gmail.com> wrote:
> If I have many scenes, for say 25, would crafty load them all at
> initialization or crafty will load them when scenes are called...?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Crafty" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to craftyjs+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

曹磊

unread,
Jun 9, 2013, 10:11:29 AM6/9/13
to craf...@googlegroups.com
Hi,Steve
I have another question about the scene.

if I defined another scene ,for example:
Crafty.scene("scene B", function_B)
and the "scene B" will be called if something be triggered in "scene A",for example:
var function_A = function(){
    ...
    if(something happened){
        Crafty.scene("scene B");
    }

}
My question is that when the Crafty.scene("scene B") was called, it will run,but the Crafty.scene("scene A") seems like unable to return. if this is true, when called repeatedly ,it may result in the stack overflow.

在 2013年5月17日星期五UTC+8下午8时11分11秒,Steve写道:

Steve Byrnes

unread,
Jun 9, 2013, 8:53:07 PM6/9/13
to craf...@googlegroups.com
I agree! If you have a scene that calls another scene, over and over
again, it will grow the call stack more and more, just like you said.
To avoid that, you can do

Crafty.scene("scene A", function () {
// do stuff
setTimeout(function () {Crafty.scene("scene B")}, 0) //
asynchronous call to run the next scene
return;
});

I think that would work, and avoid both memory leaks and growing call
stack. But I'm not 100% sure, someone can correct me if I'm wrong.

Hmm, I have an idea: We could change the Crafty source code so that you can do:

Crafty.scene("scene A", function () {
// do stuff
return "scene B";
});

The Crafty.scene() would interpret a return-value with string type as
being another scene call. Wouldn't that be an improvement?

Steve


On Sun, Jun 9, 2013 at 10:11 AM, 曹磊 <jump...@gmail.com> wrote:
> Hi,Steve
> I have another question about the scene.
>
> if I defined another scene ,for example:
> Crafty.scene("scene B", function_B)
> and the "scene B" will be called if something be triggered in "scene A",for
> example:
> var function_A = function(){
> ...
> if(something happened){
> Crafty.scene("scene B");
> }
>
> }
> My question is that when the Crafty.scene("scene B") was called, it will
> run,but the Crafty.scene("scene A") seems like unable to return. if this is
> true, when called repeatedly ,it may result in the stack overflow.
>
> 在 2013年5月17日星期五UTC+8下午8时11分11秒,Steve写道:

~~
Steven Byrnes
Postdoctoral Fellow, Applied Physics
Harvard University School of Engineering and Applied Sciences
http://sjbyrnes.com/
Reply all
Reply to author
Forward
0 new messages