Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

Instantiating an object vs. using a symbol from the Library

Hi, I'm trying to determine what would be a better course of action in terms of performance, whether to create symbols for all my visible objects in my AS3 game project or whether to instantiate them at run-time.

For example, lets say I wanted to add a ball to the stage. Would it be better in terms of performance to use a symbol object from the Library and add it to the stage, or would it be better to programmatically draw the ball, fill it with color, and add it to the stage?

It would be hard to instantiate objects like a space ship or a blast of fire and smoke, which would all be better drawn by creating the actual vector graphics, making it into a symbol and adding them to the Library, but what about primitive shapes like circle, rectangles, etc.

My main question is which of the two methods is more memory and performance effective.

Thanks,
Fulano
Avatar of Antonio Estrada
Antonio Estrada
Flag of Mexico image

Both behave pretty much the same. If you draw a ball and create a new object in the library using the GUI, you have a MovieClip. If you do the same via code (inheriting the MovieClip class but using draw methods) you get a MovieClip that might perform a bit (but just a bit) better than the drawn one.

However... if you use the Sprite class as a base, it should give you a nice performance boost. If it is something as simple as a ball or as some kind of reusable item, you could even go with the Shape class as a base.

Sprite allows user interaction, children and whatnot. The Shape class on the other hand represents raw vector shapes, meaning that you can't get events, timeline, children or the like, but that means a huge boot in performance 'cause they have limited functionality.

Good luck!

-V
Avatar of Mr_Fulano

ASKER

Hi Vulturous, thanks for the tip. Its interesting that a Sprite will perform better than a MovieClip, given that a Sprite is a type of a MovieClip (from my understanding of what a Sprite is.

Thanks for the help!
Fulano
Hello there, in fact it's the other way around.

Shape inheritance:
Object -> EventDispatcher -> DisplayObject -> Shape

Sprite:
Object -> EventDispatcher -> DisplayObject -> InteractiveObject -> DisplayObjectContainer -> Sprite

MovieClip:
Object -> EventDispatcher -> DisplayObject -> InteractiveObject -> DisplayObjectContainer -> Sprite -> MovieClip

Good luck!

-V
As Vulturous mentioned, your performance between the two options (assuming that either way your actual graphics end up being of the same complexity) is pretty much the same either way.

If you're seriously concerned with performa nce issues, I would look instead at your overall rendering scheme: are you moving vectors around on the stage, or can you use cacheAsBitmap? Performance improvements can be huge if you set cacheAsBitmap = true on objects (clips, sprites) that won't be changing very much.

Next, look at your biggest bottlenecks: your loops. Are you destroying and creating a lot of objects where instead you could be re-cycling existing ones? Surprisingly perhaps, instantiating and creating a new object (via new MyThing() ) is very expensive; perhaps you can just hide your object and then bring it back (for example suppose your spaceship can launch a missile, and only one missile can be on the screen at one time - there's a perfect candidate for recycling rather than destruction  instantiation).

Use vectors whenever possible ( var mySpaceshipsList = new Vector.<Spaceships>(); ). Use variables as caches whenever it's going to reduce the number of lookups or operations (for var i=0, j=mySpaceshipList.length; i < j; ++i ) { }  ), and do performance benchmarking using flash.utils.getTimer().

Good luck!

T
Wow...tomaugerdotcom, I need to spend 6 months or a good solid year with you as your apprentice to be able to pick you brain on all these issues. These are all very thought provoking tips and ones I'm certainly going to look into.

I'm concerned about performance, inasmuch as in any game there are multiple platforms. My game may end up as a candidate for a Smart Phone and there resources are far more limited than on a PC. So, I want to learn good habits up front and not take a "I just want to make it work" attitude, but rather try to learn how to make it work in an elegant and streamlined manner.

Thanks for all your help,
Fulano
Hi Vulturous, that is very interesting. I had it backwards. Thank you for the clarification.

Fulano
When I get to the office I will forward some links to various optimization articles you will definitely find interesting
Also you'll want to stay on top of this new development over at adobe labs: http://t.co/2oAr5ooV
ASKER CERTIFIED SOLUTION
Avatar of tomaugerdotcom
tomaugerdotcom
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tomaugerdotcom, very, very cool sites. Thank you very much for sharing!
Excellent!!!