开发者

How to keep both object and reference on stage with AS3?

I have some object, which extends movie cl开发者_如何转开发ip:

public class MyClass extends MovieClip { ... }

Now, I want to put two of this in the stage:

var obj:MyClass = new MyClass();
addChild(obj);

That would put one, but the other I want it to be exactly the same as the first, so I just need to add a "reference" of obj to the stage, along with obj itself.

In the end, I want two objects of type MyClass doing the same thing in the stage. If I try to simply do:

var obj2 = obj;
addChild(obj2);

only obj2 will appear in the stage.

How could I achieve that with references? (in order to save memory and CPU time (it is really important))

Thanks in advance!


There isn't really a good way to do what you want to do. From what I'm reading, you want two objects on the stage....but you want them to do the same thing on the stage, so you actually want only one object.

By design, an object on the display list only has one location. If you have more than one location at the same time, you'll need two objects. There is, however, another way to do this that involves blitting, but you'd have to put the framework in place....but, you can use one object and copy them to multiple places on your stage. If this is the way you want to go, there's a tutorial here.

Good luck.


If you're going to create multiple instance simultaneously, just use a for loop:

var i:uint = 0;
for(i; i<2; i++)
{
    var t:MyClass = new MyClass();

    // properties of t

    addChild(t);
}

If you want to do it later on down the track, you could try something like this:

function getCopy(taget:MyClass, ...attrs):MyClass
{
    var t:MyClass = new MyClass();

    var i:String;
    for each(i in attrs)
    {
        t[i] = target[i];
    }

    return t;
}

The downside is that you have to specify what properties you want to inherit.

var m:MyClass = new MyClass();
m.x = 10;
m.y = 30;

var b:MyClass = getCopy(m, "x", "y");

trace(b.x, b.y); //output: 10, 30


I know this is an old thread, but I was recently struggling to do something similar, and found a couple of workarounds.

As is said here, it is impossible to create multiple copies of a single instance, but it is possible to create a new instance based on an existing instance. You could write a function like this:

public function getClass(obj:Object):Class {

    return Object(obj).constructor;
}

...and then implement it like this:

var seedImage:MovieClip = new SeedImageClass();

var classRef:Class = getClass(seedImage);

var copiedImage:MovieClip = new classRef();

seedImage.x = seedImage.y = 200;
copiedImage.x = copiedImage.y = 300;

stage.addChild(seedImage);
stage.addChild(copiedImage);

...where SeedImageClass would be your AS linkage title for your artwork in Flash. If it isn't animated, you'd be better off using Sprite instead of MovieClip.

The getClass() function would work with any class, not just images.

For my purposes though, this became uneccessary, as I got in the habit of storing whatever "seed images" i might want as Class variable instead of MovieClips, and then instantiating new copies from that.

Here's a simplified example from a game I'm working on:

public class SoldierType {

    public var imageClass:Class;

    public function SoldierType(artReference:Class) {

        this.imageClass = artReference;
    }
}

public class Soldier {

    public var type:SoldierType;
    public var image:MovieClip;

    public function Soldier(typeReference:SoldierType, startX:Number, startY:Number) {            

        this.type = typeReference;
        this.image = new this.type.imageClass();   //where the magic happens
        this.image.x = startX;
        this.image.y = startY;
    }
}

..and it's implemented (more or less) like this, where art.BlueMarine is the AS linkage to the Flash symbol.

var marineType:SoldierType = new SoldierType(art.BlueMarine);

var marine01:Soldier = new Soldier(marineType, 200, 200);
var marine02:Soldier = new Soldier(marineType, 300, 300);

stage.addChild(marine01.image);
stage.addChild(marine02.image);

The point of all that last part is that you might be able to restructure your code to spawn new instances from a Class reference instead of an instantiated MovieClip, which would make your code run faster. If not, the getClass() function can do it.

Best of luck all.


You can't. You have to create 2 instances.


Short answer: You can't, you need two instances. Flash cannot display the same object two different places in the display list.
Long answer: It is possible to use the BitmapData class to take what amounts to a "snapshot" of an item -- it is literally a copy of whatever was being displayed at the time of creation. It could accomplish what you're asking about.


It really depends on what your objects are. If your requirement is to have several objects on stage doing exactly the same thing, the best way to increase performance is to look at the design of the objects itself , rather than trying to escape the fact that you need to duplicate them.

Depending on what your objects are doing, there may not be a need to duplicate the methods of your class, you could call only one method and have the results propagated thru your display objects.


You should deep clone your obj to create a new instance obj2.

Or create a other obj instance like obj2, do the same thing in initialize, then add it to the stage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