can't reference nested array
var orb_strip:Array = new Array();
var orb_strip_matrix:Array = new Array(orb_strip);
public function OrbArray() //constructor.
{
for (var a:uint = 0; a < orb_strip_row_size; a++)
{
//one for loop create an array of movieClips and draws them on the stage in a row.
for (var i:uint = 0; i < orb_strip_size; i ++)
{
var orb:Orb = new Orb();
// all the properties of each orb are created.
orb.x = (i*50);
orb.y = (a*100);
orb.alpha = 0.3;
orb.orbText.text = ("orb" + i);
orb.label = "blank";
orb.type = "speak";
orb.mouseChildren = false; //?
// more event listners but this time for each orb.
orb.addEventListener(MouseEvent.MOUSE_OVER, orbMouseOver);
orb.addEventListener(MouseEvent.CLICK, orbClick);
orb.addEventListener(MouseEvent.MOUSE_OUT, orbMouseOut);
orb_strip.push(orb); // an orb is born!
addChild(orb);
}
orb_strip_matrix.push(orb_strip);
}
orb_strip_matrix[1][3].alpha = 0.9;
}
Here is the problem this only references orbs in the top or 0 row of the array.
This is nested inside another for loop which creates an array of the movieClip rows. Problem I see them drawn and positioned correctly on the stage but I cant reference any other than the top row in order to change the alpha for example. I have looked for a solution but cant find any examples that attempt it this way so conclude the nested for loop is开发者_运维问答 wrong way to do it? Any ideas greatly received.
I didn't run your code, so I can't see why the Array
access isn't working, to be honest, it looks like it should!
However, I've got a couple of questions, why do you need to have your orbs in the two arrays, and why do you need them in an array at all, you'll be able to access them as children of the DisplayObjectContainer
they're sitting in.
Regarding the two for
loops, instead of nesting them, do one loop, and use modulo
arithmetic to build the grid of Orbs, (modulo will wrap a range of numbers, for example, like a clock wraps 24 hours.)
For example:
var orb_strip = new Array();
public function OrbArray() {
var orbHorizontalSpacing:int = 50;
var orbVerticalSpacing:int = 100;
var orbRowSize:int = 10;
var orbColumnSize:int = 10;
var totalNumberOfOrbs:int = orbRowSize * orbColumnSize;
for (var i:uint = 0; i < totalNumberOfOrbs; i ++)
{
var orb:Orb = new Orb();
// derive x from modulo totalNumberOfOrbs index (i) by orbRowSize.
orb.x = (i%orbRowSize * orbHorizontalSpacing);
// derive y from totalNumberOfOrbs index (i) by orbColumnSize
orb.y = (Math.floor(i / orbColumnSize) * orbVerticalSpacing);
orb.alpha = 0.3;
orb.orbText.text = ("orb" + i ); // i is now 0 to totalNumberOfOrbs
orb.label = "blank";
orb.type = "speak";
orb.mouseChildren = false;
orb.addEventListener(MouseEvent.MOUSE_OVER, orbMouseOver);
orb.addEventListener(MouseEvent.CLICK, orbClick);
orb.addEventListener(MouseEvent.MOUSE_OUT, orbMouseOut);
orb_strip.push(orb); // an orb is born!
addChild(orb);
}
}
Which still includes the Array, if you feel you really need it.
Update:
I'd recommend you use modulo to determine if an Orb is at the beginning or end of a row, instead of the 2D array...
But in regards to why the array access isn't working, Well, this'll teach me for not looking closer at your original code. You only have one orb_strip
array being created at the class level, you'd need an array for each row.
精彩评论