AS 3 Dynamic Creation of Display Objects From Library

Hi -

I've got a FLA file here with about 250 bitmaps inside the library.  I've added them all as actionscript classes - with names Class01, Class02, Class03, etc...

I'd like my script to dynamically pull them in...but I'm missing a part of the syntax.  Here's what I have.

var t:Number = 05;
var y:DisplayObject = "Class"+t;
var myBitmapDataObject:y = new y(100, 100);
var myImage:Bitmap = new Bitmap(myBitmapDataObject);
addChild(myImage);


How would you do that?  Clearly I'm missing something.

Thanks!
LVL 2
erzoolanderAsked:
Who is Participating?
 
erzoolanderAuthor Commented:
My question was how to get it displayed on the screen - and my "closing" of the question wasn't meant as any kind of slight - but rather because no answer prior to the closing actually resolved that question.

While it was a "key" in the eventual solution - I have a hard time accepting that it was *the* key element to solving the puzzle.  Rather - the solution was:

#1 - use your key to identify the element and treat it as a class (not as a display object as suggested)
#2 - Take the bitmap data (as the created class) and apply it to the overall Bitmap class.
#3 - Add the item generated in step #2.

symbolClass = getDefinitionByName(symbolName) as Class;
s=new symbolClass();
myImage = new Bitmap(s);
addChild(myImage);

If you want the 500 points, I don't care, I wasn't intending on stepping on any toes.  My closing was not meant as a slight - but rather because what was presented was not the solution.  Rather - I felt it comprised about 30% or so to the overall process of finding the working solution.

So - let whomever judges this appeal be the arbiter.
0
 
CyanBlueCommented:
Try using getDefinitionByName() function to reference them.

CyanBlue
var y:DisplayObject = getDefinitionByName("Class"+t) as DisplayObject;

Open in new window

0
 
erzoolanderAuthor Commented:
Here's the entire script...and the resulting error(s)

import flash.utils.getDefinitionByName;
import flash.display.DisplayObject;

var t:Number = 91;
var myy:DisplayObject = getDefinitionByName("Class"+t) as DisplayObject;
var myBitmapDataObject:myy = new myy(100, 100);
var myImage:Bitmap = new Bitmap(myBitmapDataObject);
addChild(myImage);

Open in new window


Type was not found or a compile time constant myy (line 6)
Call to possibly undefined method myy (line 6)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
CyanBlueCommented:
What did you mean by this: new myy(100, 100) and what do you do to get that to work as you have intended?

CyanBlue
0
 
erzoolanderAuthor Commented:
Turned out that because the as class is derived from bitmapdata and it's being displayed as a bitmap - the script needed to look like this... (I'm putting in a dynamic example of "95" as a value - with the corresponding AS Class in the library of course named "Class95"

var thisis:String;
var symbolClass:Class;
var s:BitmapData;
var myImage:Bitmap;

thisis=95;

symbolName = "Class" + thisis;
symbolClass = getDefinitionByName(symbolName) as Class;
s=new symbolClass();
myImage = new Bitmap(s);
addChild(myImage);

Open in new window


I guess since the flash.display.bitmap is comprised of bitmapdata - it needed to have both accounted for.
0
 
erzoolanderAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for erzoolander's comment #a38296181

for the following reason:

Because that's the solution that worked.
0
 
CyanBlueCommented:
Your original code was missing the essential piece that is getDefinitionByName() call.  Did your solution work without that call?  If it is the case, you can close this, but I will have to object otherwise.

CyanBlue
0
 
CyanBlueCommented:
As I have stated, the key element that he needed to solve the puzzle was provided in the my first comment(http:#a38288495).

CyanBlue
0
 
CyanBlueCommented:
No, I don't really care about the points.  I don't work for EE. I volunteer my free time to help others and the points won't help me buy a stick of gum.

I had to object because your closing comment sounded like you've solved all the problems.
A simple thank you might have been good but I guess it wasn't needed.

@Moderator, please accept OP's last comment(http:#a38354327) with 0 points.  That way we get to keep the information in EE for others who search.

Thanks.

CyanBlue
0
 
erzoolanderAuthor Commented:
My bad - thank you for the assistance.  It really did help and I do appreciate it :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.