"Reset" World on Click

Post about the Flash World Construction Kit and the Alchemy port of Box2D.
zuhairm
Posts: 3
Joined: Sat Feb 12, 2011 6:26 pm

"Reset" World on Click

Postby zuhairm » Sat Feb 12, 2011 6:29 pm

Hi there :)

I wanted to ask whether there is a way to manually reset a world made with WCK, maybe by pressing a button? I don't think myworld.reset(); or anything like that exists. The only possibility I can see is going to a different frame on the timeline, and then back to the previous one again.

Thanks,
Zuhair

greymoth
Posts: 12
Joined: Thu Mar 19, 2009 8:05 pm

Re: "Reset" World on Click

Postby greymoth » Sun Feb 13, 2011 6:05 pm

HI. I've just been solving a related problem, which is how to set up a system for loading, resetting, and replacing levels.

It looks like if you contain your level / scene objects inside a MovieClip, you can just remove it from the stage and then add a new instance of the same clip.

To make this clearer, this is the structure you might use:

Code: Select all

wck.WCK clip (either as your Document class or some inner Clip, placed on the stage.)
   wck.World clip (This can contain objects like the ground, which you want to stay there all the time.)
      level1 clip - a clip containing all the wck objects you want to be able to remove and / or reset.


if you 'Export for Actionscript' resettableObjects, eg with the class name "Level1", then you can remove it from your world, and re-add a reset version of it with code something like this:

Code: Select all

worldInstance.removeChild( level1 );
level1 = new Level1();
worldInstance.addChild( level1  )


I've only just started using wck, but from what I can tell, removing objects from the stage removes them from the b2World as well, and takes care of destroying the Box2D objects. I'll be profiling this for memory leaks, so if I turn out to be wrong, I'll get back to you.

BTW, wck.World contains a reference to the Box2D b2World instance. It's b2world ( with a lowercase 'w' ). You can use this to get at the Box2d Objects directly. For example, this code will list all the objects in the world:

Code: Select all

   var w:wck.World = wckMain.world; //this would be your root level 'Document' class, if you have followed the tutorial, or are using the default template.

   var b2w:b2World = wckMain.world.b2world;

   var node:b2Body = b2w.GetBodyList();
   while (node)
   {
       var b:b2Body = node;
       node = node.GetNext();
      trace(node);
   }

sketchbookgames
Posts: 564
Joined: Tue Feb 24, 2009 4:10 pm
Location: Michigan
Contact:

Re: "Reset" World on Click

Postby sketchbookgames » Sun Feb 13, 2011 9:48 pm

i make each level a world (it extends wck.World)
i leave an empty frame (usually frame 1, so its predictable)

then something like this will do.

Code: Select all

var currentLevel:int = currentFrame;
gotoAndStop(1);
gotoAndStop(currentLevel);

Squishy
Posts: 8
Joined: Tue Jun 14, 2011 5:02 pm

Re: "Reset" World on Click

Postby Squishy » Tue Jul 26, 2011 6:47 pm

How do you have multiple worlds? I errors if I have more than one.

mayobutter
Posts: 915
Joined: Fri Dec 14, 2007 8:07 pm
Contact:

Re: "Reset" World on Click

Postby mayobutter » Tue Aug 09, 2011 9:47 am

Well what are the errors? :)

zuhairm
Posts: 3
Joined: Sat Feb 12, 2011 6:26 pm

Re: "Reset" World on Click

Postby zuhairm » Fri Aug 26, 2011 3:16 pm

Hi guys,

Sorry about this ridiculously late reply - been very busy with things.

Good news is the game for which I needed the original info from this topic has been completed. To have multiple worlds I just had one movieclip, a world container if you will, that held about 13 frames - each with one world in them.

As for my original question as to how I can "reset" a world on click, I just used the simple yet effective code provided by sketchbookgames:

i make each level a world (it extends wck.World)
i leave an empty frame (usually frame 1, so its predictable)

then something like this will do.

Code:
var currentLevel:int = currentFrame;
gotoAndStop(1);
gotoAndStop(currentLevel);


I'd like to mention that I used a different method in my Android game (built with Adobe Air + WCK) for reseting the world. One very similar to that which greymoth suggested:

if you 'Export for Actionscript' resettableObjects, eg with the class name "Level1", then you can remove it from your world, and re-add a reset version of it with code something like this:

Code:
worldInstance.removeChild( level1 );
level1 = new Level1();
worldInstance.addChild( level1 )


Greymoth, your ideas on the structure of a level are interesting, I'll have to try them out.

When my game has been released I'll make a post or something :)

Best,
Zuhair


Return to “WCK”



Who is online

Users browsing this forum: No registered users and 1 guest