Do you extend or compose Sprite, MovieClip and DisplayObject?
I got such a great response from my last help request that I'm posting another:
On my journey to try and be a better developer, I'm questioning some of the fundamental ways I work and organise code. Like most Flash developers (I think), I normally extend Sprite or MovieClip when I want to control something on the stage. But would I be better off creating a class that composes (i.e. controls via a reference) a Sprite or MovieClip instead? What are the advantages and disadvantages? Which are you doing? Leave comments please!
On my journey to try and be a better developer, I'm questioning some of the fundamental ways I work and organise code. Like most Flash developers (I think), I normally extend Sprite or MovieClip when I want to control something on the stage. But would I be better off creating a class that composes (i.e. controls via a reference) a Sprite or MovieClip instead? What are the advantages and disadvantages? Which are you doing? Leave comments please!
Comments
http://www.moock.org/blog/archives/000248.html
Also it feels like I'm more in control that way, that the display object is part of a whole, rather than being the whole/
Wow, what a great post :S Sorry, tired and the words just aren't coming tonight.
On a related note I try not to use the Library Symbol Properties to link a symbol to a class. IMO it's cleaner to keep the linkage ids in the AS file.
@felix - need to correct you on that thing about classes extending MovieClip being dynamic - not true, unless you explicitly use the dynamic tag. So actually if you compose a MovieClip you've have more dynamic objects in your code.
I use a mix, but a majority of my classes extend Sprite or MovieClip. MovieClip if its art brought in from an Assets.swf and Flash IDE. Sprite if I am dynamically creating it.
I find it easier to not have to re-invent the wheel by creating getter/setter functions for everything. If I want to restrict or customize anything specific then I will override that function.
So basically I combine my controller class with my view in most cases and only use the basic class/object for my data and util classes.
Right or wrong... that is what I do. Thanks for the discussion Iain.
I'm not sure why you'd need a load of getter / setters. Maybe on the controller classes ( Such as a BaddieHandler class that loops through all them bad boys and handles triggering them and houseKeeping ) but when you're in those child classes, the player bullets, the particles, they're kinda self supportive.
For the most part they need an init(), mainloop() and houseKeeping() and that's it.
It is all kinda moot though, I think it boils down to personal preference.
It's like the first Bond you saw usually ends up being your favourite, even if it was Roger Moore.
However, you have to keep in mind that compose instead of extend use more memory and is one more step when you need to acess some property or method of the Sprite. It's a small step, but it's one more thing to slow down your app.
With regards to the display list, when I started doing AS3 I found I was using inheritance far too much.
Over the last year I've found that composition seems to work much better. Why? It's tricky to precisely define, it just seems more logical and cleaner.
Bet that doesn't help at all :)
I feel that I somehow must be missing the good sides of composition, or do something wrong, is there any good blog post about inheritance?
Cheers,
T