Thursday, June 07, 2007

Flash: Tiling an external image

Here's how to tile an image using MovieClipLoader loadClip, loadMovie, createEmptyMovieClip, and BitmapData.


/**
* Sets the tiled background image for the panel
*
* @param backgroundImageUrl the URL of the image
*/
private function setBackgroundImageUrl(backgroundImageUrl: String): Void {
var imageHolder = this.createEmptyMovieClip('imageHolder', Helper.getNewDepth());
var movieClipLoader = new MovieClipLoader();
movieClipLoader.addListener({onLoadInit: Delegate.create(this, function() {
var tileWidth = imageHolder._width;
var tileHeight = imageHolder._height;
if (tileWidth == 0 || tileHeight ==0) { return; } // Something went wrong [Jon Aquino 2007-06-07]
for (var x = 0; x < Stage.width; x += tileWidth) {
for (var y = 0; y < Stage.height; y += tileHeight) {
if (x == 0 && y == 0) { continue; }
var tile = this.createEmptyMovieClip('tile_' + x + '_' + y, Helper.getNewDepth());
tile._x = x;
tile._y = y;
var bitmapData = new BitmapData(tileWidth, tileHeight, true, 0);
tile.attachBitmap(bitmapData, Helper.getNewDepth());
bitmapData.draw(imageHolder);
}
}
})});
movieClipLoader.loadClip(Config.getInstance().getBackgroundImageUrl(), imageHolder);
}

0 Comments:

Post a Comment

<< Home