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.
Related
Struggling to find the answer. Right so I have a variable which is storing a number, and I have a movieclip called "note". I want to add the number onto the end of the note while adding it to the stage. Any suggestions please ?
var num = 1;
addChild(note); // Should be note1
Edit: Thanks for all your help, i've solved the solution. What I have done is created an array for of the movieclips and used a for statement to go through all of the movieclips.
If note here is the name of your class (AS linkage), and you want that your instance be called note1, for example, your code can be like this :
var num:int = 1;
this['note' + num] = new note();
addChild(this['note' + num]);
Hope that can help.
Try this
import flash.utils.getDefinitionByName;
import flash.display.MovieClip;
var num = 1;
var cl:Class = getDefinitionByName("Note"+num) as Class;
addChild(new cl() );
I have a code in AS3 that works perfectly, but I have mane repeated methods and functions, they are the same but using different instance names, so I would like to replace the instance name with a variable to avoid re-writing too much code.
here is part of my code:
urb_mc.urb.select(0);
trace("Urb: " + urb_mc.urb.selectedIndex);
I want to replace in this case "urb" with a variable so I tryed this:
var estado = currentLabel;
trace("este es mi estado " + estado);// this is ok = "urb"
//now I need to inset the variable in my code:
String(estado)+_mc.String(estado).select(0);//thi is so wrong!
trace("Urb: " + String(estado)+_mc.String(estado).selectedIndex);//thi is so wrong!
Any idea?
Thanks in advance
Try using:
this[estado+"_mc"][estado].select(0);
trace(this[estado+"_mc"][estado].selectedIndex);
Sorry for all the edits!
Thanks for the challenge! I learned there is such a thing as a multidimensional array operator for objects.
Part 2
Try this:
var tweenNameArray:Array = ["Name1", "Name2", "Name3"]
for (var i:int = 0; i > tweenNameArray.length(); i++){
var myTween:Tween = new Tween();
myTween.name = String("myTween_" + estado + "_in"); // You may want to try .toString();
}
Then referencing the tweens should work like this:
Tween(MovieClip(this.stage.getChildByName("myTween_" + estado + "_in")).whateverMethod(); // Try with and without the MovieClip().
I will say right now, it is not recommended to do things this way.
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]);
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...
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 = ...