Skip to main content
Known Participant
March 7, 2011
Question

Removing an object, removes its instances objects and events?

  • March 7, 2011
  • 2 replies
  • 600 views

Hi

I´m creating a game.

When the current game has finished, I thinking to do this:

removeChild(game)

game = null

then

game = new Game()

Doing this way, it automatically removes the game´s object instances?

It automatically removes the game´s object instances events?

That would be an easy way to restart the game, if yes for both questions.

Can I do like that, or I have to remove all objects and events manually?

Thanks

This topic has been closed for replies.

2 replies

Fl_as_he_ro
Participant
March 7, 2011

try this! Gamer lol!

package
{
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.text.TextField;

     public class NewClass extends Sprite
     {
          private var game:Sprite;
          public function NewClass()
          {
               game = new Sprite();
               //getProperties();
               madebtn();
          }
          private function madebtn()
          {
               var btn01:Sprite =  new Sprite();
               var btn02:Sprite =  new Sprite();
               lowBtnProperties(btn01,0x0000FF,100,100,"Boton-1");
               lowBtnProperties(btn02,0x00FF00,0,100,"Boton-2");
          }
          private function lowBtnProperties(btn:Sprite,color:uint,posX:int,posY:int,txtname:String)
          {
               var texto:TextField = new TextField();
               btn.graphics.beginFill(color);
               btn.graphics.drawRect(0, 0, 100, 30);
               btn.buttonMode = true;
               btn.useHandCursor = true;
               addChild(btn);
               btn.addChild(texto);
               texto.height = 30;
               texto.width = 100;
               texto.selectable = false;
               texto.text = txtname;
               btn.x = (stage.stageWidth /2 )- posX;
               btn.y = (stage.stageHeight/2)-posY;
               btn.addEventListener(MouseEvent.CLICK, getActions);
          }
          private function getProperties()
          {
               game.buttonMode = true;
               game.useHandCursor = true;
               game.graphics.beginFill(0xFF0000);
               game.graphics.drawRect(0, 0, 100, 80);

               game.x = (stage.stageWidth /2 )- 50;
               game.y = (stage.stageHeight/2)-50;
               game.addEventListener(MouseEvent.CLICK, getActions);
               addChild(game);

          }
          private function getActions(e:MouseEvent)
          {
               //trace(e.target.name);
               switch (e.target.name)
               {
                    case ("instance5") :
                         getProperties();
                         trace("Assign object  properties");
                         break;
                    case ("instance6") :
                         game.removeEventListener(MouseEvent.CLICK, getActions);
                         trace("RemoveEventListener" + game);
                         //game = null;
                         //trace("object = null" + game);
                         break;
                    case ("instance2") :
                         trace("Object and Instance  still in memory: PosX->"+ e.target.x+" PosY->"+ e.target.y );
                         break;
               }
          }
     }

}

Known Participant
March 8, 2011

How do you post code like that?

Which tag I have to use to post code like that?

Fl_as_he_ro
Participant
March 7, 2011

I thinking yes, But I'm not sure.. You can see it, with monsterdebugger... it's an air app to test your game with all objets, functions, variables, etc inside your project...in real time, so you can edit them, and try it!

http://demonsterdebugger.com/

In my opinion, when you get : game=null the object has no references, and the garbage recolector is begin to work!

But well, share your results with us! lol

Regards!

Known Participant
March 8, 2011

There´s any need to set to null a reference in this case?

private function initGame():void{

var game:Game = new Game()

}

private function endGame():void{

removeChild(game)

//here, should I set game to null?

}

then, to start a new game:

initGame()

In this case, do I need to set game to null? Or by adding a new game instance to game variable, the previous instance has no more reference and so is eligible for garbage collection?

Thanks