tags:

views:

37

answers:

1
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.

A: 

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.

slomojo
Hi thank you for your time in answering my problem.
adrian
I wanted to use arrays because I wanted to be able to manipulate the orbs with array type functions and also I thought the array access method would be straight forward (Quoting you "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!") that's encourageing to me because I cant see why it doesn't.
adrian
Thank you for giving me the modulo concept. Another reason for using nested arrays is, I wanted to know which orb is at a row end or the beginning of a row or manipulate orbs on a specific row and in my head I thought a nested array would be easy for me to visualise. Still don't understand why orb_strip_matrix[1][3].alpha = 0.9; doesn't work?
adrian