Skip to content

Instantly share code, notes, and snippets.

@blvz
Created Jul 21, 2010
Embed
What would you like to do?
creating large bitmaps as tiles
package {
import flash.display.StageScaleMode;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.text.TextField;
public class Sample extends Sprite {
private var txt : TextField;
private var loader : Loader;
public function Sample() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
txt = new TextField();
addChild(txt);
loadLargeImage();
}
private function loadLargeImage() : void {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loader.load(new URLRequest("http://www.ai-candy.com/images/bye-bye-promo.jpg"));
}
private function loaderProgressHandler(event : ProgressEvent) : void {
txt.text = Math.ceil(event.bytesLoaded / event.bytesTotal * 100) + "%";
}
private function loaderCompleteHandler(event : Event) : void {
removeChild(txt);
drawTiled(loader.content);
}
private function drawTiled(displayObject : DisplayObject) : void {
var tiled : TiledImage = new TiledImage(displayObject);
tiled.scaleX = tiled.scaleY = .15;
addChild(tiled);
}
}
}
package {
import flash.geom.Matrix;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
public class TiledImage extends Sprite {
public function TiledImage(source : DisplayObject) {
if (source)
this.draw(source);
}
private function clear() : void {
if (this.numChildren) {
this.removeChildAt(0);
this.clear();
}
}
private function draw(source : DisplayObject) : void {
this.clear();
var tileSize : int = 400;
var numCols : int = Math.ceil(source.width / tileSize);
var numRows : int = Math.ceil(source.height / tileSize);
var totalWidth : int = Math.ceil(source.width);
var totalHeight : int = Math.ceil(source.height);
var x : int = 0;
var y : int = 0;
for (var row : int = 0; row < numRows; row++) {
x = 0;
totalWidth = Math.ceil(source.width);
var h : int = Math.min(totalHeight, tileSize);
totalHeight -= h;
for (var col : int = 0; col < numCols; col++) {
var w : int = Math.min(totalWidth, tileSize);
totalWidth -= w;
var bmpData : BitmapData = new BitmapData(w, h);
var matrix : Matrix = new Matrix();
matrix.scale(source.scaleX, source.scaleY);
matrix.translate(-x, -y);
bmpData.draw(source, matrix);
var bmp : Bitmap = new Bitmap(bmpData);
bmp.x = x;
bmp.y = y;
x += w;
this.addChild(bmp);
}
y += h;
}
}
}
}
@blvz
Copy link
Author

blvz commented Jul 21, 2010

I needed to draw a bitmap, but Flash has some limitations on doing so. I coded this to break an image in tiles, so Flash can render it properly.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment