0

In AS3, I go to a scene like this: gotoAndStop(frame, scene), but when I go to the same scene later on I want all the changes I made when I went there first to be undone. How can I do this? I thought I could maybe open a copy of the scene, but how should I do that?

Edit I guess that wasn't really clear, so I'll try to explain it some more. Basically, the scene contains symbols which are exported for Actionscript, and everytime the scene is played, I want the code for that symbol to run. This is the class which is linked to those symbols:

package level{

    public class Block(){

        public function Block(){
            Mission.blockArray.push(this); //Adds itself to a static array
        }

    }

}

So, everytime the scene is loaded, I want every block to be added to the array. How should I do this?

Jonan
  • 2,485
  • 3
  • 24
  • 42
  • Are you saying the scene's frames have code attached to control those symbols? Why not just cut that code and put into your document class, in a function that would be called when needed after you `addChild` those symbols? Also see this: http://stackoverflow.com/questions/24271396/as3-refresh-a-function/24278363#24278363 – VC.One Jul 06 '14 at 20:01
  • In the link I posted it's the last function at bottom that I wanted you to see and consider. `function reset_Board():void`. Also is your code all in the timeline or do you have a class file attached to the FLA to control everything from that one central place? If not I suggest you do that. It's like having all your code on Frame(1) but it all works effortless no matter what frames/scenes you jump to. – VC.One Jul 06 '14 at 20:31
  • @VC.One yeah, I've got a document class. The symbols are exported for Actionscript and the above code is their class. What if, in the document class, I had this function: `private function _destroy(object:*){ for(var i:String in object){ _destroy(object[i]); object[i] = null; } }` would that be sufficient for garbage collecting? – Jonan Jul 07 '14 at 13:41
  • Don't worry about garbage collecting just yet.. when the Ram/CPU is lagged by your test code then you consider optimising. About this line `Mission.blockArray.push(this);` is Mission another class? Just to understand... In your main class (Main.as?) did you make an instance of Block? like `var myBlock:Block = new Block();` and also create a blockArray? `blockArray:Array = new Array;` Then you can do `blockArray.push(myBlock);` all inside Main.as. To run a function in Block class would be `myBlock.name_of_Function ();` but really code in Block class is not necessary in this case I dont think.. – VC.One Jul 07 '14 at 17:50
  • @VC.One the mission class is a different class, and I can't access the blocks because they're already on the stage, so if the scene is played, every block is added to the `blockArray` so I *can* access them – Jonan Jul 07 '14 at 19:06

2 Answers2

2

Try using gotoAndPlay(frame, scene) instead, and go to the target scene, at the beginning of the code, add the function stop();. That's how I do it, and it resets. Good luck with your program!

MNOPYZ
  • 55
  • 1
  • 2
  • 13
  • 1
    I dont think his code is on the timeline so he can't just jump to "earlier" code. Also your way of resetting might work now but its not a true reset that cleans up after itself (memory-wise etc) so one day as your code gets more complex over many frames there will be issues (i.e crashes). – VC.One Jul 06 '14 at 19:49
  • @VC.One have you got a suggestion how I can do it without crashes? – Jonan Jul 06 '14 at 19:58
  • @Jonan By not jumping to an earlier frame and assuming that you have a reset. Consider Frame(1) = Start game menu and Frame(2) = lots of enemies spawned and a countdown timer takes you back to Frame(1) when play time is over.. Now it looks like you have a "reset" but those spawned guys are in memory eating it up plus now you will make more when you play frame (2) again (also depends on how you code but just be careful & aware of what everything is doing where). – VC.One Jul 06 '14 at 20:18
  • There is no easy one suggestion because every project is different but look up AS3 garbage collection for starters and also this: http://stackoverflow.com/questions/8380789/what-are-the-major-performance-hitters-in-as3-aside-from-rendering-vectors – VC.One Jul 06 '14 at 20:21
  • @VC.One My program's very simple, just a bunch of menus and a shuffling function, so `gotoAndPlay` works fine for me. But you're right, using it in a complex program isn't a very good idea. Any ideas on resetting without the memory overusage? – MNOPYZ Jul 07 '14 at 11:52
1

I found out that gotoAndStop() works perfectly; the error was somewhere else in my code where I did this:

while (numChildren){
    removeChildAt(0);
}

without changing to another scene first. Thanks to everyone who tried to help me!

Jonan
  • 2,485
  • 3
  • 24
  • 42
  • Glad you got some progress at last. I gotta say though from the Title, Question content and Shown code no one would have guessed that one for ya. I'll give @MNOPYZ a big-up cos he was closer to the truth. Peace – VC.One Jul 07 '14 at 19:32