[SOLVED] Make specific pixels of a sprite transparent


(B6ka) #1

Hello fellow FP aficionados,

Is it possible to make specific pixels of a sprite transparent or simply delete them? I am thinking of a simple entity with an animated sprite as its graphic. I would like the transparent/deleted pixel to be removed from all frames. The removed pixel may be random and can be on the edge or between other pixels. I do not use masks, I simply use hit-boxes, so it does not matter for collision detection. I would appreciate any suggestions.


(MartĂ­ Angelats i Ribera) #2

It’s not too hard.

  1. Save in a variable the BitmapData that you want pass to the Spritemap (do it using FP.getBitmap).
  2. Pass that variable to the Spritemap in the contructor.
  3. Modify that BitmapData as you want, it will modify the buffer of the Spritemap as well.

(MartĂ­ Angelats i Ribera) #3

And to modify that BitmapData as you want you need a simple code:

public static function deleteRandomPixel(source:BitmapData, tileWidth:uint, tileHeight:uint, wTiles:uint, hTiles:uint):void
{
var x:uint = FP.rand(tileWidth);
var y:uint = FP.rand(tileHeight);
var w:uint = wTiles;
var h:uint;

while (w--)
{
h = hTiles;
while (h--)
{
source.setPixel32(w*tileWidth+x, h*tileHeight+y, 0);
}
}
}
  • source is the bitmap to be modified
  • tileWidth is the width of the tile in pixels
  • tileHeight is the height of the tile in pixels
  • wTiles is the number of tiles (horitzontaly)
  • hTiles is the number of tiles (vertically)

(B6ka) #4

Thank you very much (once again :slight_smile: ) It works very well. Here is the simple code for the entity that gradually disappears pixel by pixel in case anybody is interested in the future. Note that I used setPixel32 instead of setPixel for the BitmapData to make the pixels transparent.

ETester.as (1.1 KB)

The spritemap is separately stored in Assets class in a standard way:

[Embed(source = ‘…/assets/sprites/tester.png’)] public static const TESTER:Class;


(MartĂ­ Angelats i Ribera) #5

I’m glad it helped.

Oh yeah wrong function, but you got the idea :stuck_out_tongue:

Why are you saying that the spritemap is at the Assets class? It don’t really matter. You only have to have its BitmapData.


(B6ka) #6

Thanks once again. I was trying to make some sort of zombie with a skeleton, so if you shoot it, parts of the flash come off while the bone underneath is revealed. So I was trying to remove random pixels from the top skin sprite. Unfortunately, this does not work very well for the animation when the parts of the body move relative to each other as deleted pixels cover different areas for different frames. At the end I decided to simply animate different levels of damage separately.

I guess I will find a nice way to use the technique anyway :).

In the code I refer to Assets.TESTER, which is a public static constant in Assets class where I keep the png, audio, etc assets. I did not upload that class, so I mentioned that to avoid confusion if somebody decides to use the code.


(MartĂ­ Angelats i Ribera) #7

Oh ok, I get it now.

I recommend you to add “[SOLVED]” at the end or the start of the title so it’s a lot more visible that this is solved (also for future serches). :wink: