Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

erasing sprite graphics?

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 the beginFill() method lets you set an RGBA color (like you can in bitmapData). Setting alpha = 0 in the graphics method of 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.

like image 936
jedierikb Avatar asked Dec 07 '22 07:12

jedierikb


2 Answers

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
like image 139
Ross Henderson Avatar answered Dec 19 '22 19:12

Ross Henderson


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().

like image 34
Stiggler Avatar answered Dec 19 '22 21:12

Stiggler