Skip to main content
Inspiring
July 7, 2009
Answered

Need class to be variableized ; )

  • July 7, 2009
  • 1 reply
  • 1344 views

No matter how I revisit my application, I cannot find a simple way to:

var thisOne:myVariable = new myVariable;

where myVariable is a name of one of my sprites in my library.  I considered creating a class myVariable and rewriting the above line as

var thisOne:myVariable = new myVariable(spriteName);

but then I run into the same problem internally

...

public function myVariable(s:String):void {

     var thisOne:String = new String;   <<<<<<<<<------ this obviously does not work and resembles the above only one step removed.

}

Everytime I think I figured it out using arrays, I end up with only one instantiation being referenced multiple times.  Understandable since arrays access by reference.  So how about it?

A little background in case my approach is orthogonal to best practice (i.e. I'm totally whack).

In each lesson I may create hundreds of identical Sprites that are displayed so the student can count or subquan them.

Followup lessons are similar except for the quantity and the Sprite.  Therefore, I only need to pass the variables quantity and Sprite (which are sometimes animated) to my program.  But I can not figure out to instantiate the Sprite via a variable or string reference.

I hope I didn't miss the obvious in the on-line Adobe references or that my web searches are brain-dead so please bear with me (and, if possible, bear me up.)

This topic has been closed for replies.
Correct answer kglad

var classRef:Class = Class(getDefinitionByName(yourclassstringvariable));
var yourclassinstance:* = new classRef();

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 7, 2009

var classRef:Class = Class(getDefinitionByName(yourclassstringvariable));
var yourclassinstance:* = new classRef();

Inspiring
July 7, 2009

getDefinitionByName took care of the problem for accessing library sprites but for some reason I get:

we are here [object Sprite]

ReferenceError: Error #1065: Variable thing is not defined.
    at global/flash.utils::getDefinitionByName()
    at bna::BNA()
    at bna::BNALesson()

When trying to access the sprites I create (ex: thing)

Snippet from main

                 var myBNA:BNA = new BNA(sX, sY, "thing");

Snippet from BNA.as

           ...

            createThings();
            trace("we are here " + thing);        <<<<<<<<------------- trace above shows thing to be a Sprite object
            var theSprite:Class = Class(getDefinitionByName(s));

            ...

        private function createThings():void {
            thing = new Sprite();    <<<<<<<<<<<----------- declared as class public variable
            thing.graphics.beginFill(0xFFCC00);
            thing.graphics.lineStyle(0,0);
            thing.graphics.moveTo(0,0);
            thing.graphics.drawRect(0,0,16,16);
            thing.graphics.endFill();
        }

Ideas??

kglad
Community Expert
Community Expert
July 7, 2009

where's s defined?