Hi all
This is a conceptual question with regards to how to treat the relationships between objects (that is, instances of a custom class) and movieclips.
Oftentimes I run across this problem: how to set things up responsibility wise between my class instances and the mcs they represent.
Say I have 5 cars on stage and each car is an instance of class Car; these
are obviously visually represented by 5 different movie clips. I presume that when time comes to delete the object, it is the object which I should delete, not the movieclip. The deletion of the object should imply the automatic deletion of the movieclip; however, there’s no such thing as a destructor function in AS, leaving me in a quandary.
Right now, for example, I’m making an app that has a wand mc which is the mouse cursor. When the button is held down, the head of teh wand spits out a certain number of sparks every second which will fall to the ground (bottom edge of stage) and slowly burn out. When their alpha reaches zero (burnout), I want both the object and the movie clip to disappear. It would be easier if I put the code on the movieclip to say:
ActionScript Code:
[FONT=Courier New][LEFT]mc.[COLOR=#0000FF]onUnload[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]delete[/COLOR] [COLOR=#0000FF]this[/COLOR].[COLOR=#000080]relatedObj[/COLOR];
[COLOR=#000000]}[/COLOR]
[/LEFT]
[/FONT]
…But I don’t like this solution because it means I have to attach a property to the MC so that I can reference it’s associated object. This is why I say – the object should surely be responsible for everything.
But how? Ugh… Must I have an Array containing all the sparks that currently exist, and iterate through this whole list every frame to then let each object calculate the MC’s alpha? Each spark object (rather than MC) then could take responsibility for its own deletion and that of the MC.
I’m really confused, overall, about how to order my objects under circumstances like these. I definitely want objects to take full control if possible, because then I don’t need to attach properties to movies, or have global variables, for indexing the correct object, as then information is not centralized.
TIA,
-Nick