• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Various noob questions, arrays

Explorer ,
Feb 03, 2014 Feb 03, 2014

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

553

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 03, 2014 Feb 03, 2014

The " i " used in the loop is commonly used as a variable name for a loop counter.  In another life/language, "n" used to be the more popular choice.  It could be named just about any non-reserved name you like.  Using " i " keeps it short and sweet.

Arrays start at an index of 0, which is why "i" starts out at 0.  numPegs  dictates how many pegs you intend to create.  Since i starts at 0, as soon as i is 1 less than numPegs, the job is done.

Arrays cannot create content dynamically.  They can sto

...

Votes

Translate

Translate
LEGEND ,
Feb 03, 2014 Feb 03, 2014

Copy link to clipboard

Copied

The " i " used in the loop is commonly used as a variable name for a loop counter.  In another life/language, "n" used to be the more popular choice.  It could be named just about any non-reserved name you like.  Using " i " keeps it short and sweet.

Arrays start at an index of 0, which is why "i" starts out at 0.  numPegs  dictates how many pegs you intend to create.  Since i starts at 0, as soon as i is 1 less than numPegs, the job is done.

Arrays cannot create content dynamically.  They can store references to dynamically created content, but cannot create that content.  The content had to be created else where.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 03, 2014 Feb 03, 2014

Copy link to clipboard

Copied

Thanks for the info, it's much appreciated.

Could you perhaps elaborate on currentRow.push({peg: newPeg, color: 0}); because "peg" and "color" here are not referenced or created earlier in the script.

Is "peg" created/defined as being the number of "newPeg" in the array currentRow, and "color" as being "0" in that same array? If 5 "newPegs" are created, that means 5 "pegs" can be referenced from currentRow? (or something like that)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 03, 2014 Feb 03, 2014

Copy link to clipboard

Copied

In the line...

currentRow.push({peg: newPeg, color: 0});

peg and color are essentially properties of an Object, which you can dynamically define like you see.   The Object is created by using the curly braces { ... }  and the stuff between them are the properties that you define for that Object. 

The Object is a collection of data that represents content that was dynamically created, namely the Peg_mc object that was just created by the variable name newPeg.  peg is just a property of the Object that points to the Peg_mc object that was dynamically added.  color is likely some form of status relative to that Peg_mc object.

The newPeg variable in itself provides somewhat of a temporary identity for the Peg_mc object.  When all is said and done, chances are the rest of the program will be referencing that Object using the array it is being placed in to manipulate the Peg_mc object that it represents, such as....

currentRow.peg.visible = false;     // to make the peg invisible

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 03, 2014 Feb 03, 2014

Copy link to clipboard

Copied

LATEST

Solid info, I think that's becoming more clear now. Thanks a bunch, Ned.

I'm sure I'll have more questions in the near future for you or others to hopefully answer. (I am bookmarking these in a special folder for future reference, too.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines