Group for Array - actionscript-3

Possible to group multiple MC and post it in an Array?
exp:
array([mc1,mc2,mc3],[ac1],[bc1,b2])
If its possible how could it be achieve in a for...loop function?

You are almost there.
var mcArray1:Array = [[mc1, mc2, mc2], [ac1], [bc1,b2]];
//or
var mcArray2:Array = new Array([mc1, mc2, mc2], [ac1], [bc1,b2]);
//you can access them with for loops
var array:Array;
var mc:MovieClip;
for(var i:int = 0; i < mcArray.length; i++)
{
array = mcArray1[i];
for(var j:int = 0; j < array.length; j++)
{
mc = MovieClip(array[j]);
mc.x = 20;//do whatever you want with it.
}
}

I could understand your question wrong, but it seems that you just need multidimensional array, i.e. array of arrays:
var array: Array = new Array(
new Array(mc1,mc2,mc3),
new Array(ac1),
new Array(bc1, b2)
);

Related

How To Create An Array of Movie Clip with number

I have multi movieclips in my stage and they have instanceName e.g. k1,k2,... . I want to createar=[k1,k2,k3,k4, ...].
var i: int;
var ar: Array = new Array();
for (i = 1; i < 5; i++)
{
ar[i-1] = ["k" + i];
}
trace(ar);
ar[1].x = 100;
But the end of code does not perform.
What you want to do is to create an array of MovieClips, but instead of it you create an array of Arrays of Strings. To achieve your goal, you need to find an instance of a clip on stage by its name. Here is how you can try to do that:
const array:Array = new Array();
for(var i:int = 0; i < 5; i++) {
const childName:String = "k" + (i + 1);
const myMovieClip:MovieClip = stage.getChildByName(childName) as MovieClip;
array.push(myMovieClip);
}

Is there any difference in performance between these two for loop?

As I said: Is there any difference (in performance) between these two for loop?
Between this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
var currentObject:DisplayObject = displayObjects_[i];
currentObject.width = newWidth;
}
and this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
displayObjects_[i].width = newWidth;
}
I tested them before. The result said the first one was faster but I don't know if I did it right.
I know it's not really an answer to your question, but if you're looking for fastest way to iterate through this array you should do:
for each(var currentObject:DisplayObject in displayObjects_) {
currentObject.width = newWidth;
}
I tried this out on a bare-bones project using SDK 4.6. This is the code.
public class Main extends Sprite
{
public function Main()
{
var displayObjects_:Array = [];
for (var i:int = 0; i < 1000000; i++)
{
displayObjects_.push(new Sprite());
}
var start:int = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
var currentObject:Sprite = displayObjects_[i];
currentObject.width = 100;
}
var end:int = getTimer()
trace(end, start, end - start);
start = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
displayObjects_[i].width = 100;
}
end = getTimer()
trace(end, start, end - start);
}
}
These are the results.
Done(0)
[Starting debug session with FDB]
16703 16250 453
17141 16703 438
Like I said, it would be very surprising to see any difference between the two. You'll probably see more improvements through using Vector instead of Array. Otherwise, this stuff is too mundane to fuss over.
As I know, Actionscript compiler automatically moves variable definitions to the begin of the function. So, this loop doesn't declare variable each time, first sample the same that:
var currentObject:DisplayObject;
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
currentObject = displayObjects_[i];
currentObject.width = newWidth;
}
So, I think the difference only in one additional variable declare and it willn't affects performance. But 3vilguy's example is better.

Creating variables in a systematical way using loops in AS3?

