Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 Is it possible to duplicate a Shape object?

I'm trying to make a shape available for duplicating. Here's an explanation of what I've done, what I'm trying to do, and where I'm stuck:

  • Drew a shape manually in the Flash IDE (paintbrush).
  • Created a new movieclip containing the shape; exports as a class.
  • Instantiate the class (var mc:MovieClip = new shapeMovieClip()).
  • Add a reference to the shape in mc that I want (var myShape:Shape = mc.getChildAt(0) as Shape;
  • This works perfect and I now have my shape, but how can I duplicate it without instantiating the parent MovieClip class - is it possible?

I've tried creating a new shape and using the copyFrom() graphics method with no avail, I believe this method just copies draw calls when they're made on the referenced graphics and instead I just have a reference of a shape that's already been drawn.

Right now I'm extending the movieclip as a sprite instead, instantiating, pulling the shape from the parent and saving its reference, and then nulling the sprite. Does anyone know of a better, more lightweight, strategy for duplicating Shapes in this manner?

like image 270
Carl Miller Avatar asked Nov 19 '25 04:11

Carl Miller


2 Answers

Basically depends on whether you need to scale your shapes. If you don't, and you can work it out with a fixed sized bitmap representation of the shape, then you will get much better performance drawing your shape to a BitmapData (it's called rasterisation) and instanciating Bitmap objects (as other commenters have pointed out). The code would go something like this:

var base:Sprite = new shapeMovieClip();
var bmd:BitmapData = new BitmapData(base.width, base.height, true, 0);
bmd.draw(base);
var clip1:Bitmap = new Bitmap(bmd);
var clip2:Bitmap = new Bitmap(bmd);

If you do need to scale the clips, you will get pixelation using bitmaps. When scaling down Bitmap.smoothing can help to some extent (also when rotating), but if you need to scale up, you will probably have to use some kind of mip-mapping. This is basically creating a few bitmaps of the shape at different scale levels, and then swap them depending on the current scale. Coding this has some complexity (using a helper parent to adjust the scale can help) but it will definitely perform better than using many shape symbols (with or without a sprite parent).

like image 160
Cay Avatar answered Nov 20 '25 18:11

Cay


This is very old, but it still comes up high in Google, so I just wanted to share a true shape duplicating method:

var shapeonstage:Shape = shapeMadeInIDE;
var g:Vector.<IGraphicsData> = shapeonstage.graphics.readGraphicsData();

var shapecopy:Shape = new Shape();
shapecopy.graphics.drawGraphicsData(g)

And boom. It works. Had to share this because it would have helped me a looooong time ago and in so many ways.

UPDATE:

There is some clarification I'd like to add why you would want to use this method, for duplicating AND for storing references to Shapes that are within an swf.

If you target a Shape object on the stage or in a movie clip, the Flash Rendering engine does something strange. It will RECYCLE Shape objects to render new graphics thus making your Shape reference point to a COMPLETELY different rendering.

For example:

  • Create an fla with a movieclip.
  • Inside the movie clip make 10 frames.
  • On each frame, draw a different shape
  • In your code, get a reference to the shape (The Shape on Frame 1)
  • Draw and verify your shape (draw to a bitmap then put the bmp on stage)
  • Now let the flash engine play for 5 frames
  • Draw and verify your shape again

The second time you draw your shape without EVER reassigning your shape reference, will SOMETIMES yield a completely different shape.

Just know, this little quirk can leave you pulling your hair out if you don't know what you're looking for :)

like image 26
Diniden Avatar answered Nov 20 '25 18:11

Diniden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!