as3将多个MovieClip添加到随机MovieClip中

时间:2016-07-16 13:20:48

标签: actionscript-3 flash

我试图在这里做一些动态的MovieClip放置。我已经尝试了几天,但我无法找到正确的方法来完成这个伎俩。这是this的延续。我没有设法让我的MC出现在三角形中,所以我已经制作了一个金字塔的矩形(更适合我的情况,因为我可以改变每一个' em,到更符合我想要的形状 - 一个不那么规则的三角形)。 这里的代码带有一些注释:

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

btn_toys_2.confirm.addEventListener(MouseEvent.MOUSE_UP, confirmToys);

var toysPlc:Array = new Array(); //an array for a bunch of rectangles
var toAdd:int = 100 //this is supposed to be dynamic, user defined
var toy:MovieClip = new shar_001; //an MC from a library

for (var j:int=0; j<33; j++){ 
    toysPlc.push("tPl_" + j); //here I add that bunch of rects into an array
}

function confirmToys(e:MouseEvent):void{
    for (var k:int=0; k<toAdd; k++){ //supposed to add an "toAdd" amount of "toys"
    var p:int = Math.random()*toysPlc.length; //^do so in a random rect
    toysPlc[p].addChild(toy); //supposed to place a toy in a random rect
    toy.x = Math.random()*toysPlc[p].width; //positioning
    toy.y = Math.random()*toysPlc[p].height;  //positioning
    }
}

我得到的错误是:TypeError:错误#1006:value不是函数。

我所管理的是在这些地方随意放置一个玩具,所以我不记得如何:) 提前谢谢!

编辑:null让我澄清案件,所以这里是全局:

我得到了: - 类似三角形的MC(因为三角形MC无论如何都是闪光的矩形,我通过创建一个33个矩形的金字塔,相互分层来解决这个问题); - 玩具(多个框架可随时更换); - 文本字段(输入所需玩具的数量); - 确认按钮(一旦点击就发生魔法);

现在我需要用户在输入字段中输入一个3位数字,按&#34;确认&#34;,该数量的0.4应该出现在&#34;三角形MC&#中34 ;. 例如:用户输入数字:600,结束值为600 * 0.4 = 240。现在240&#34;玩具&#34;在我的33个矩形之间随机扩展,在它们的宽度范围内(每个矩形都不同)。

希望能再解释一下。

我需要的简化示例here

1 个答案:

答案 0 :(得分:2)

  

有没有办法用MovieClip而不是String值填充数组?这就是这个问题的答案。

实际上有不止一种方式:

  • 创建时将所有实例名称放入数组中:

    var rectangles:Array = [instanceName1, instanceName2];
    

    没有引号,这会创建字符串文字。只是名字。

    对于大量对象,这种方法很快变得不切实际。

  • 鉴于实例名称具有数字组件,请与getChildByName()一起遍历名称。我认为这是你在问题中尝试的内容:

    var rectangles:Array = []; // initialise empty array
    
    for (var j:int=0; j<33; j++){ 
        rectangles.push(getChildByName("tPl_" + j));
    }
    

原始答案

 toysPlc.push("tPl_" + j); //here I add that bunch of rects into an array

不,不。您正在使用Array对象填充String。它与任何矩形完全无关。

现在下一行,尝试在每个String上调用一个函数,该函数失败。

toysPlc[p].addChild(toy);

以上相当于

"tPl_0".addChild(toy);
"tPl_1".addChild(toy);
// etc.

String没有该方法addChild