February 2010
M T W T F S S
« Jan   Mar »
1234567
891011121314
15161718192021
22232425262728

Show array elements on the stage using library items as an array elements

Problem

Need show array elements on the stage like: show array random array left to right array right to left array row columns array left to right random array

Solution

Using array as a type. Using length, for, Math.random and other. Please see detailed explanation.

Detailed explanation

Please create 5 movie clip boxes with different color in your library.

show array

var classNames:Array = ["box1", "box2", "box3", "box4", "box5"];
// for efficiency sake - declare the length
var len:int = classNames.length;
// this array will hold of all the boxes
var boxesList:Array = [];
var ClassRef:Class;

for (var i:int = 0; i < len ; i++) {
ClassRef = Class(getDefinitionByName(classNames[i]));
// keep references to the objects in an array
// now each element of the array is a DisplayObject
// this way you don't need a separate array
// for positions and can refer to memebers of array's
// x and y directly
boxesList[i] = new ClassRef();
addChild(boxesList[i]);
}
trace(boxesList);

show_array

random array
random_array


var classNames:Array = ["box1", "box2", "box3", "box4", "box5"];
// for efficiency sake - declare the length
var len:int = classNames.length;
// this array will hold of all the boxes
var boxesList:Array = [];
var ClassRef:Class;

for (var i:int = 0; i < len ; i++) {
ClassRef = Class(getDefinitionByName(classNames[i]));
// keep references to the objects in an array
// now each element of the array is a DisplayObject
// this way you don't need a separate array
// for positions and can refer to memebers of array's
// x and y directly
boxesList[i] = new ClassRef();
addChild(boxesList[i]);
}

// declare variable once
var currentBox:MovieClip;
for (var ii:int = 0; ii < len ; ii++) {
currentBox = boxesList[ii];
currentBox.x = Math.random() * (stage.stageWidth -
currentBox.width);
currentBox.y = Math.random() * (stage.stageHeight -
currentBox.height);
}

left to right array

left_to_right_array


var classNames:Array = ["box1", "box2", "box3", "box4", "box5"];
// for efficiency sake - declare the length
var len:int = classNames.length;
// this array will hold of all the boxes
var boxesList:Array = [];
var ClassRef:Class;

for (var i:int = 0; i < len ; i++) {
ClassRef = Class(getDefinitionByName(classNames[i]));
// keep references to the objects in an array
// now each element of the array is a DisplayObject
// this way you don't need a separate array
// for positions and can refer to memebers of array's
// x and y directly
boxesList[i] = new ClassRef();
addChild(boxesList[i]);
}

// declare variable once
var currentBox:MovieClip;

var nextX:Number = 0;
// left to right
for (var iii:int = 0; iii < len ; iii++) {
currentBox = boxesList[iii];
currentBox.x = nextX;
nextX += currentBox.width;
}

right to left array

right_to_left_array


var classNames:Array = ["box1", "box2", "box3", "box4", "box5"];
// for efficiency sake - declare the length
var len:int = classNames.length;
// this array will hold of all the boxes
var boxesList:Array = [];
var ClassRef:Class;

for (var i:int = 0; i < len ; i++) {
ClassRef = Class(getDefinitionByName(classNames[i]));
// keep references to the objects in an array
// now each element of the array is a DisplayObject
// this way you don't need a separate array
// for positions and can refer to memebers of array's
// x and y directly
boxesList[i] = new ClassRef();
addChild(boxesList[i]);
}

// declare variable once
var currentBox:MovieClip;

var nextX:Number = 0;

currentBox = boxesList[0];
nextX = stage.stageWidth - currentBox.width;
for (var iii:int = 0; iii < len ; iii++) {
currentBox = boxesList[iii];
currentBox.x = nextX;
nextX -= currentBox.width;
}

rows columns array

row_columns_array


var classNames:Array = ["box1", "box2", "box3", "box4", "box5"];
var boxesList:Array = [];
var ClassRef:Class;

for (var i:int = 0; i < classNames.length ; i++) {
ClassRef = Class(getDefinitionByName(classNames[i]));
boxesList[i] = new ClassRef();
addChild(boxesList[i]);
}

var currentBox:MovieClip;

var numCol:int = 3;

for (var columnIterator:int = 0; columnIterator <
boxesList.length; columnIterator++) {
var column:int = columnIterator % numCol;
var row:int = int(columnIterator / numCol);

currentBox = boxesList[columnIterator];
currentBox.x = currentBox.width * column;
currentBox.y = currentBox.height * row;
}

left to right random array

left_to_right_random_array


var classNames:Array=["box1","box2","box3","box4","box5"];
var boxesList:Array=[];
var ClassRef:Class;

for (var i:int = 0; i < classNames.length; i++) {
ClassRef=Class(getDefinitionByName(classNames[i]));
boxesList[i] = new ClassRef();
addChild(boxesList[i]);
}

var currentBox:MovieClip;

var nextX:Number=0;
for (var iii:int = 0; iii < classNames.length; iii++) {
currentBox=boxesList[iii];
currentBox.x=nextX;
nextX+=currentBox.width;
}

for (iii= 0; iii < 1*boxesList.length; iii++) {
var pos1:int = (Math.random()*1000) %boxesList.length;
var pos2:int = (Math.random()*1000) %boxesList.length;

if (pos1!=pos2) {
var temp:int=boxesList[pos1].x;
boxesList[pos1].x=boxesList[pos2].x;
boxesList[pos2].x=temp;
}
}

In attached files you can find all .fla files.

Source:
show_array_from_library

Thank you for reading.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>