ViewHelper in Actionscript 3.0

April 28th, 2008 § 0

I always found the MovieClipHelper.as of the Pixlib framework useful for rolling out applications in a quick and straightforward manner.  Although the newest iteration of the framework  LowRA is a useful and powerful xml driven environment to roll out RIAs, I still find the need for a manager to hold references to all my views.  The power of a centralized repository of all views and standard methods for x, y manipulation is great to have around for any project.  I've also been looking through the Vegas framework and was unable to find a View Management tool along the same lines.  So I decided to roll my own using Pixlib as a basis and Vegas as my environment.   This little class does most of the things you'll find in the Pixlib version like holding all view references in a hash map.  This design principle makes managing views incredibly easy.  By just defining a single enum file of static strings, you can uniquely name all the views of your application and then subsequently access those views from anywhere in the application.  I find this useful when managing sizing and positioning of multiple views that are dependent upon one another.  So herein follows a quick and dirty ViewManager designed for Sprites in AS3. 

 
package com.extralongfingers.definitionalliterature.client.util
{
	import flash.display.DisplayObject;
 
	import vegas.data.map.HashMap;
 
	import flash.geom.Point;
	import flash.events.EventDispatcher;
	import flash.display.Sprite;
 
	/**
	 * @author Gregory Sogorka
	 */
	public class ViewHelper
	{
		public var view 			: Sprite;
		public var _dispatcher  	: EventDispatcher = new EventDispatcher();
		private static var _a 		: HashMap = new HashMap();
		private var _sName			: String;
 
		function ViewHelper( s : String, spr : Sprite )
		{
			if( spr == null ) trace( " You must pass a Sprite to the constructor.");
 
			else
			{
				view = spr;
				_setName( s );
			}
		}
 
		public function setVisible( b : Boolean ) : void
		{
			if ( b )
			{
				show();
			}
			else
			{
				hide();
			}
		}
 
		public function hide() : void
		{
			view.visible = false;
		}
 
		public function show() : void
		{
			view.visible = true;
		}
 
		public function move( x : Number, y : Number ) : void
		{
			view.x = x;
			view.y = y;
		}
 
		public function getPosition() : Point
		{
			return new Point( view.x, view.y );
		}
 
		public function setSize( w : Number, h : Number ) : void
		{
			view.width = w;
			view.height = h;
		}
 
		public function getSize() : Point
		{
			return new Point( view.width, view.height );
		}
 
		public function getName() : String
		{
			return _sName;
		}
 
		public function release() : void
		{
			ViewHelper._unregister( _sName );
			_sName = null;
		}	
 
		public function isVisible() : Boolean
		{
			return view.visible;
		}
 
		public static function getMovieClipHelper( sName:String ) : ViewHelper
		{
			if (!ViewHelper._a.containsKey( sName ) )
			{
				trace( "Can't find ViewHelper instance with '" + sName + "' name." );
			}
			return _a.get( sName );
		}
 
		public static function isRegistered( sName:String ) : Boolean
		{
			return ViewHelper._a.containsKey( sName );
		}
 
		//Private methods.
 
		private function _setName( name:String ) : void
		{
			if ( ViewHelper._register( name, this ) ) _sName = name;
		}
 
		private static function _register( sName:String, oHelper:ViewHelper ) : Boolean
		{
			if ( ViewHelper._a.containsKey( sName ) )
			{
				trace( "ViewHelper instance is already registered with '" + sName + "' name." );
				return false;
			}
			else
			{
				ViewHelper._a.put( sName, oHelper );
				return true;
			}
		}
 
		private static function _unregister( sName:String ) : void
		{
			ViewHelper._a.remove( sName );
		}
 
	}
}
 

    If you have any suggestions or improvements that might make this type of thing work even better drop me a line.

Insertion Sort in Actionscript 3

April 8th, 2008 § 0

Started reading the book Introduction to Algorithms by Cormen, Leiserson, Rivet, & Stein.  And figured I would start posting my algorithmic implementations in actionscript for those in the community who are interested.  First up is a quick and dirty implementation of insertion sort in two forms: 1. Sort in ascending order 2. Sort in descending order.  This is pretty simple stuff once you wrap your head around the while loop. I'm interested to see more about how O,  notation, and    notation can help us to determine upper bound running times of recursive operations.  I'll keep you all posted as I discover more on this.  And now for the increasing insertion sort code:  

 
//Insertion Sort
 
var arr : Array    = new Array(4, 3,2324, 28, 999, 821, 423,22, 21, 2, 1);
var j    : Number = 0;
 
for( j; j < arr.length; j++)
{
	var key 	: Number = arr[j];
	var i 		: Number = j-1;
 
	while( i > -1 && arr[i] > key )
	{
		arr[i+1] = arr[i];
		i= i-1;
	}
 
	arr[i+1] = key;
}
 
trace( arr );
 


And the decreasing insertion sort code :

 
 
var arr : Array = new Array ( 1, 2, 3,99, 22, 33, 4, 55, 44545, 23, 4 );
 
for( var j : Number = 0; j< arr.length; j++ )
{
	var key : Number = arr[j];
	var i     : Number = j-1;
 
	while( i > -1 && arr[i] < key )
	{
		arr[i+1] = arr[i];
		i= i-1;
	}
 
	arr[ i+1] = key;
}
 
trace( arr );
 

If anyone in the New York City area is interested in jointly studying this topic with me drop me a line.

Where am I?

You are currently viewing the archives for April, 2008 at Meandering Thought.