package com.mindfock.display { import flash.display.Bitmap; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; /** * ... * @author John del Rosario * * Creates an object with a tiled image from an external source. * After creating, the TiledBG object can be resized and a new pattern can be loaded and automatically updates the tile. */ public class TiledBG extends Sprite { private var bg:Sprite; private var bm:Bitmap; private var loader:Loader; private var _patternUrl:String; private var _tileWidth:Number; private var _tileHeight:Number; /** * constructor * @param patternUrl: url of image to be tiled * @param w: initial width of the tile * @param h: initial height of the tile */ public function TiledBG(patternUrl:String, w:Number, h:Number) { _patternUrl = patternUrl; _tileWidth = w; _tileHeight = h; init(); } private function init():void{ newPattern(_patternUrl); } /** * public instance methods */ /** * loads a new image for the source of the pattern * @param patternUrl: url of image to be tiled */ public function newPattern(patternUrl:String):void{ loader = new Loader(); loader.load(new URLRequest(patternUrl)); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded); } /** * redraws the TiledBG object, updating width and height. * @param newWidth * @param newHeight */ public function redrawBG(newWidth:Number = 0, newHeight:Number = 0):void{ if(newWidth != 0 || newHeight != 0){ _tileHeight = newHeight; _tileWidth = newWidth; } bg.graphics.clear(); bg.graphics.beginBitmapFill(bm.bitmapData); bg.graphics.drawRect(0, 0, _tileWidth, _tileHeight); bg.graphics.endFill(); } /** * private instance methods */ private function drawBG():void{ bg = new Sprite(); bg.graphics.beginBitmapFill(bm.bitmapData); bg.graphics.drawRect(0, 0, _tileWidth, _tileHeight); bg.graphics.endFill(); addChild(bg); } private function loaded(event:Event):void{ bm = new Bitmap(event.target.loader.content.bitmapData); drawBG(); } /** * Getters and Setters */ public function get tileWidth():Number { return _tileWidth; } public function set tileWidth(value:Number):void { _tileWidth = value; redrawBG(); } public function get tileHeight():Number { return _tileHeight; } public function set tileHeight(value:Number):void { _tileHeight = value; redrawBG(); } } }