AS3 lightBox class
This is not ready but I would like to share it anyways, for two reasons: I couldn’t find a free class (or didn’t look hard enough) and someone else might be looking for it. And also so people can take a look at my code and point something that could be improved.
I will post the final version once it is ready.
LightBox.as
//AS3/////////////////////////////////////////////////////////////////////////// // // FlashInit.com // //////////////////////////////////////////////////////////////////////////////// package { /** * LightBox Class * * @langversion ActionScript 3 * @playerversion Flash 9.0.0 * * @author Pedro Canterini * @since 22.04.2009 */ import flash.display.Sprite; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.DisplayObject; import flash.events.Event; import flash.events.ProgressEvent; import flash.net.URLRequest; public class LightBox extends Sprite { //--------------------------------------- // PRIVATE VARIABLES //--------------------------------------- private var _imageHolder:Sprite; private var _loader:Loader; /** * @constructor */ public function LightBox(url:String) { trace("lightbox is on! load: ", url); var req:URLRequest = new URLRequest(url); _loader = new Loader(); _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true); _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress, false, 0, true); _loader.load(req); } //--------------------------------------- // PUBLIC METHODS //--------------------------------------- public function alignToCenter(object:DisplayObject):void { object.x = (object.stage.stageWidth - object.width) / 2; object.y = (object.stage.stageHeight - object.height) / 2; } //--------------------------------------- // PRIVATE METHODS //--------------------------------------- private function handleProgress(event:ProgressEvent):void { var percent:uint = int((event.bytesLoaded / event.bytesTotal) * 100); trace(percent); } private function onComplete(event:Event):void { _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete); handleImage(); } private function handleImage():void { /* create holder for the bitmap */ _imageHolder = new Sprite(); addChild(_imageHolder); _imageHolder.addChild(_loader); /* align object */ alignToCenter(_imageHolder); } } }
in order to use it just create a new Instance of it:
var lightBox:LightBox = new LightBox("assets/imgs/retratos/img-001.jpg"); addChild(lightBox);
I am working on the background and a loader and I will post as soon as they are done and integrated.
Post the final when you get a chance. I’d love to see a working demo.