For loop 如何使用eventlisteners获取加载movieclips的循环

For loop 如何使用eventlisteners获取加载movieclips的循环,for-loop,actionscript-3,actionscript,addeventlistener,For Loop,Actionscript 3,Actionscript,Addeventlistener,我希望场景加载5个不同的电影剪辑(命名为B1-B5)。每个电影剪辑都放置在特定的x和y轴上。每个电影剪辑在翻滚/翻滚时都会增大/缩小 我通过键入所有内容并每次复制每个部分使代码正常工作,但这很混乱,我希望通过使用循环来清理代码(如果可能的话) 这是有效的代码,但我必须复制每个电影剪辑(更改明显的位) 下面是我尝试过的,但已经被困了很长一段时间 for (var i:int=1; i<5; i++){ var scene[i]:MovieClip = new "B"+i();

我希望场景加载5个不同的电影剪辑(命名为B1-B5)。每个电影剪辑都放置在特定的x和y轴上。每个电影剪辑在翻滚/翻滚时都会增大/缩小

我通过键入所有内容并每次复制每个部分使代码正常工作,但这很混乱,我希望通过使用循环来清理代码(如果可能的话)

这是有效的代码,但我必须复制每个电影剪辑(更改明显的位)

下面是我尝试过的,但已经被困了很长一段时间

for (var i:int=1; i<5; i++){
    var scene[i]:MovieClip = new "B"+i();
    addChild("scene"+i);
    //var scene[i]:MovieClip = new B[i]();
    scene[i].addEventListener(MouseEvent.MOUSE_OVER, onRollOverEvent);
    scene[i].addEventListener(MouseEvent.MOUSE_OUT, onRollOutEvent)

    function onRollOverEvent(e:MouseEvent) {
    scene[i].width=25.9;
    scene[i].height=25;
 }
    function onRollOutEvent(e:MouseEvent) {
    scene[i].width = 20.9;
    scene[i].height = 20;
 }
}

scene1.x = 170.30;
scene1.y = 231.15;
scene2.x = 284.30;
scene2.y = 250.75;
scene3.x = 377.30;
scene3.y = 280.15;  
scene4.x = 444.30;
scene4.y = 321.15;
scene5.x = 196.30;
scene5.y = 172.15;

for(var i:int=1;i首先,让我们检查一下您的错误

new "B"+i();
最好是将一个数字i作为函数调用,并将结果作为字符串添加到“B”中。但是,即使是新的“B1”()也不同于新的B1()。事实上,有一个方法getDefinitionByName(…)允许通过名称对类进行寻址,但我不建议使用它,因为它是高级主题

var scene[i]:MovieClip
你不能用这种方式定义变量scene1scene2等等。实际上你能设计的最接近方括号的符号是:这个[“场景”+i]=…

addChild("scene"+i);
for (...)
{
    ...
    function onRollOverEvent(e:MouseEvent)
    ...
}
参数必须是显示对象实例,而不是字符串

addChild("scene"+i);
for (...)
{
    ...
    function onRollOverEvent(e:MouseEvent)
    ...
}
不要在其他函数或循环中定义函数

scene[i].width = 20.9;
scene[i].height = 20;
在循环结束时,i将等于5,那么,您认为这样的记录将解决什么问题

然后,解决方案

当您将工作解决方案扩展到多个实例时,您需要使用算法。循环和数组是您的朋友

// Lets devise a list of classes and (x,y) coordinates.
var Designs:Array = [
    null, // the 0-th element
    {id:B1, x:170, y:230},
    {id:B2, x:285, y:250},
];

for (var i:int = 1; i < Design.length; i++)
{
    // Retrieve a record for the future object.
    var aDesign:Object = Designs[i];

    // Get a reference to the object's class.
    var aClass:Class = aDesign.id;

    // Create the object. Yes, you CAN omit () with
    // the "new" operator if there are no mandatory arguments.
    var aThing:Movieclip = new aClass;

    // Set coordinates from the design record.
    aThing.x = aDesign.x;
    aThing.y = aDesign.y;

    // Add to the display list.
    addChild(aThing);

    // Subscribe the event handlers.
    aThing.addEventListener(MouseEvent.MOUSE_OVER, onOver);
    aThing.addEventListener(MouseEvent.MOUSE_OUT, onOut);

    // Save the object's reference for the later use.
    // If you'd need to address, say, 3rd object,
    // you do it as following:
    // Designs[3].instance
    aDesign.instance = aThing;
}

function onOver(e:MouseEvent):void
{
    // You subscribed all of the objects to this one event handler.
    // This is the correct way to learn, which one of the objects
    // is under the mouse and is dispatching the said event.
    var aThing:MovieClip = e.currentTarget as MovieClip;

    // Change the object's size.
    aThing.width = 26;
    aThing.height = 25;
}

function onOut(e:MouseEvent):void
{
    // Get the source of the dispatched event.
    var aThing:MovieClip = e.currentTarget as MovieClip;

    // Change the object's size.
    aThing.width = 21;
    aThing.height = 20;
}
//让我们设计一个类和(x,y)坐标的列表。
变量设计:数组=[
null,//第0个元素
{id:B1,x:170,y:230},
{id:B2,x:285,y:250},
];
对于(变量i:int=1;i
Hi Organia,很抱歉回复太慢了。我只记得我的密码x.x!非常感谢!我的密码正常工作,在记住密码的同时,我设法让我的代码的其他部分正常工作。我尝试了你在这里列出的一些东西,但不知道如何组合,因为我尝试了这么多组合,不值得在这里尝试。多亏了你在这里键入的内容,我已经确认了我的错误所在。