kaboom icon indicating copy to clipboard operation
kaboom copied to clipboard

restoring the states when returning to earlier scene

Open garyhlai opened this issue 4 years ago • 4 comments

Right now when I go from scene A to scene B and then back to scene A, scene A is completely re-initiated. Is there anyway for me to go back to the states scene A was in when I last left it?

garyhlai avatar Apr 21 '21 10:04 garyhlai

It used to be you have to explicitly call a

reload("scene");
go("scene");

to reinitiate and switch scene, then changed go() to call reload() by default cuz it looks like that's a more often use case, we can either

  • go back to having to explicitly call reload("scene")
  • come up with a function name for go() to a scene but not reinitiate

slmjkdbtl avatar Apr 21 '21 16:04 slmjkdbtl

You could also have go(name, reload=True, ...args) although that introduces a breaking change.

Meanwhile, I tried commenting out the reload function and found a bug.

In the level example, if you have a level like this below, where you go to a different scene when you overlap with door "|". When you come back (without reloading), the coins would all fall down and off the screen.

 const map = addLevel(
          [
            "                                                                                                                                                      ",
            "                                                                                                                                                      ",
            "          ooo                  ooo                     ooo                 ooo             ooo                                                        ",
            "     ^+++====+=                ====+=                  ====+=              ====            ====                                                       ",
            "                                                                                                                                                      ",
            "                                                                                                                                                      ",
            "                                                                                                                                                      ",
            "                   |                                                                                                                                  ",
            "               =++===                                                                                                                                 ",
            "                                                                                                                                                      ",
            "                    oo                                                                                                                                ",
            "        =====      +++ **                                                                                                                             ",
            "==========++===+=^====+++==========++===+=^====+++==========++===+=^====+++==========++===+=^====+++==========++===+=^====+++==========++===+=^====+++",
          ],
          {
            width: 11,
            height: 11,
            pos: vec2(0, 0),
            "+": [sprite("steel"), solid()],
            "=": [sprite("grass"), solid()],
            "^": [sprite("jumpy"), solid(), "jumpy"],
            "|": [sprite("door"), "door"],
            "*": [
              sprite("spike"),
              body(),
              area(vec2(0, 6), vec2(11, 11)),
              "hurt",
            ],
            o: [sprite("coin"), body(), "coin"],
          }
        );

Before going to the door

Screen Shot 2021-04-22 at 12 01 52 PM

After coming back. You can see the coins falling down (sorry for the bad cropping - had to take it very quickly since the coins would fall off the screen completely)

Screen Shot 2021-04-22 at 12 02 09 PM

garyhlai avatar Apr 22 '21 04:04 garyhlai

hmm, do you have the full code that I can try?

slmjkdbtl avatar Apr 22 '21 17:04 slmjkdbtl

hmm, do you have the full code that I can try?

sorry I can't find it anymore, but you should be able to reproduce it just from what I have above. Either way, this bug doesn't affect me personally anymore for now - I suppose it's just something to keep in the back of the head as the project moves forward.

Btw, I loooove Kaboom. I really feel like I can get building within 5 min of playing with the APIs (I have no game dev experience, only web dev). Great work!

garyhlai avatar Apr 24 '21 07:04 garyhlai

Closed.

For sending data through scenes can be used:

kaboom()

scene("1", () => {
    let data = {
        life: 300,
    }

    go("2", data)
})

scene("2", (data) => {
    debug.log(data.life)
})

go("1")

And stay() for mantain objects in scene changes

lajbel avatar Oct 04 '22 15:10 lajbel