Aenil
Aenil

Reputation: 13

as3 image over another using bitmap

Ok so im trying to load a bitmap image as a background using this:

screenBitmapData.copyPixels(tilesBitmapData,new Rectangle(sourceX,sourceY,tileSize,tileSize),new Point(destX,destY));
screenBitmap = new Bitmap(screenBitmapData);
addChild(screenBitmap);

This load my tiled map correctly and display it on the screen.

Now i want to add another image which will be used as my character and the frame displaying it contain my character movement and then display the image as follow:

screenBitmapData.copyPixels(playerSheetBitmapData, new Rectangle(currentSpriteColumn * CHAR_SPRITE_WIDTH, currentSpriteRow * CHAR_SPRITE_HEIGHT, CHAR_SPRITE_WIDTH, CHAR_SPRITE_HEIGHT), new Point(xPos, yPos), null, null,true);

I setted the alpha channel on my character image and this is the result when im moving around my map with it:

http://snag.gy/L2uuR.jpg

As you can see, the background image doesnt refresh. And i simply dont know how to do this. Im pretty new with flash and as3 and ive been trying for days to make it work. I know it have something to do with the copypixel or redrawing the background before i draw the sprite again... Any ideas?

Upvotes: 0

Views: 421

Answers (1)

Marty
Marty

Reputation: 39466

You need to redraw the entire scene. All you're doing at the moment is drawing the player ontop of the result of your previous draw.

All you need to do in your case is draw the entire background each frame before you draw the character. It could look like this:

function renderScene():void
{
    // Draw the background, which will replace all the current graphics
    // on the Bitmap.
    screenBitmapData.copyPixels(tilesBitmapData,new Rectangle(sourceX,sourceY,tileSize,tileSize),new Point(destX,destY));

    // Then draw the player.
    screenBitmapData.copyPixels(playerSheetBitmapData, new Rectangle(currentSpriteColumn * CHAR_SPRITE_WIDTH, currentSpriteRow * CHAR_SPRITE_HEIGHT, CHAR_SPRITE_WIDTH, CHAR_SPRITE_HEIGHT), new Point(xPos, yPos), null, null,true);
}

To actually clear the Bitmap though, you can just use fillRect() to fill it with a color (e.g. black):

// Fill the Bitmap with black.
screenBitmapData.fillRect( screenBitmapData.rect, 0x000000 );

Upvotes: 1

Related Questions