View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2005-09-18 #26 Old  
Bitmap Tile Example
Hi Guys.


Here's an example of how to create a tiled bitmap in Flash (as requested by MYTiX).

Here's the important part...

ActionScript Code:
  1. import flash.display.BitmapData;
  2. import flash.geom.Rectangle;
  3. import flash.geom.Point;
  4.  
  5. function createTiles(target:BitmapData, tileID:String):Void {
  6.     var tile_bmp:BitmapData = BitmapData.loadBitmap(tileID);
  7.     var tW:Number = tile_bmp.rectangle.width;
  8.     var tH:Number = tile_bmp.rectangle.height;
  9.     var tX:Number = Math.ceil(target.rectangle.width / tW);
  10.     var tY:Number = Math.ceil(target.rectangle.height /  tH);
  11.     for(var x:Number = 0; x < tX; x++) {
  12.         for(var y:Number = 0; y < tY; y++) {
  13.             var rect:Rectangle = new Rectangle(0,0,tW,tH);
  14.             var point:Point = new Point(tW*x,tH*y);
  15.             target.copyPixels(tile_bmp, rect, point);
  16.         }
  17.     }
  18. };
...and this is an example of how to use it...

ActionScript Code:
  1. // CREATE AN EMPTY MOVIECLIP TO USE AS A CONTAINER
  2. var container_mc:MovieClip = this.createEmptyMovieClip("container_mc", 0);
  3.  
  4. // CREATE A NEW BITMAP
  5. var target_bmp:BitmapData = new BitmapData(600, 300, false, 0);
  6.  
  7. // ATTACH THE BITMAP TO THE CONTAINER
  8. container_mc.attachBitmap(target_bmp,0);
  9.  
  10. // CALL THE createTiles FUNCTION SO THAT THE CONTAINER BITMAP
  11. // IS TILED WITH A PNG (etc) FROM THE LIBRARY
  12. createTiles(target_bmp, "source3BMP");
source3BMP is the linkage name I gave a PNG in the library when I was testing the function, this can obviously be changed to whatever you want.

Hope that's of some use.
Reply With Quote