Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 - How to copy sprites / graphics of sprites?

Let's say I have a 2D level that is build out of 2D blocks. Some of them are boxes.

The boxes look just the same. No difference! How can I "copy" or clone the graphics of one box to another ? The only difference the boxes will have is that sprite.x and sprite.y have different values. I would probably go that way:

public static function drawBox(graphics:Graphics):void
{
    graphics.clear();
    // draw box
}

drawBox(box1.graphics);
drawBox(box2.graphics);
drawBox(box3.graphics);

No textures will be used, only vector drawing!

Is this a good practice ? Is there another way to achieve the same ?


Update: Sometimes I draw sprites randomly (very hard to redraw them if I need many instances of one sprite and all its attributes).
like image 769
n4pgamer Avatar asked Feb 16 '23 16:02

n4pgamer


1 Answers

You can use the function copyFrom.

Something like this:

var s:Sprite = new Sprite();
s.graphics.beginFill(0);
s.graphics.drawRect(0, 0, 100, 100);
s.graphics.endFill();
addChild(s);

var s2:Sprite = new Sprite();
// Copyfrom accepts a `Graphics` object.
s2.graphics.copyFrom(s.graphics);
s2.x = 100;
s2.y = 100;
addChild(s2);

Have a look at the documentation about copyFrom().

like image 149
putvande Avatar answered Mar 25 '23 09:03

putvande