[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

AS 3 Dynamic Creation of Display Objects From Library

Posted on 2012-08-13
12
Medium Priority
?
311 Views
Last Modified: 2012-09-03
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!
0
Comment
Question by:erzoolander
  • 5
  • 5
10 Comments
 
LVL 37

Expert Comment

by:CyanBlue
ID: 38288495
Try using getDefinitionByName() function to reference them.

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

Open in new window

0
 
LVL 2

Author Comment

by:erzoolander
ID: 38288548
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
 
LVL 37

Expert Comment

by:CyanBlue
ID: 38289775
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:erzoolander
ID: 38296181
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
 
LVL 2

Author Comment

by:erzoolander
ID: 38338781
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
 
LVL 37

Expert Comment

by:CyanBlue
ID: 38338782
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
 
LVL 37

Expert Comment

by:CyanBlue
ID: 38354268
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
 
LVL 2

Accepted Solution

by:
erzoolander earned 2000 total points
ID: 38354327
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
 
LVL 37

Expert Comment

by:CyanBlue
ID: 38357557
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
 
LVL 2

Author Comment

by:erzoolander
ID: 38357672
My bad - thank you for the assistance.  It really did help and I do appreciate it :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
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.
Suggested Courses

834 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