AS3 - How to copy sprites / sprite graphics?

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

The boxes look the same . No difference! How can I "copy" or clone graphics from one window to another? The only values ​​that the fields will have are that sprite.x and sprite.y have different meanings. I would probably go as follows:

 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 any other way to achieve the same?


Update: Sometimes I draw sprites randomly (it is very difficult to redraw them if I need many instances of one sprite and all its attributes).
+4
source share
2 answers

You can use the copyFrom function.

Something like that:

 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); 

See the documentation for copyFrom () .

+7
source

If you treat it like an object in your game, most likely you should consider the proposed @Pier OOP approach.

This comes in many flavors:

You can extend the class from Sprite and draw a window as soon as the ADDED_TO_STAGE field is ADDED_TO_STAGE its parent.

 public class box extends Sprite { protected var _color:uint; protected var _size:int; public function box(size:int=100, color:uint=0x000000) { super(); _size = size; _color = color; this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } protected function onAddedToStage(event:Event):void { this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); draw(); } protected function draw():void { this.graphics.beginFill(_color); this.graphics.drawRect(0,0,_size,_size); this.graphics.endFill(); } } 

This block can be created / created by calling:

 var b:box = new box(); this.addChild(b); 

Or you can let the box contain itself - which could be more doable if you are dealing with a large number of objects. The box just needs a reference to its parent, then - and, of course, it must provide the dispose () function -

  public class box { private var _parent:Sprite; protected var s:Sprite; public function box(parent:Sprite) { _parent = parent; s = new Sprite(); s.graphics.beginFill(0x00000); s.graphics.drawRect(0,0,100,100); s.graphics.endFill(); _parent.addChild(s); } public function dispose():void { _parent.removeChild(s); } } } 

In this case, you should build the window as follows: it requires a link to Sprite (or any extension) that has already been added to the scene:

 var b:box = new box(this); 

In both cases, you can dynamically change attributes and make the object more universal:

 public function set size(val:int):void { _size = val; draw(); } public function set color(val:uint):void { _color = val; draw(); } 
+1
source

Source: https://habr.com/ru/post/1495145/


All Articles