Need help understanding AddChild Method

190 views Asked by At

Question: I want to know why I have to add an addChild to both my document class and the External Class in order for my scoreBoard movie clip that I have properly linked from my library to appear on the stage. If I remove the addchild from either class the code fails. The code works and I get the proper amount of instances but I don't quite understand why. What I wanted to do was simply pass the number of instances of the scoreBoard class I wanted to the class ScoreBoard contructor and create x instances using a for loop in the external ScoreBoard class. Instead I have been forced to do the for loop from the document class. Any help is appreciated.

/*This is my Document Class*/

package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.*;// Import statement(s) 
import flash.display.Stage;

//creates the 5 instances of my scoreBoard
public class Base extends MovieClip {

    // Class Constructor
    public function Base() {

        //create an object of our ship from the Ship class

        //add it to the display list
        for(var cnti:int = 0; cnti< 5; cnti++){
        //var myScoreBoard:ScoreBoard = new ScoreBoard();
        var s = new ScoreBoard;
        addChild(s);}
    }
    private function init() {

    }
}
}


 /*This is my external class */
package {
        /*++++++++++++++
        last modified 1/6/2010
        class creates the scoreboard
        */

        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.events.MouseEvent;

        //the class
        public class ScoreBoard extends MovieClip
        {
            private static var score = 0;
            private static var curRow = 0;
            private static var teamCounter = 0;
            private static var teamScoreArray = new Array();

            var teamScoreBoard:MovieClip = new teamScore(); //decalre the name property

            //the constructor
            public function ScoreBoard()
            {   //makes instance of the class


                teamScoreBoard.teamName.text = "Team "+[teamCounter+1];
                teamScoreBoard.name = "Team "+[teamCounter+1];
                teamScoreBoard.score.text = score;
                teamScoreBoard.x = getXCoord();
                teamScoreBoard.y = getYCoord(teamCounter);
                trace(teamCounter);
                teamScoreBoard.plus.addEventListener(MouseEvent.CLICK, plusBtnClickHandler);
                teamScoreBoard.minus.addEventListener(MouseEvent.CLICK, minusBtnClickHandler);    
                teamCounter++;
                this.addChild(teamScoreBoard);


            }//end contructor

            private function getXCoord():int
            {
                var xCoord:int;
                xCoord = 100; 
                return xCoord;
             }

             private function getYCoord(someNum):int
             {
                 var yCoord:int;
                 yCoord = 100 + someNum * 85;
                 return yCoord;
             }

             private function plusBtnClickHandler(event:MouseEvent):void 
            {
                //var   curRow = 0;
                var pointValue = getPointValue(0);
                //incriment the score
                event.currentTarget.parent.score.text = score += pointValue;

            }

            private function minusBtnClickHandler(event:MouseEvent):void 
            {
                //var   curRow = 0;
                var pointValue = getPointValue(0);
                //incriment the score
                //event.currentTarget.parent.score.text = score -= pointValue;
                event.currentTarget.parent.score.text = score -= pointValue;
            }

            private function getPointValue(curRow):int
            {
                var pointValue;
                switch(curRow){
                    case 0: pointValue = 100; break;
                    case 1: pointValue = 200; break;
                    case 2: pointValue = 300; break;
                    case 3: pointValue = 400; break;
                    default: pointValue = 500; break;
                }
                return pointValue;

            }

        }//end class

}//end package
1

There are 1 answers

0
ToddBFisher On

addChild() adds a child to a parent object's display list. If I read correctly, the above code makes a relationship as follows:

Base.ScoreBoard.teamScoreBoard

teamScoreBoard must be added to ScoreBoard or ScoreBoard would be empty. ScoreBoard must be added to Base or Base would be emtpy, etc...

__

If you are wanting a relationship like:

stage.ScoreBoard.teamScoreBoard

In your base class instead of addChild(s); you can try stage.addChild(s);

or declare your var teamScoreBoards inside a loop (inside your constructor), within the loop add them to Scoreboard, something like: (untested code)

public function ScoreBoard() {
    for(var cnti:int = 0; cnti< 5; cnti++){
       var teamScoreBoard:MovieClip = new teamScore();
       teamScoreBoard.teamName.text = "Team "+[teamCounter+1];
       ...
       teamCounter++;
       this.addChild(teamScoreBoard);
    }
}

Additionally you can add them to a vector if you need to access them again.

__

If I miss interpreted your question let me know.