Various noob questions, arrays
Hey all,
this isn't really an important question, but as I'm trying to learn AS3 as best I can, I figured I'd make use of you intelligent people to help me understand things better.
I'm going through Gary Rosenzweig's book (good info, but really hard for me to grasp all the logic and keep track of all the variables), and recently created the "Deduction" game. It works, but I don't feel everything was explained as well as it could have been.
1. Could somebody maybe break down what is happening when the "for" statement for arrays are created?
| private var currentRow:Array; |
//
currentRow = new Array();
for (var i:uint=0; i<numPegs; i++) {
var newPeg:Peg_mc = new Peg_mc();
newPeg.x = i*pegSpacing+horizOffset;
newPeg.y = turnNum*rowSpacing+vertOffset;
newPeg.gotoAndStop(1);
newPeg.buttonMode = true;
newPeg.pegNum = i;
newPeg.addEventListener(MouseEvent.CLICK, clickPeg);
//newPeg.setChildIndex(newPeg, 5); ASK ABOUT ON AS3 FORUM
addChild(newPeg);
allDisplayObjects.push(newPeg);
// record pegs as array of objects
currentRow.push({peg: newPeg, color: 0});
}
Is it something like this: you want a new array, and "for" is used to help define how many objects or "slots" are to be created for that array? (Is i used as the "creation variable" because it's simply convenient?) Below, i starts out at 0, is suppose to be 1 less than the numPegs' (a constant) number, and needs 1 added to the i amount (++) until it equals i<numPegs...
something like that?
2. I vaguely recall the book mentioning that arrays can create content "on the go" or "dynamically" - for instance, currentRow.push({peg: newPeg, color: 0}); creates the "peg" and "color" attributes without needing a definition for them earlier in the script.
Any help would be appreciated. Just realize I'll probably be asking a LOT more questions in the weeks to come.
(I'm trying to create a game of my own.)
