ActionScript 3.0 :: Using Square Brackets And String To Instantiate Class?
Oct 13, 2010
I have some classes stored in a swc which I would like to instantiate using data pulled in by xml - using a string to reference the classname.So where I would normally use
ActionScript Code:
var tagline:Sprite = new Tagline1();
I would now like to do something like
ActionScript Code:
var tagname:String = "Tagline1"; // really the string comes from XML
var tagline:Sprite = new [tagname]();
I have a vague idea this is possible using square brackets, but I don't seem to be able to find out anything about it.
I'm trying to do that trick where you make a variable name with square brackets. I think I don't fully understand what I'm doing though...
Code: var cursorDirection1:Point = pt.subtract(rootpos1); var cursorDirection2:Point = pt.subtract(rootpos2); var farShoulder:int = 1;//1 = left 2 = right
[Code]....
I'm trying to avoid having to do a conditional to choose between cursorDirection1 or cursorDirection2. My last line there isn't working, I'm not entirely sure why. Assuming farShoulder is 1, the code should be interpreted as
I'm stuck with trying to target a movieclip dynamically from a loop. I'm using the square brackket syntax but I can't figure out why it's not working. Here's what I'm trying to do: Code: this.t1.t1._alpha=20; And here's how I'm trying to do it: Code: this["t"+1+".t"+1]._alpha= 20;
I'm reading a book on ActionScript and I'm in the section on controlling movie clips. I'm reading about the basic methods, and I'm starting to realize I still don't fully understand the significance of square brackets.[code]Obviously, this is the prototype form of the lineStyle() method. What I don't get is why "thickness" is the only parameter without square brackets. I know that square brackets are used for retrieving data from an array or object, but either my book doesn't cover why they're used like this in a method, or I just completely blanked it out.
I've got a string which, in run-time, contains the name of a class that I want to instantiate. I read suggestions to use flash.utils.getDefinitionByName(): var myClass:Class = getDefinitionByName("package.className") as Class; var myInstance:* = new myClass();
However, that gives me the following error: [Fault] exception, information=ReferenceError: Error #1065: Variable className is not defined.
I am kinda new to as3 and I have seen [] the square brackets used when trying to access array index but I have also seen some people use it in other places. I was just wondering what its exact function was.
I have an SWF that has a var that is a string called fullpath. Now, if fullpath was "game.item1", How would I evaluate it as _level0.game.item1 in brackets [] rather than _level0.game.item1.Basically, how would I evaluate an item path from a string with brackets []? This is AS 2
I would like to know if its possible, to use a string in order to create a UI element.I think many people have this kind of problem, they want an easy setup in flash to generate a dynamic UI, through code.It should look like this (pseudo code):
addToStage("Button","add"); addToStage("TextInput","name"); private function addToStage ( o : Object, str : String ) : void {
I have some classes: LowA, LowB, LowC, SpaceShip, that all subclass BaseChar. How can I instantiate one of the subclasses by using a string for the name. (I have been trying to get this to work for a couple of hours now). For example, the typical way that I have been instantiating the subclasses:
I keep getting this error: Incorrect number of arguments. Expected 0.Probably just something stupid... but I'm struggling...I have two classes (ribostrand.as and nucleo.as) in a folder called architect. The main FLA file is outside the folder.I'm trying instantiate the nucleo class with parameters, from the ribostrand class... but its not working.
var ribo:nucleo = new nucleo("A",50,50) < code moved to the next thread and reformated >
I was chatting with my buddy about this, he is convinced you can do this and says he has done it, but I cannot get this to work.
I am wondering if it is even possible at all. I tried typing a var as a Class that is within the externally downloaded SWF and then making an instance but no can do.
some code
private static function onCompleteHandler(e:Event) { dashboardObject = e.target.content; // registerClassAlias("Dashboard", ); doesnt work
[Code]....
So it seems you cannot make an instance of a class unless it is complied within the project SWF. Which if true is what I want it to do. I do not want people trying to make instances of my classes just from downloading the SWF file for what I am building here.
I have an abstract class which gives specific 'base' behavior to multiple sub classes. I want to instantiate a Singleton inside this abstract class. Is it good practice to:a) instantiate a class within an abstract classb) do this with a Singleton (I know these may be frowned upon)For clarity I will give an example, the method instantiating the Singleton is:
public function createErrorRepository(repositoryType:String):void { this._errorFactory = ErrorFactory.getInstance(); this._errorRep = this._errorFactory.createErrorRepository(repositoryType);
To call a class we would type: var testClass = new TestClass();Can I store the class in an array, and somehow pull it when I want to call it the same way? I need to be able to call a new class like the example above, but from an array.
How can I instantiate another class object from a class instance? In the code below (which doesn't work) I'd like the function to return a new class instance based the passed argument's class. In other words, I want the function to return a new instance of MySprite without having to call new MySprite();.
var mySprite:Sprite = new MySprite(); var anotherSprite:Sprite = makeAnotherSprite(mySprite); function makeAnotherSprite(instance:Sprite):Sprite { return new getDefinitionByName(getQualifiedClassName(instance)); }
I'm trying to use a listbox to instantiate a class. "SEA30_05_215_Single_Classroom" is a class that is loaded in using another function just fine. When I attempt to use the function below I get an error that says "Instantiation attempted on a non-constructor."Any ideas how to either fix the error or do it a different way? It seems like it should be a fairly simple thing to instantiate a class using a list box..[code]
The following code works to instantiate a class from a listbox selection...
[Code]....
But when I try and load the same label and data from XML it does NOT work... Below is the for loop that I'm using to parse my XML and populate the listbox:
[Code]...
The setupID should be populating with a class that I can instantiate (just like when I hard-code it without using XML). Instead I get the error "Instantiation attempted on a non-constructor." I also received this error when I had quotes around the hard-coded version. So, I'm assuming that maybe the XML is being read as a string. But I haven't been able to find a way to cast it differently. I've been stuck on this for quite some time now.
Possible Duplicate: Where is the "proper" place to initialize class variables in AS3 Whether its better to instantiate class on it's variable declaration or within a constructor? For example, this: protected var _errorHandler:ErrorHandler = new ErrorHandler(); or this: protected var _errorHandler:ErrorHandler; public function someClass() { _errorHandler = new ErrorHandler(); }
Using org.as3commons.reflect I can look-up the class name, and instantiate a class at runtime.I also have (non-working) code which invokes a method. However, I really want to set a property value. I'm not sure if properties are realized as methods internally in Flex.I have a Metadata class which stores 3 pieces of information: name, value, and type (all are strings).I want to be able to loop through an Array of Metadata objects and set the corresponding properties on the instantiated class.[code]I realize that I have to declare a dummy variable of the type I was to instantiate, or use the -inculde compiler directive. An unfortunate drawback of Flex.Also, right now there's code to account for typecasting the value to it's specified type.
I'm unsuccessfully attempting to instantiate a reference of a class that is passed as a parameter to another class. In this example there are 3 classes: MainClass, Canvas, MyCircle From the MainClass I am creating an instance of Canvas, which is passed a class reference of MyCircle as I want to create instances of MyCircle from within Canvas. However, the MyCircle constructor contains required parameters that are created from within Canvas. How can I pass and instantiate a class reference with required parameters?
MyCircle: package { //Imports import flash.display.Shape; //Class public class MyCircle extends Shape { [Code] .....
I would like to dinamically create array variables, depending on a XML file. The problem I have is that I don't know how to create them dinamically using a name I have constructed into a string variable. What I would like is something like this:
var strVarName; ... strVarName = "Var" + i; var strVarName = new Array(); ...
So I would have n arrays called Var1, Var2, ... Varn.
i'm trying to do is to move all the squares. I've got a Square movie clip class in my library. The program should populate an amount of squares, unfortunately the amount can vary, which is why i haven't just gone with
var square1:Square = new Square(); var square2:Square.. etc. var thisSquare:Square;[code]........
I have this game, and some levels have a moving square as the player, you move the square with your arrow keys.... I want to have an enemy square that chases the player square.... They only chased mario when you didn't look at them? Kinda like that...... but in this regards:
I only want the enemy square to chase the player square when the player square moves.... soo say the player square moves 5 pixels per movement (know how to do all that), I want the enemy square to move when the player square moves..... towards the player square at like 7 pixels per movement..... so eventually you won't be able to avoid the enemy square....
When I mouseover the square and it expands and when I mouseout the square it should shrink back to its original position. The expandSquare works but the shrinkSquare doesn't.[code]