Archive for April, 2009

How to use Static properties and methods to simplify your tasks in Flash

Posted in Uncategorized on April 25th, 2009 by admin – 3 Comments

Do you write your code over and over every time you need to align an image to the stage center of your project? or maybe for some other task? I know I used to before I learned about static properties and methods, of course I had some snippets for that stored somewhere but still this is not the best solution.

This is very useful because it allows you to use properties from a class without having to import it first. Basically all you need to do is put the word static when declaring the properties and methods inside your class.

Utils.as

package
{	
	/**
	 *  Description
	 *    
	 *  @langversion ActionScript 3
	 *  @playerversion Flash 9.0.0
	 *
	 *  @author Pedro Canterini
	 *  @since  11.04.2009
	 */
 
	import flash.display.DisplayObject;
 
	public class Utils
	{
 
		public static var myStr:String = "My tring here...";
 
		/**
		 *   @constructor
		 */
		public function Utils()
		{
			//super();
		}
 
		//---------------------------------------
		// Align to center
		//---------------------------------------
		public static function alignToCenter(obj:DisplayObject):void
		{
			obj.x = obj.stage.stageWidth / 2 - obj.width / 2;
			obj.y = obj.stage.stageHeight / 2 - obj.height / 2;
		}
 
	}
}

How to use it:

Utils.alignToCenter(theDisplayObject);

Notice that you can align any display object to the center of the stage not just images.

AS3 lightBox class

Posted in AS3 on April 24th, 2009 by admin – 5 Comments

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.

trace(”Hello World!”);

Posted in Uncategorized on April 24th, 2009 by admin – 3 Comments

Hi!

I intent to have this blog as a tool to share my ActionScript 3 experiences, learn something from my erros and hopefully provide and share solutions for others having the same issues that I once had.

Please feel free to say anything and also provide better solutions/improvements for all the code you might find here.

Cheers

-Pedro