views:

1065

answers:

3

If I have a sprite, with which I have drawn some stuff, how do I erase part of what I have drawn? Preferably I could use drawRect with some "alpha=0" paint.

However, I don't believe beginFill lets you set an rgba color (like you can in bitmapdata). Setting alpha = 0 in beginFill doesn't really do anything -- it just draws nothing.

In my particular use case, masking isn't an option.

Also, calling clear() isn't a good solution since it clears away everything.

+1  A: 

Unfortunately, you cannot do exactly what you're hoping to do with the Graphics class. Though erasing could mean that you draw over something you've already drawn with the background color, I'm guessing you're hoping to "draw transparency" back onto the Graphics object. Drawing with an alpha of 0 is not drawing "nothing" - you just don't see what you draw because it is fully transparent.

This is what you get for working with vectors as opposed to bitmaps. To "erase" part of a vector means that you are creating an entirely new vector, something that's going to take some computation rather than just setting some pixel to a certain color value. Graphics doesn't provide such advanced functionality, though you could certainly write your own functions to do it. :P

One workaround is to use bitmaps instead:
http://www.actionscript.org/forums/showthread.php3?t=187857
http://www.actionscripts.org/forums/showthread.php3?t=149021

Another is to consider whether you could implement what you're trying to do in a different way; one that would lend itself to using clear().

Stiggler
A: 

If you are going to wish to keep part of your drawing while erasing some other parts then the only feasible route would be to draw using several sprites. Create a sprite for each part of the drawing, then you can pick the sprite you wish to erase and leave the rest.

Other than that all you are left with is using the bitmapdata to copy sections then after using clear draw the copied sections back again. (this would be difficult to implement in a complex manor, try copying just a complex curved area!)

kenneth
A: 

You can achieve this nicely if you don't need to be able to interact with any elements underneath your Sprite.

To do this

  1. create a Shape object in the shape you want and set its cacheAsBitmap property to true
  2. set your Sprite's cacheAsBitmap property to true
  3. set the blendMode property of your Shape to BlendMode.ERASE
  4. add the Shape to your Sprite's display list
Ross Henderson