PDA

View Full Version : [AS3]Using bitmapData.draw and clipRect for tiles is problematic



flashmxboy
July 20th, 2009, 05:35 PM
Hi again

I ran into some problems while splitting my big level into tiles.
I use 10 bitmapData`s to split my level in 10 pieces and clipRect to draw only what i want to.
So i use this code:
var tile1:BitmapData = new BitmapData(grounder.width / 10,grounder.height,true,0x00FFFFFF);
var tile2:BitmapData = new BitmapData(grounder.width / 10,grounder.height,true,0x00FFFFFF);
//etc for the rest of the tiles
//Then some clipRects:
var tile1Rect:Rectangle = new Rectangle(ground.x, ground.y, ground.width/10,ground.height);

var tile2Rect:Rectangle = new Rectangle(tile1Rect.x + tile1Rect.width, tile1Rect.y, ground.width/10, ground.height);
//Etc for the rest of the tiles
//Then i draw out the first tile:
tile1.draw(ground, null,null,null,tile1Rect);

So far the code works fine:
http://nettvei.com/correctDraw.JPG
but when i use this code to draw the next tile:
tile2.draw(ground,null,null,null,tile2Rect,true);
//I set a red square to indicate where the tile rectangle is
redSquare.x = tile2Rect.x;
redSquare.y = tile2Rect.y;
redSquare.width = tile2Rect.width;
redSquare.height = tile2Rect.height;
Then this happens:
http://www.nettvei.com/wrongDraw.JPG

Pretty strange huh?

This is the ground moveiclip im drawing:
http://nettvei.com/actualDraw.JPG
Se any differences?:P

Anyway..what am i doing wrong?

Thanks in advance

therobot
July 20th, 2009, 06:17 PM
the clipRect doesn't work as you'd expect it, hey?

it looks like the x & y properties of the clip rectangle are where you want to paste your draw region into your bitmap, and the width & height of the rectangle specify how big of a region to copy from the source movieclip.

You should be able to work around this, but you will want to put your terrain movieclip inside another movieclip. Everytime you want to draw your clip to a new bitmap, you should slide the terrain movieclip over a certain number of pixels. Does this make sense?

I can probably whip something up really fast if you're having a hard time.

therobot
July 20th, 2009, 06:37 PM
I've attached an fla that should hopefully resolve your problems. The important thing to pay attention to is the hierarchy of the movieclips, and the nested for loop that systematically breaks the movieclip down into bitmaps.

Cheers!


the fla was made in cs3 with as3 (it's in the .zip)

TOdorus
July 20th, 2009, 07:28 PM
Why not use copypixels instead? When you're not modifying the source bitmapData it's a lot cheaper alternative to draw.

Basicly the rectangle in the draw function should be the portion of the source bitmapData you want to copy. I expect that you want the whole bitmapData to be drawn? In that case (as the livedocs say), you don't even have to define the rectangle.

If you'd use copypixels you would do it something like this.



//the topleft position of the screen
var screenX:int
var screenY:int
//the topleft position of a tile
var tileX:int
var tileY:int
//
//now your tile needs to be drawn relative to the screen position. So say your tile is at x = 100. If your screenX = 30, then that tile should be drawn 100- 30 = 70 pixels to the right on your screen.
//
var whereToDrawOnScreen:Point = new Point(tileX-screenX, tileY-screenY)
copyPixel(tileBitmapData, tileBitmapData.rectangle, whereToDrawOnScreen)


EDIT:
or are we talking how to draw the movieclip to the bitmapdata?

therobot
July 20th, 2009, 07:40 PM
or are we talking how to draw the movieclip to the bitmapdata?

My understanding is that flashmxboy wanted to draw his levels in flash as a movieclip, then convert that data to a series of bitmapdata tiles. From there, copyPixels would be the way to go once you have the tiles, but until then, I think draw() is the only way for his method.

Edit:
if you remember, from an earlier thread, he wanted to do it this way, but there were limitations on the overall size of bitmapdata (without using the workaround todorus mentioned)