Destroying a Class Object (Flash GURU'S)

I’ve recently worked on creating different classes for movieclips within my library so when i create/attach them they have built in functionality.

My question is simple how do i destroy them so that other objects don’t recognise their existance.

There are two classes involved.

The first is a Ship Registry Class and the Secound the Ship:


//inside the ship init class
ShipRegistry.RegisterShip(this);
 
//inside the ship registry class
static public var aShipRegistry:Array = [];
 
 static public function RegisterShip(obj:Object)
 {
      aShipRegistry.push(obj);
 }

So when the ship is created it is inserted into the registry list,
ok that works just fine…

Then we move onward to remove the object and i can remove it but, it doesn’t recognise that in the registry list.


//////// ------- Inside the Ship
//function called to destroy the ship and all its components
private function getShipDestroy()
 {
  destroy();
  ShipRegistry.getRegistryUpdate();
 }
 private function destroy()
 {
  //delete function "alive"
  delete this.alive;
  //run the function to confirm deletion != deleted
  alive();
  delete this.onPress;
 
  unloadMovie(this);
  removeMovieClip(this);
 
  this = undefined;
  //traces undefined
  trace(this);
 }
 
////////---------- Inside the Registry class
//updates the ship registry to confirm its removal as an object.
static public function getRegistryUpdate()
 {
  var i:Number = new Number();
  for(i=0;i<aShipRegistry.length;i++)
  {
   //traces out all the objects even those set to "undefined"
   trace("Refershing Object List: "+aShipRegistry*);
   if(aShipRegistry*==undefined)
   {
    aShipRegistry.splice(i,1);
   }
  }
 }

Sure the moviclip is removed but, two concerns… are my objects getting deleted? is the memory being freed up? Is it just an empty referance?

How do i delete functions and prevent them from being called?

any help would be greatly appreciated.

~BNNS

Hmm, I see what you’re issue is here… and there are a number of ways to handle this. The great thing is, you’re concerned about your memory usage.

To track memory usage, you can view the activity monitor on your computer - Activity Monitor on the Mac - not sure what its called on the PC. But, you can run your app, for a few hours, going to and from the computer interacting with the app. If the memory usage continues to go up - then something isn’t cleared.

AS3 actually has some really neat ways of tracking system memory, but the flash 9 player with AS3 takes a lot more monitoring of this situation - in AS3 if you delete an object, but you have stored references (in an array) - you’re never sure if/when the garbage collector will delete them. There are actually bugs in the current player that have caused quite a stir on this scenario.

I digress - in AS2, as I understand it, if you delete an object (an MC or Class etc.) Then the references shouldn’t hog the same amount of memory as when the object was being rendered… but you still want to get that object reference out of your array, whether its undefined or not.

some ideas/suggestions - others hopefully will chime in too:

  1. even though you remove your MC - and set class=undefined, its not going to get delete from the array - the array will simply show undefined for that index.

  2. its almost the same idea as using an Observer pattern (might look into that, it mirrors some of the ideas you’re using) But, when you destroy() your clip - one way to clear the object from the array is to fire a “ShipRegistry.deRegisterShip(this)” - then, create a loop in the shipregistery class that will search for a match (==) in the array and then splice it from the array. So, you remove reference from the array, then you continue with your destroy() items within the ship class itself. I don’t believe its possible to have this “automatically” happen.

  3. Have you place with the EventDispatcher class? Where you can listen and dispatch from your individual Ship classes? This is another way to handle this. Where the ship register (the observer) listens to the ship classes… then the ship classes just need to dispatch and event (I’m destroying myself) and whomever is listening will manage what needs to be done - removal of that item from the array. This more loosely couples your ships and your ship registery. But the bigger advantage is that you can have other classes listen to the ships - so numerous items notified if/when a ship is destroyed.

Hope all this helps.

thnx creatify,

you gave a very detailed explanation in your answer and i sincerely appreciate that. You mentioned a possibility of setting the ship’s class to “undefined”. how would i go about doing that?

I have been playing around with the notion of calling ShipRegistry and unregistering the class which is probably the best way to go, but just to be on the safe side i’d like to switch the class to undefined state as well.

I still have to look at event dispatchment as an option, it might prove to be invaluable.

I really appreciate all the help you have provided thus far and any additional help would be much appreciated.

~BNNS