How to assign variables to an instance name? - actionscript-3

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.

Related

creating multiple sharedobject dynamically

I would like to dynamically create multiple sharedobject to create multi-login and I used the below code for that. But it throws error.Please post if there is any way to do it.
var k:Number=0;
var signup:SharedObject = SharedObject.getLocal("newSignUp");
signup_mc.signup_btn.addEventListener(MouseEvent.CLICK, signupcheck);
function signupcheck(event:MouseEvent):void {
newusername = signup_mc.username_txt.text;
newpassword = signup_mc.password_txt.text;
signup.data.nwusername+k = newusername;
signup.data.nwpassword+k = newpassword;
signup.flush();
k++;
}
Sure there would be errors. Assigning a+b=c will be wrong in most languages, I guess. If you want keys like nwusername0, nwusername1 etc you need to create keys in a correct way:
signup.data["nwusername" + k] = newusername;
signup.data["nwpassword" + k] = newpassword;

CreateJs MovieClip Instance name

Hi Guys (sorry for my bad english), I've stuck with this in a week, is it possible to get the movieclip instance name with createJS (I've put the movieclip manually on the stage and I give it an instance name), in AS3 I can use movieclip.name to get the instance name, how can I do that in createJS, when I use this.movieclipName.name it always return null in console log, do You have any suggestion? Thanks for Your help?
#Ferry thank You very much for Your solution. finally I figured it out, We can't get the instance name of the object by myObject.name because the name of the object is not signed previously (it looks like the instance name not same as name in html5). that's why it always giving a null. This is my script to get all the instances name of my objects.
for (var a = 0; a <= maxObject - 1; a++) {
var myObjectName = 'object' + (a + 1);
var myObject = this[arrObject[a]];
myObject.name = myObjectName;
}
Thank you guys for all of your respon.
i solve this problem using:
for (i = 1; i < 6; i++) {
this["m" + i].addEventListener("click", clickme.bind(this));
this["m" + i].hamdy=i;}
so you can use var hamdy to pass any change in clickme funaction.

AS3 Concatenating Reference Not Working

I'm struggling to concatenate a reference to a variable from a XML document. I'm trying to get:
chat_History.Msg.chatMessage1, chat_History.Msg.chatMessage2, chat_History.Msg.chatMessage3
It's instead over-riding the reference and turning into the value '0', '1', '2'. My code:
public function onReceivedChatData(Event:LoaderEvent)
{
var raw_user_info = LoaderMax.getContent("chatHistory");
var chat_History:XML = XML(raw_user_info);
if (chat_History.Msg)
{
trace("ReceivedChatData");
trace(chat_History);
for (var i:int = 0; i < int(chat_History.chatLength); i++)
{
var chatString:String = chat_History.Msg.chatMessage;
chatString += i.toString();
shopchatbox.shop_chat_window.text = shopchatbox.shop_chat_window.text + "\n" + chatString;
shopchatwidebox.shop_chat_window.text = shopchatwidebox.shop_chat_window.text + "\n" + chatString;
}
}
else
{
trace("chat_History XML Does Not Exist!!! Noooo :( ");
trace(chat_History);
}
}
The chatLength is 3, and it's calling the for statement 3 times correctly, however chatString isn't referencing it's variable (a string) correctly and only appears as '0', '1', '2'. I'm guessing I'm not concatenating this right and that's the problem, but I'm not sure how to do this?
Thanks!
chatString += i.toString();
That's going to give you the indexes 0, 1, 2. i just stores increments for iteration. Not the values from chat_History.Msg.chatMessage So you're adding i onto the content of chatString, not setting the variable name.
var chatString:String = chat_History.Msg.chatMessage;
chatString += i.toString();
Your code here says, take chatString, and set it to the value chat_History.Msg.chatMessage, then concatenate i as a String onto the content of chatString
If you wanted to access your variables by a variable name, I believe you'd do something like this;
var chatString:String = chat_History.Msg["chatMessage"+ String(i+1)];
So i+1 = 1, 2, 3. Which should mean you're accessing chat_History.Msg.chatMessage1, chat_History.Msg.chatMessage2 and chat_History.Msg.chatMessage3
Remove the line;
chatString += i.toString();
I'm afraid I can't test this as I don't have the XML code, but hopefully that gives you a general idea. I'm not 100% sure I've got calling a variable by a variable name correct, but it is possible to call a variable by a string name.
Edit:
After doing a little testing, the syntax appears to be correct, so that should get you the values from chat_History.Msg

How Do You Concatenate Arrays In Actionscript 3 When Using Different Image Files?

I've looked everywhere on the web to see if I could find an answer to this and I've looked throughout the Adobe Flash Builder help site too and I can't seem to find a concrete answer for what I'm trying to do.
It is possible to reference an array inside of an image file name, here's the scenario.
I have a list of images that only differ by the number at the end of the name (for example. orangeimg1.jpg, appleimg2.jpg, strawberryim3.jpg, etc. Is it possible when referencing the image that I could somehow reference the array in the file name rather than repeating the same code over and over again?
I have two different arrays set up one for fruit which has (orange, apple, stawberry) and I have another array with the numbers (1, 2, 3). I have jpg images for each of these combinations but how to I reference that in one line when I'm trying to refer to these images. I thought something like source = "[fruit].img.[number].jpg" would work but I'm a newbie so I'm pretty sure that isn't right.
Again I've found some information on the web but it doesn't refer to how it would work if I was coding a source for my images.
I'm really confused and would appreciate any help with this. Thanks.
Strings are concatenated using +.
var fruits:Array = ["banana", "cherry"];
var numbers:Array = [4, 2];
var source = fruits[0] + "img" + numbers[0] + ".jpg"; // bananaimg4.jpg
If you want to create multiple file names, you can use a loop:
for(var i:int = 0; i<fruits.length; i++) {
var source = fruits[i] + "img" + numbers[i] + ".jpg";
// ...
}
You probably don't need that numbers array. If those numbers are in order you could calculate the number in the loop:
for(var i:int = 0; i<fruits.length; i++) {
var source = fruits[i] + "img" + (i+1) + ".jpg";
// ...
}
Above answer is good but i am giving only some changes to make code more effective.
We can use for loop in given below format. it's optimized code:
var len:uint = fruits.length;
for(var i:int = 0; i<len; i++) {
var source = fruits[i] + "img" + (i+1) + ".jpg";
// ...
}

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.