问题:我想知道为什么我必须将 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