Re: [Flashcoders] Can an object delete itself?
Mark Burvill wrote: public function die():Void { trace (deleting); removeMovieClip (baddie_mc); delete this; trace (Am i still alive?); } The movieclip is successfully removed, but I would expect the second trace Am I still alive NOT to appear as the object has been deleted, but it does. I read somewhere that it is illegal for an object to delete itself - is this true? Is there another easy way of doing this? I use the delete all the time like you are doing. Flash continues to run your code until the end of the statement block. I thought that once you delete something that garbage collection would take over after the function exits. Someone correct me if I'm wrong. for example, run this in a new fla index = 0; this.onEnterFrame = function () { if (index==10) { delete onEnterFrame; trace(i can still run code but only until the function closes) } trace(index=+index++) } Judah ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Can an object delete itself?
delete something that garbage collection would take over after the function exits. Someone correct me if I'm wrong. Yes, you can do that with Flash methods like onEnterFrame, but not objects. Notice, just because you delete an object like that and it doesn't respond to methods that cause a trace event doesn't mean it's gone. It's just orphaned (has no reference) but it will never get collected. If you want to see an example of this. function loadMyXML() { myXML = new XML(); myXML.onLoad = function() { trace(xml loaded); delete this; } myXML.load(someurl.xml); } BTN_Loader.onRelease = function() { trace(myXML = + myXML.onLoad); loadMyXML(); }; Click the button after each time the xml is loaded. You will see that myXML cannot delete itself because the trace on the button spits out [type Function]. So, let's take it to the next step, and create a for loop that will delete any completed XML objects. this.xmlHolder = {}; // function loadMyXML() { for (var i in this.xmlHolder) { if (this.xmlHolder[i].complete) { delete this.xmlHolder[i]; } } var id = new Date().getTime(); this.xmlHolder[id] = new XML(); this.xmlHolder[id].ignoreWhite = true; this.xmlHolder[id].onLoad = function() { trace(xml loaded); this.complete = true; }; this.xmlHolder[id].load(someurl.xml); } BTN_Loader.onRelease = function() { var n = 0; for (var a in xmlHolder) { n++; } trace(xmlObjs = + n); loadMyXML(); }; When you click the button, you will see 0, and then 1 every time. Looks like it is working, right? The object and the reference are both getting deleted. However, if you continue to do this, you will see your memory usage continue going up slowly over time. Yes, the XML object is getting deleted, but the onLoad function you assigned is being orphaned and thus is not getting cleaned up by the garbage collector. So, you have to explicitly delete the onLoad function of the xml object before you delete the xml object. for (var i in this.xmlHolder) { if (this.xmlHolder[i].complete) { delete this.xmlHolder[i].onLoad; delete this.xmlHolder[i]; } } Now you have no memory waste. It's really only an issue with apps you expect to continue running for quite some time. The onLoads that pile up constitute only a little bit each time, but it does add up. HTH, Steven ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Can an object delete itself?
Mark Burvill wrote: Hi all, I've just started transitioning to AS2 (so go easy on me), and have a question about a game I'm building... I have a Baddie object which handles a baddie on the screen, and when one gets killed, and has finished it's death animation, I'm running a method within the Baddie object called die() which deletes the associated movieClip, and then is meant to delete the baddie Object itself, but it doesn't seem to work public function die():Void { trace (deleting); removeMovieClip (baddie_mc); delete this; trace (Am i still alive?); } The movieclip is successfully removed, but I would expect the second trace Am I still alive NOT to appear as the object has been deleted, but it does. I read somewhere that it is illegal for an object to delete itself - is this true? Is there another easy way of doing this? Thanks! :o) Mark ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No sir, objects do not delete themselves. For my projects i have an external destructor function, for instance: //enemy class class Enemy{ private var creator:Object; private var myClip:MovieClip; public function Enemy(c:Object){ creator = c; } public function death():Void{ playSound(explosion); //for instance removeMovieClip(myClip); } public function destroy():Void{ creator.destroyEnemy(this); } } //whatever deploys the enemies var enemies:Array; function addEnemy(){ enemies.push(new Enemy()); } function destroyEnemy(e:Enemy){ for(var i = enemies.length;i--;){ if(enemies[i]==e){ enemies[i].death(); enemies.splice(i,1); break; } } } Didnt give this much thought so its probably not brilliant, but you get the idea - A ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Can an object delete itself?
Legal or not, it's very unwise to let an object kill itself. Letting an object commit suicide would leave the rest of a program in a lurch as it wouldn't really know where to go from there. You haven't officially returned back to the caller at that point. You are better off sending out an event to a container object that will put you on death row, so you can be properly disposed of when you are finished executing. jord On Dec 21, 2005, at 8:44 AM, Mark Burvill wrote: public function die():Void { trace (deleting); removeMovieClip (baddie_mc); delete this; trace (Am i still alive?); } The movieclip is successfully removed, but I would expect the second trace Am I still alive NOT to appear as the object has been deleted, but it does. I read somewhere that it is illegal for an object to delete itself - is this true? Is there another easy way of doing this? -- Jordan L. Chilcott, President Interactivity Unlimited Guelph, Ontario - Tel: (519) 837-1879 eFax: (253) 276-8631 mailto:[EMAIL PROTECTED] http://www.interactivityunlimited.com iChat/AIM: j1chilcott Author: Building Web Sites with Macromedia Studio MX Author: Building Dynamic Web Sites with Macromedia Studio MX Author: Flash Professional 8: Training From the Source ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Can an object delete itself?
Nice one - thanks Andreas. :o) Andreas Rønning wrote: Mark Burvill wrote: Hi all, I've just started transitioning to AS2 (so go easy on me), and have a question about a game I'm building... I have a Baddie object which handles a baddie on the screen, and when one gets killed, and has finished it's death animation, I'm running a method within the Baddie object called die() which deletes the associated movieClip, and then is meant to delete the baddie Object itself, but it doesn't seem to work public function die():Void { trace (deleting); removeMovieClip (baddie_mc); delete this; trace (Am i still alive?); } The movieclip is successfully removed, but I would expect the second trace Am I still alive NOT to appear as the object has been deleted, but it does. I read somewhere that it is illegal for an object to delete itself - is this true? Is there another easy way of doing this? Thanks! :o) Mark ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No sir, objects do not delete themselves. For my projects i have an external destructor function, for instance: //enemy class class Enemy{ private var creator:Object; private var myClip:MovieClip; public function Enemy(c:Object){ creator = c; } public function death():Void{ playSound(explosion); //for instance removeMovieClip(myClip); } public function destroy():Void{ creator.destroyEnemy(this); } } //whatever deploys the enemies var enemies:Array; function addEnemy(){ enemies.push(new Enemy()); } function destroyEnemy(e:Enemy){ for(var i = enemies.length;i--;){ if(enemies[i]==e){ enemies[i].death(); enemies.splice(i,1); break; } } } Didnt give this much thought so its probably not brilliant, but you get the idea - A ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- *Mark Burvill* Interactive designer www.eyegas.com http://www.eyegas.com *Work:* 0117 953 0100 *Mobile*: 07780 608498 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Can an object delete itself?
maybe I am just thinking out loud (and my mind is a tricky one) but, don't you delete an object's instances and not directly the class itself (how could it delete itself ?) ? like: var baddieInstance = new Baddie(); ... later ... baddieInstance.die(); // internal deletion process (like removeListener ,) delete(baddieInstance); // instance deletion ?? Cedric Nice one - thanks Andreas. :o) Andreas Rønning wrote: Mark Burvill wrote: Hi all, I've just started transitioning to AS2 (so go easy on me), and have a question about a game I'm building... I have a Baddie object which handles a baddie on the screen, and when one gets killed, and has finished it's death animation, I'm running a method within the Baddie object called die() which deletes the associated movieClip, and then is meant to delete the baddie Object itself, but it doesn't seem to work public function die():Void { trace (deleting); removeMovieClip (baddie_mc); delete this; trace (Am i still alive?); } The movieclip is successfully removed, but I would expect the second trace Am I still alive NOT to appear as the object has been deleted, but it does. I read somewhere that it is illegal for an object to delete itself - is this true? Is there another easy way of doing this? Thanks! :o) Mark ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders No sir, objects do not delete themselves. For my projects i have an external destructor function, for instance: //enemy class class Enemy{ private var creator:Object; private var myClip:MovieClip; public function Enemy(c:Object){ creator = c; } public function death():Void{ playSound(explosion); //for instance removeMovieClip(myClip); } public function destroy():Void{ creator.destroyEnemy(this); } } //whatever deploys the enemies var enemies:Array; function addEnemy(){ enemies.push(new Enemy()); } function destroyEnemy(e:Enemy){ for(var i = enemies.length;i--;){ if(enemies[i]==e){ enemies[i].death(); enemies.splice(i,1); break; } } } Didnt give this much thought so its probably not brilliant, but you get the idea - A ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders -- *Mark Burvill* Interactive designer www.eyegas.com http://www.eyegas.com *Work:* 0117 953 0100 *Mobile*: 07780 608498 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Can an object delete itself?
If you're going to constantly be creating enemies, you might want to simple do a check of existing enemies and if an enemy is dead, delete it when you create one. The important thing to note is that an enemy cannot call the destroyEnemy() method of its manager because then it's still the same thread (the method is being called from the object you're trying to delete). It has to happen independantly outside the object. If you want the enemies to manage themselves, it is best to just give them a boolean property called dead (for instance) and then check independantly for dead enemies somewhere else. enemies = {}; function createEnemy() { for (var a in enemies) { if (enemies[a].dead) { for (var b in enemies[a]) { delete enemies[a][b]; } delete enemies[a]; } } // code to create an enemy // enemies[enemyID] = new Baddie(); } enemies[enemyID].dead = true; Or baddie.onKilled = function() { this.dead = true; } Notice that I've gone through and deleted level one props/methods inside the enemy object. If you apply methods to an enemy object after you instantiate it, you should delete those methods before you delete the object or they get orphaned and memory waste can occur. Hence, my delete loop through the enemy object before deleting it. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders