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

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";
// ...
}

Related

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 to push instantiated MC into Array dynamically?

Im really stuck. I have 5 MC´s that are being spliced from one array at a certain time. In that same function I want to push another movieclips into another array. The two arrays holds mc's that represent right or wrong answers. So when one question is being given a correct answer that questions visualisation alters.
This function holds a incrementing variable as I do want the mc's to be pushed by the user and one at the time. The thing is I cant seem to refer them properly.
I´ve tried
pQuestSum = this[pQuest + pQuestNumber];
and
pQuestSum = this[pQuest] + pQuestNumber;
and pretty much everything I´ve imagined would work...but the problem is I havent tried
the right thing.
when I trace pQuestSum (which would be the reference) I get an error saying thats its not a number.
this is one of 5 mc's named from 1-5:
var passedquest1:PassedQuest = new PassedQuest();
this is the vars that i try to to build a reference of
var pQuest = "passedquest";
var pQuestNumber = 1;
var pQuestSum;
var questCorrArray:Array = [];
if(event.target.hitTestObject(questArray[ix])){
removeChild(questArray[ix]);
questArray.splice(ix,1);
pQuestNumber ++;
pQuestSum = this[pQuest] + pQuestNumber;
trace("pQuestSum"); // NaN
questCorrArray.push(pQuestSum);
//trace(questArray.length);
pointsIncreased = false;
questPoints = 0;
}
How do I refer an existing movieclip when the reference consists of both a string and a number? Hope I made myself somewhat clear:)
If you had an instance of an object on your timeline called "passedquest1" (as an example), then you could access it this way:
var myObj = this["passedquest" + 1];
Or,
var pQuest = "passedquest";
var pQuestNumber = 1;
var myObj = this[pQuest+ pQuestNumber.toString()];
When you do this: pQuestSum = this[pQuest] + pQuestNumber;, you are trying add the number to an object (this[pQuest]), unless you have number/int var called "passedquest", this will result in NaN.

How do i make my pickNum array just one index instead of 3 different indexes.

this will random my numPool and push the random three numbers to my array called pickNum. I need that pickeNum to be just one index instead of three indexes. Thanks and i will appreciate any help thanks.
var numPool:Array = [1,2,3];
var pickNum:Array = [];
var randomCount:Number = 3;
var r:Number;
for (var i = 0; i < randomCount; i++)
{
r = Math.floor(Math.random() * numPool.length);
pickNum[pickNum.length] = numPool.splice(r,1);
}
trace("Number Picked " + pickNum);
Can't say I completely understand what you are saying, but the issue with the code above is that splice returns an Array containing the values that were spliced from the array. Also, would be easiest to just do a push. So the proper line for getting that value and pushing it into the pickNum array would be :
pickNum.push(numPool.splice(r,1).pop());
And if you are saying that you want only 1 random number from the pool, then why do you need a loop ?

How to assign variables to an instance name?

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.

Losing leading 0s when string converts to array

I have a textInput control that sends .txt value to an array collection. The array collection is a collection of US zip codes so I use a regular expression to ensure I only get digits from the textInput.
private function addSingle(stringLoader:ArrayCollection):ArrayCollection {
arrayString += (txtSingle.text) + '';
var re:RegExp = /\D/;
var newArray:Array = arrayString.split(re);
The US zip codes start at 00501. Following the debugger, after the zip is submitted, the variable 'arrayString' is 00501. But once 'newArray' is assigned a vaule, it removes the first two 0s and leaves me with 501. Is this my regular expression doing something I'm not expecting? Could it be the array changing the value? I wrote a regexp test in javascript.
<script type="text/javascript">
var str="00501";
var patt1=/\D/;
document.write(str.match(patt1));
</script>
and i get null, which leads me to believe the regexp Im using is fine. In the help docs on the split method, I dont see any reference to leading 0s being a problem.
**I have removed the regular expression from my code completely and the same problem is still happening. Which means it is not the regular expression where the problem is coming from.
Running this simplified case:
var arrayString:String = '00501';
var re:RegExp = /\D/;
var newArray:Array = arrayString.split(re);
trace(newArray);
Yields '00501' as expected. There's nothing in the code you've posted that would strip leading zeros. You may want to dig around a bit more.
This smells suspiciously like Number coercion: Number('00501') yields 501. Read through the docs for implicit conversions and check if any pop up in your code.
What about this ?
/^\d+$/
You can also specify exactly 5 numbers like this :
/^\d{5}$/
I recommend just getting the zip codes instead of splitting on non-digits (especially if 'arrayString' might have multiple zip codes):
var newArray:Array = [];
var pattern:RegExp = /(\d+)/g;
var zipObject:Object;
while ((zipObject = pattern.exec(arrayString)) != null)
{
newArray.push(zipObject[1]);
}
for (var i:int = 0; i < newArray.length; i++)
{
trace("zip code " + i + " is: " + newArray[i]);
}