views:

68

answers:

1

I'm always tempted to extend the Sprite class just so my object can be part of the display list (and also have EventDispatcher functions), even though it has nothing to display. It will, however, contain things to be displayed. It's convenient because those contained objects need only reference their container for display list access.

Has anyone run into this temptation?
It seems like it would be common, but also seems inappropriate.

Is there a significant memory cost to needlessly extending the Sprite class?

A: 

Why do you want something to be part of the display list? You say: It will, however, contain things to be displayed.

If you mean you want to compose displayable objects, e.g. doing something like:

var container:Sprite = new Sprite();
var image:Sprite = new some_lib_image();
var image2:Sprite = new some_other_lib_image();
container.addChild(image);
container.addChild(image2);
stage.addChild(container);

Then that is totally acceptable. If you want to create a class called ImageContainer that manages the adding and removal of images and use it instead of Sprite - that too is totally acceptable. I wouldn't call it a temptation but I wouldn't do it unless you were adding something worthwhile to the above code.

Is there a significant memory cost to needlessly extending the Sprite class?

Not particularly. The size of your class will generally be something close to sizeof(Sprite) + sizeof(instanceVariables[]) where instanceVariables[] are the new variables you declare in your class. I wouldn't worry about it. Composition is more costly than inheritance; that is needlessly using too many containers. In the general case I never worry about it but if I were to create 5000 particles in a particle system I'd try and keep each particle as simple as possible.

James Fassett