Box2D Forums

It is currently Sun May 19, 2013 9:50 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: "Reset" World on Click
PostPosted: Sat Feb 12, 2011 6:29 pm 
Offline

Joined: Sat Feb 12, 2011 6:26 pm
Posts: 3
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


Top
 Profile  
 
PostPosted: Sun Feb 13, 2011 6:05 pm 
Offline

Joined: Thu Mar 19, 2009 8:05 pm
Posts: 12
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:
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:
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:
   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);
   }


Top
 Profile  
 
PostPosted: Sun Feb 13, 2011 9:48 pm 
Offline

Joined: Tue Feb 24, 2009 4:10 pm
Posts: 564
Location: Michigan
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);


Top
 Profile  
 
PostPosted: Tue Jul 26, 2011 6:47 pm 
Offline

Joined: Tue Jun 14, 2011 5:02 pm
Posts: 8
How do you have multiple worlds? I errors if I have more than one.


Top
 Profile  
 
PostPosted: Tue Aug 09, 2011 9:47 am 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
Well what are the errors? :)


Top
 Profile  
 
PostPosted: Fri Aug 26, 2011 3:16 pm 
Offline

Joined: Sat Feb 12, 2011 6:26 pm
Posts: 3
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:

Quote:
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:

Quote:
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group