如何在as3中克隆对象

时间:2022-05-10 19:56:36

I'm quite new to as3, but the game I'm making requires the use ofclones. How would I make them (I know it involves using the child thing but I don't know how to make them)? I also need to make a function that sets their location to random places on screen, how would I do this? I'm not sure how i would refer to the clone's x and y locations without it moving all 50 of them. Thanks

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

1 个解决方案

#1


3  

The best way to make clones of anything is to assign AS3 class to the Library item (lets say you named the class SomeThing) and then instantiate it with the new operator and add to display list with addChild(...) method.

制作任何克隆的最佳方法是将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;

#1


3  

The best way to make clones of anything is to assign AS3 class to the Library item (lets say you named the class SomeThing) and then instantiate it with the new operator and add to display list with addChild(...) method.

制作任何克隆的最佳方法是将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;