Solved

Instantiating an object vs. using a symbol from the Library

Posted on 2011-09-25
11
334 Views
Last Modified: 2013-11-11
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
0
Comment
Question by:Mr_Fulano
  • 5
  • 4
  • 2
11 Comments
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 36597090
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
0
 

Author Comment

by:Mr_Fulano
ID: 36597203
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
0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 36597240
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
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 36598940
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
0
 

Author Comment

by:Mr_Fulano
ID: 36699822
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
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Mr_Fulano
ID: 36699881
Hi Vulturous, that is very interesting. I had it backwards. Thank you for the clarification.

Fulano
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 36708832
When I get to the office I will forward some links to various optimization articles you will definitely find interesting
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 36708985
Also you'll want to stay on top of this new development over at adobe labs: http://t.co/2oAr5ooV
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 500 total points
ID: 36710537
0
 

Author Comment

by:Mr_Fulano
ID: 36801926
Tomaugerdotcom, very, very cool sites. Thank you very much for sharing!
0
 

Author Closing Comment

by:Mr_Fulano
ID: 36802140
Excellent!!!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now