需要帮助了解AddChild方法

时间:2012-01-09 20:33:12

标签: flash actionscript-3 oop class addchild

问题:我想知道为什么我必须在我的文档类和外部类中添加addChild,以便我从我的库中正确链接的scoreBoard影片剪辑出现在舞台上。如果我从任一类中删除addchild,代码将失败。代码工作,我得到适当数量的实例,但我不太明白为什么。我想要做的只是将我想要的scoreBoard类的实例数传递给ScoreBoard类构造函数,并使用外部ScoreBoard类中的for循环创建x实例。相反,我被迫从文档类中执行for循环。任何帮助表示赞赏。

/*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 个答案:

答案 0 :(得分:1)

addChild()将子项添加到父对象的显示列表中。如果我读得正确,上面的代码建立了如下关系:

Base.ScoreBoard.teamScoreBoard

teamScoreBoard必须添加到ScoreBoard或ScoreBoard为空。 ScoreBoard必须添加到Base或Base将是emtpy等...

__

如果你想要这样的关系:

stage.ScoreBoard.teamScoreBoard

在您的基类而不是addChild(s);中,您可以尝试stage.addChild(s);

或在循环内(在构造函数内)声明你的var teamScoreBoards,在循环中将它们添加到Scoreboard,类似于: (未经测试的代码)

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);
    }
}

此外,如果您需要再次访问它们,可以将它们添加到矢量中。

__

如果我错过解释你的问题,请告诉我。