0

I need to be able to insert an object in my library to the stage. They are all different .jpg that have been properly imported and have AS Linkage names like GIS_1 GIS_2 etc.

I am trying to run a function to import them one at a time depending on an external variable.

example:

var GIS_Image = new GIS_#();       //the "#" needs to change as depending on a counter
addChild(GIS_Image);

but what I need is to be able to put the name "GIS_1" as a variable so i can have something that allows it to change every time I need a new picture. my first thought was to make a string along the lines of:

var counter:int = 2;
var test:String = ("GIS_"+count);
var circle = new test();

but it wont work because it is looking for an object names test rather than the accessing the string that text makes up.

any ideas?

THANKS!

1 Answer 1

1

Check out getDefinitionByName. This allows you to create an instance of the given class which is identified by String.

package {
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    public class GetDefinitionByNameExample extends Sprite {
        private var bgColor:uint = 0xFFCC00;
        private var size:uint = 80;

        public function GetDefinitionByNameExample() {
            var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
            var instance:Object = new ClassReference();
            instance.graphics.beginFill(bgColor);
            instance.graphics.drawRect(0, 0, size, size);
            instance.graphics.endFill();
            addChild(DisplayObject(instance));
        }
    }
}

So, you could pass the name of a symbol and create new instances of it by demand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.