AS3: addChild 库 mc 的多个实例不显示

AS3: addChild multiple instances of library mc not displaying

我正在尝试创建一个小型概念验证,其中将 TextField 附加到存储在库中的 mc 并实例化(通过 addChild)。我可以让脚本为一个实例工作,但如果我尝试制作四个副本(如下所示),则只会显示 TextField 的第三个(?)实例。 (actionscript 的图片已删除)感谢您的宝贵时间。

代码如下:

import flash.display.MovieClip;
flash.display.Sprite;

var i:uint = 0;
var str:String = ""; // to be used to give the textField some values
var aGroup:Sprite = new Sprite(); //used to be some holder other than stage to get all the addChild (don't know if this is absolutely necessary
var aUFO:Array = new Array(); // these are arrays that will hold the instantiated copies of the same object
var aField:Array = new Array();

//main process for creating instances
for (i=0; i<4; i++) {
    var mcUFO:Saucer = new Saucer(); //mcUFO resides in the library with a MovieClip "Saucer" linkage
    aGroup.addChild(mcUFO);
    aUFO[i] = mcUFO;

    aUFO[i].addChild(tFormula); //tFormula is a textField defined elsewhere (previous frame)
    aField[i] = tFormula;

    str = "FillText"+String(i); //used just to temporarily load the textField with some values
    aField[i].text = str;

    aField[i].x -= 21.5; //this is just a rough, quick offset of the textfield to a better postion within the mcuFO
    aField[i].y -= 8;

    var mcDome:Sprite = new Dome(); //mcDome exists in the library with a Sprite linkage
    mcUFO.addChild(mcDome);
    mcDome.x -= 54; //this is just a quick way to relatively reposition the dome over the mcUFO
    mcDome.y -= 35;
}

然后我用一个小代码来重新定位四个实例,使它们形成一个 2x2 堆栈。

for (i=0; i<4; i++) {
    if ((i == 0) || (i ==1)) {
        aUFO[i].x = 200 + i*200;
        aUFO[i].y = 200;
    }else{
        aUFO[i].x = 200 + (i-2)*200;
        aUFO[i].y = 100;
    }
}

stage.addChild(aGroup);//I don't know if this sprite is necessary, but I read somewhere that we're not supposed to add instances directly onto stage, but rather add to a sprite and then addChild that to the stage

this.stop();

你的错误是不了解实例的工作原理。

图书馆不是存储,它就像是蓝图的集合。每次使用 new 运算符时,您都会根据库中的蓝图创建一个新实例(或与任何特定库无关的 class 的新实例对象,就像一个空的 new Sprite 容器)。

另一方面,你在舞台上预先设计的一切都作为最终实例存在。如果你设计了一个TextField,其实只有一个TextField.

假设您有一个苹果(TextField 实例)和一台生产(new Saucer)纸袋的机器。你做了一个纸袋,把苹果放进去。你又做了一个袋子,把苹果放进第二个袋子里。现在你明白了,对吧?第一个袋子现在是空的,即使您没有明确指定从第一个袋子中取出苹果。

要解决这个问题,您需要在库中的 Saucer 中设计一个 TextField。这样,当您实例化一个 new Saucer 时,您也会将 TextField 实例化为新的 Saucer来自图书馆蓝图。给 TextField 一个实例名称(例如 "Formula")并用它来寻址。您的代码将如下所示:

// Content container.
var aGroup:Sprite = new Sprite;

// The list of Saucers.
var aUFO:Array = new Array;

// The list of Saucer's TextFields.
var aField:Array = new Array;

// Instantiation loop.
for (var i:int = 0; i < 4; i++)
{
    // Create a new instance of Saucer object.
    var mcUFO:Saucer = new Saucer; 
    aGroup.addChild(mcUFO);
    aUFO[i] = mcUFO;

    // Obtain the unique TextField reference from the Saucer
    // you are currently working on.
    aField[i] = mcUFO.getChildByName("Formula");

    // Assign text to the TextField.
    aField[i].text = "FillText" + i;

    // No need, TextField is already positioned by design.
    // aField[i].x -= 21.5; // aField[i].y -= 8;

    // Instantiate a Dome from the library
    // (but you can do the same and pre-design Dome into the Saucer).
    var mcDome:Sprite = new Dome;
    mcUFO.addChild(mcDome);

    // Position the Dome.
    mcDome.x -= 54;
    mcDome.y -= 35;
}

或者,您可以全部使用脚本并创建新的 TextField 实例,但请记住,您需要设置该新实例的每个 属性 ,否则您可能根本看不到任何文字或任何内容:

var textArea:TextField;

textArea = new TextField;

textArea.x = 10;
textArea.y = 10;

textArea.border = true;
textArea.wordWrap = false;
textArea.multiline = true;
textArea.selectable = true;
textArea.background = true;

var aFormat:TextFormat;

aFormat = textArea.getTextFormat();
aFormat.font = "_typewriter";
aFormat.size = 12;
aFormat.align = TextFormatAlign.LEFT;

textArea.setTextFormat(aFormat);
textArea.defaultTextFormat = aFormat;