AS3 How to instantiate a class with getdefintionbyname and getqualifiedbyclass with TYPED declaration - actionscript-3

example:
var c : Class = Sprite;
//This can be random class such as movieclip/etc
var o = getDefintionByName(getQualifiedClassName(c));
this works, but in flash develop,
it says that the variable 'o' has no type declaration
which basically means
var o : SOMETHING = getDefintionByName(getQualifiedClassName(c));
but how do i put that something there when i do not know what its coming because of random classes?

one solution would be using
var o : * = getDefintionByName(getQualifiedClassName(c));
the asterick symbol is a temporary one, but it works

Create an interface or a base class for the classes you wish to instantiate and then type the var to that.
var o:ICustomClass = ...
or
var o:BaseClass = ...

Related

as3 can I do this on one line

Hi I can go this
var firstname = firstname_mc;
var fname = firstname.getChildAt(0).text;
but
var firstname = MovieClip(firstname_mc).getChildAt(0).text;
does not work
I assume firstname_mc is not a MovieClip instance. Maybe it is an instance of DisplayObjectContainer? Therefore casting fails.
Try this instead :
var firstname = TextField(MovieClip(firstname_mc).getChildAt(0)).text;
Assuming, firstname_mc is a movieclip with a textfield inside that you are trying to access.
If firstname_mc is a movieclip or sprite:
var firstname = TextField(firstname_mc.getChildAt(0)).text;
However, your screwed if you add another movieclip inside the firstname_mc, right under the textfield. Then you are not sure its the child at 0.. So why not give it the textfield a name (like "label_txt")? Then you can do much shorter syntax:
var firstname = firstname_mc.label_txt.text;

How to declare a filled Vector?

I am using Vectors in Flash 10 for the first time, and I want to create it in the same way I used to do with Arrays, e.g:
var urlList : Array = [url1, url2, url3];
I have tried various different methods but none seem to work, and I have settled on the following as a solution:
var urlList : Vector.<String> = new Vector.<String>();
urlList.push(url1, url2, url3);
Is this even possible?
When it doubt, check the AS3 docs. :)
var urlList : Vector.<String> = new <String>["str1", "str2", "str3"];
trace(urlList);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#Vector()
Direct quote of the line I adapted this from in the documentation:
To create a pre-populated Vector instance, use the following syntax instead of using the parameters specified below:
// var v:Vector.<T> = new <T>[E0, ..., En-1 ,];
// For example:
var v:Vector.<int> = new <int>[0,1,2,];
You coerce an array to a Vector:
var urlList:Vector.<String> = Vector.<String>([url1, url2, url3]);

Obtain a Class reference to Vector.<T>

Is it possible to obtain a Class reference to a Vector.<T>? I tried the following code:
var cls : Class = Vector.<int>;
But it fails with a coercion error, presumably because Vector.<T> is also a global function. Is there a simple way around this?
Edit: Best solution:
var vectorIntDefinition:Class = Vector.<int> as Class;
I don't know if you call this solution simple as you need some "reflection magic" but it works:
var vectorIntClassName:String = getQualifiedClassName(Vector) + ".<" + getQualifiedClassName(int) + ">";
var vectorIntDefinition:Class = getDefinitionByName(vectorIntClassName) as Class;
Hint: If you use that more than once you could create a little helper method.
Edit: Look at my 2nd comment.

AS3 How to make a kind of array that index things based on a object? but not being strict like dictionary

How to make a kind of array that index things based on a object? but not being strict like dictionary.
What I mean:
var a:Object = {a:3};
var b:Object = {a:3};
var dict:Dictionary = new Dictionary();
dict[a] = 'value for a';
// now I want to get the value for the last assignment
var value = dict[b];
// value doesn't exits :s
How to make something like that. TO not be to heavy as a lot of data will be flowing there.
I have an idea to use the toString() method but I would have to make custom classes.. I would like something fast..
Why not make a special class that encapsulates an array, put methods in there to add and remove elements from the array, and then you could make a special method (maybe getValueByObject(), whatever makes sense). Then you could do:
var mySpecialArrayClass:MySpecialArrayClass = MySpecialArrayClass();
var a:Object = {a:3};
var b:Object = {a:3};
mySpecialArrayClass.addElement(a,'value for a');
var value = mySpecialArrayClass.getValueByObject(a);
I could probably cook up a simple example of such a class if you don't follow.
Update:
Would something like this help?
http://snipplr.com/view/6494/action-script-to-string-serialization-and-deserialization/
Update:
Could you use the === functionality? if you say
if ( object === object )
it compares the underlying memory address to see if two objects are the same reference...

Dynamic variables in ActionScript 3.0

so.... eval() out of the question, any idea to do this? I also don't know how to use "this" expression or set() in actionscript 3 ( i seem couldn't find any complete reference on it ), just say through php file a multiple variable (test1, test2, test3,...) sent by "echo", how the flash aplication recieved it? I'm trying not to use xml on mysql to php to flash aplication. Simply how to change a string to a variable ?
example
(in as3-actions frame panel)
function datagridfill(event:MouseEvent):void{
var varfill:URLVariables = new URLVariables();
varfill.tell = "do it";
var filler:URLRequest = new URLRequest();
filler.url = "http://127.0.0.1/flashdbas3/sendin.php";
filler.data = varfill;
var filling:URLLoader = new URLLoader();
filling.dataFormat = URLLoaderDataFormat.VARIABLES;
filling.load(filler);
filling.addEventListener(Event.COMPLETE, datain);
function datain(evt:Event){
var arraygrid:Array = new Array();
testing.text = evt.target.Name2 // worked
// just say i = 1
i=1;
arraygrid.push({Name:this["evt.target.Name"+i],
Test:this.["evt.target.Test"+i]}); // error
//or
arraygrid.push({Name:this["Name"+i],
Test:this.["Test"+i]}); // error too
// eval() noexistent, set() didn't worked on actions frame panel
//?????
}
};
I hope it's very clear.
You could use this[varName] if I understand your question right.
So if varName is a variable containing a string which should be a variables name, you could set and read that variable like this:
this[varName] = "someValue";
trace(this[varName]);
Update:
In your example, you could try: evt.target["Test"+i] instead of Test:this.["evt.target.Test"+i]
If you have a set of strings that you'd like to associate with values, the standard AS3 approach is to use an object as a hash table:
var o = {}
o["test1"] = 7
o["test2"] = "fish"
print(o["test1"])