如何在as3中克隆对象

时间:2017-04-21 11:51:37

标签: actionscript-3 actionscript

我对as3很新,但我正在制作的游戏需要使用克隆。我怎么做它们(我知道它涉及使用儿童的东西,但我不知道如何制作它们)?我还需要创建一个功能,将其位置设置为屏幕上的随机位置,我该怎么做?我不确定如何在不移动所有50个位置的情况下引用克隆的x和y位置。 感谢

1 个答案:

答案 0 :(得分:3)

制作任何克隆的最佳方法是将AS3类分配给Library项(假设您将类命名为 SomeThing ),然后使用 new 对其进行实例化运算符并使用 addChild(...)方法添加到显示列表。

import SomeThing;

// Lets create a list to keep things.
var things:Vector.<SomeThing> = new Vector.<SomeThing>;

function addThing():SomeThing
{
    // Create.
    var result:SomeThing = new SomeThing;

    // Put it to a list for further reference.
    things.push(result);

    // Add it to display list.
    addChild(result);

    return result;
}

// Create one thing.
// This one will go to (0,0) coordinates.
addThing();

// You can create several things.
for (var i:int = 0; i < 100; i++)
{
    var aThing:SomeThing = addThing();

    aThing.x = 100 + 200 * Math.random();
    aThing.y = 100 + 100 * Math.random();
}

// Now you can address things via list access.
things[49].x = 50;
things[49].y = 50;