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

时间:2018-07-01 09:30:17

标签: actionscript-3 flash

我正在尝试创建一个小的概念证明,其中将TextField附加到存储在库中的mc并实例化(通过addChild)。我可以使脚本适用于一个实例,但是如果我尝试制作四个副本(如下所示),则仅显示TextField的第三个(?)实例。 (删除动作脚本的图像)谢谢您的时间。

代码如下:

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

1 个答案:

答案 0 :(得分:1)

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

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

另一方面,您在舞台上预先设计的所有内容都作为最终实例存在。如果您设计了一个 TextField ,则实际上只不过一个 TextField

想象一下,您有一个苹果( TextField 实例)和一台生产(新茶碟)纸袋的机器。您制作一个纸袋,然后将苹果放进去。再制作另一个纸袋,然后将苹果放进第二个袋中。现在您得到了图片,对不对?第一个袋子现在是空的,即使您没有明确指定从第一个袋子中取出苹果。

要解决该问题,您需要在库的 Saucer 内已经设计了一个 TextField 。这样,当您实例化新茶碟时,您的 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;