Is there? Like Let's say I need 5 variables in ActionScript 3.0 and would like to name them as follows:
var myVar_1 = "things";
var myVar_2 = "things";
var myVar_3 = "things";
var myVar_4 = "things";
var myVar_5 = "things";
But instead of having to type them 1 by 1, would it work in a loop? I can't seem to make it work and would really love some help/advice on this matter.
Yes, you can create a dynamic property name using [ ] array access:
var variables:Object = {};
for(var i:int = 0; i < 5; i++){
variables["myVar" + i] = "value " + i;
}
trace(variables.myVar3); // "value 3"
The variables object in this case could be replaced by any dynamic object, including MovieClips.
However, in most cases to store data by index it usually makes more sense to use an array. Example:
var variables:Array = [];
for(var i:int = 0; i < 5; i++){
variables.push("value " + i);
}
trace(variables[3]); // "value 3"
You should use Vector.<String>, Array, Object or Dictionary for that:
var variables:Vector.<String> = new <String>[];
for(var i:int = 0; i<5; i++)
{
variables[i] = "things";
}

Cleaner way to get a key and value from JSON

Currently the way that I am taking a JSON string where I "Don't know the contents" and pulling the keys and the values is as follows:
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
var arrLen = arr.length;
for(var i = 0; i < arrLen; i++){
var myKeys = Object.keys(arr[i]);
var keysLen = myKeys.length;
for(var x = 0; x < keysLen; x++){
keyName = myKeys[x];
keyValueStr = "arr[i]."+keyName;
keyValue = eval(keyValueStr);
console.log(keyName+':'+keyValue);
}
}
There has to be a cleaner and more efficient way of doing this. Any suggestion would be appreciated.
Using jQuery you can use http://api.jquery.com/jQuery.parseJSON/ & Object.keys - Than:
var obj = jQuery.parseJSON('{"manager_first_name":"jim","manager_last_name":"gaffigan"}');
var keys = Object.keys(obj);
jQuery.each(keys,function(k, v){
console.log(v);
console.log(obj[v]);
});
Create an object and initialize it with your JSON:
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
var JSONObject = new MyJSONObject(arr);
in your constructor, set the object's properties.
You could use for-in loop which iterates over the enumerable properties of an object, in arbitrary order.
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
for (var i = 0; i < arr.length; i++) {
var currentObj = arr[i];
for (var item in currentObj) {
console.log(item + " : " + currentObj[item]);
}
}
If your array always have only one item, you could omit the outermost for loop and just use arr[0]
var currentObj = arr[0];
for (var item in currentObj) {
console.log(item + " : " + currentObj[item]);
}

adding movieclips from random generated no in array

hey i am trying to add movie clips to the stage randomly from a list of cards, no 1- 10,
this is what i have tried so far but i get an error saying its my randomly selected card is not a function, just wondering if anybody can help or know the proper way of accomplishing it
thank you
var printArray:Array =new Array();
var randPrint:String;
var rand
for(var n:int = 1; n <= 28; n++)
{
randNo=Math.round(Math.random() * 10+.5);
randPrint = "cardPrint"+randNo;
printArray.push(randPrint);
}
var cardPrint1:MovieClip = new card_1();
var cardPrint2:MovieClip = new card_2();
var cardPrint3:MovieClip = new card_3();
var cardPrint4:MovieClip = new card_4();
var cardPrint5:MovieClip = new card_5();
var cardPrint6:MovieClip = new card_6();
var cardPrint7:MovieClip = new card_7();
var cardPrint8:MovieClip = new card_8();
var cardPrint9:MovieClip = new card_9();
var cardPrint10:MovieClip = new card_10();
for(var p:int = 1; p <= 1; p++)
{
trace(printArray[p]);
addChild(printArray[p]);
}
some help would be great, thank you so much
I think something like the following will do what you want. I populated an array with all the available assets and then filled an array with random numbers between 0-9. The last for loop just creates the movieclips and adds them to the stage.
var printArray:Array = [];
var mcs:Array = [card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8, card_9, card_10];
for(var n:int = 1; n <= 28; n++)
{
var randNo:int = int(Math.random() * 10);
printArray.push(randNo);
}
for(var p:int = 0; p < printArray.length; p++)
{
trace(printArray[p]);
var mc:MovieClip = new mcs[printArray[p]];
addChild(mc);
}