AboutFlex.net

flex,air,flash …

Archive for June, 2008

CursorManager - Change Mouse Cursor

Posted by Fedele Marotti under Flex

This example shows you how to change the cursor using an image, in this case a png;

This movie requires Flash Player 9

the application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="300" viewSourceURL="srcview/index.html">
	<mx:Script>
		<![CDATA[
			import mx.managers.CursorManager;

			[Embed(source="assets/cursor1.png")]
            [Bindable]
			public var Cursor1:Class;      

			[Embed(source="assets/cursor2.png")]
            [Bindable]
			public var Cursor2:Class;      

			[Embed(source="assets/cursor3.png")]
            [Bindable]
			public var Cursor3:Class;      

			[Embed(source="assets/cursor4.png")]
            [Bindable]
			public var Cursor4:Class;      

			[Embed(source="assets/cursor5.png")]
            [Bindable]
			public var Cursor5:Class;      

			private function changeCursor(e:MouseEvent):void{
				CursorManager.setCursor(e.currentTarget.source);
			}
			private function restoreCursor(e:MouseEvent):void{
				CursorManager.removeAllCursors();
			}		     

		]]>
	</mx:Script>

	<mx:Image mouseOver="{changeCursor(event)}" mouseOut="{restoreCursor(event)}" x="314" source="{Cursor1}" verticalCenter="5"/>
	<mx:Image mouseOver="{changeCursor(event)}" mouseOut="{restoreCursor(event)}"  x="274" source="{Cursor2}" verticalCenter="5"/>
	<mx:Image mouseOver="{changeCursor(event)}" mouseOut="{restoreCursor(event)}"  x="154" source="{Cursor3}" verticalCenter="5"/>
	<mx:Image mouseOver="{changeCursor(event)}" mouseOut="{restoreCursor(event)}"  x="234" source="{Cursor4}" verticalCenter="5"/>
	<mx:Image mouseOver="{changeCursor(event)}" mouseOut="{restoreCursor(event)}"  x="194" source="{Cursor5}" verticalCenter="5"/>

</mx:Application>

This example shows you the way of open a browse window with a custom filter for files;

This movie requires Flash Player 9

The application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	creationComplete="{init()}"
	>

	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private var imageFilter:FileFilter;
			private var htmlFilter:FileFilter;
			private var fileReference:FileReference;

			private function init():void{

				imageFilter=new FileFilter("Image Files","*.jpg;*.gif;*.png")
				htmlFilter=new FileFilter("HTML Files","*.html")
				fileReference=new FileReference();
				fileReference.addEventListener(Event.SELECT, fileSelectedHandler);

			}

			private function fileSelectedHandler(e:Event):void{
				if( e.target.type.toString()!='.jpg'&&
					e.target.type.toString()!='.png'&&
					e.target.type.toString()!='.gif'&&
					e.target.type.toString()!='.html'){

					Alert.show("Unexpected File Format","Error");	

				}else{

					Alert.show( "Name : "+e.target.name.toString()+"\n"+
						"Type : "+e.target.type.toString()+"\n"+
						"CreationDate : "+e.target.creationDate.toString()+"\n"+
						"Size : "+e.target.size.toString()+"bites \n",
						"File Informations");
				}
			}
			private function browse(filter:FileFilter):void{

				fileReference.browse([filter]);

			}
		]]>
	</mx:Script>
	<mx:Button label="Browse Images Files" click="browse(imageFilter)"  x="10" y="60" width="150"/>
	<mx:Button label="Browse Html Files" click="browse(htmlFilter)"  y="30" x="10" width="150"/>
</mx:Application>

This example shows you how to set blur, duration, transparency, and transparencyColor to a modal window in Flex

This movie requires Flash Player 9

the Application :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="500" height="500">
	<mx:Script>
		<![CDATA[
			import mx.events.ColorPickerEvent;
			import mx.events.SliderEvent;
			import mx.controls.Alert;

			private function setDuration(e:SliderEvent):void{
				application.setStyle('modalTransparencyDuration',e.value)
			}
			private function setColor(e:ColorPickerEvent):void{
				application.setStyle('modalTransparencyColor',e.color)
			}
			private function setBlur(e:SliderEvent):void{
				application.setStyle('modalTransparencyBlur',e.value)
			}
			private function setTransparency(e:SliderEvent):void{
				application.setStyle('modalTransparency',e.value)
			}
		]]>
	</mx:Script>
	<mx:Style >
	/* CSS file */

	 	global {

            modalTransparencyBlur: 5;
            modalTransparency: 0.8;
            modalTransparencyColor: #666666;
            modalTransparencyDuration: 500;

        }

	</mx:Style>
	<mx:HBox width="100%">
		<mx:Text text="Duration: " width="100"/>
		<mx:HSlider minimum="0" value="500" snapInterval="100" maximum="2000" tickInterval="500"
			change="{setDuration(event)}"/>
	</mx:HBox>
	<mx:HBox width="100%">
		<mx:Text text="Transparency:" width="100"/>
		<mx:HSlider minimum="0" value="0.5"  maximum="1"  snapInterval="0.1" tickInterval="0.1"
			change="{setTransparency(event)}"/>
	</mx:HBox>
	<mx:HBox width="100%">
		<mx:Text text="Blur:" width="100"/>
		<mx:HSlider minimum="0" value="5"  maximum="10"  snapInterval="0.5" tickInterval="1"
			change="{setBlur(event)}"/>
	</mx:HBox>
	<mx:HBox width="100%">
		<mx:Text text="Color:" width="100"/>
		<mx:ColorPicker change="{setColor(event)}"/>
	</mx:HBox>
	<mx:Button label="showAlert!!" click="{Alert.show('Hello world from an Alert!!')}"/>
</mx:Application>

the relevant part of the code :

//how to make with CSS:
global {
            modalTransparencyBlur: 5;
            modalTransparency: 0.8;
            modalTransparencyColor: #666666;
            modalTransparencyDuration: 500;
	}

//the Actionscript way:
application.setStyle('modalTransparencyDuration',500)
application.setStyle('modalTransparencyColor',#666666)
application.setStyle('modalTransparencyBlur',5)
application.setStyle('modalTransparency',0.8)

Related article



This example shows you how to implement the interface that enables you to activate the back button of the browser;In this example the IHistoryManagerClient interface is implemented into a component;
in the same way you can implement it in the application.

Click here to view the example in action

The Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	xmlns:creativesource="it.creativesource.*" width="420"
	height="320" paddingLeft="10" paddingRight="10"
	paddingTop="10" pageTitle="10">

	<creativesource:HistoryManger x="10" y="10" />

</mx:Application>

The Component :

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300"
	initialize="initApp()"
	implements="mx.managers.IHistoryManagerClient" >
	<mx:states>
		<mx:State name="STATE1">
			<mx:SetProperty target="{text1}" name="text" value="State 1"/>
		</mx:State>
		<mx:State name="STATE2">
			<mx:SetProperty target="{text1}" name="text" value="State 2"/>
		</mx:State>
		<mx:State name="STATE3">
			<mx:SetProperty target="{text1}" name="text" value="State 3"/>
		</mx:State>
	</mx:states>
	<mx:Script>
		<![CDATA[
			import mx.events.StateChangeEvent;
			import mx.managers.HistoryManager;

			public function initApp() :void{
				HistoryManager.register(this);
				addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE,handleStateChange);
			}

			private function handleStateChange(e:StateChangeEvent):void {
			    HistoryManager.save()
			}
			public function loadState(historyState:Object) :void{
				removeEventListener(StateChangeEvent.CURRENT_STATE_CHANGE,
				handleStateChange);

				if (historyState == null){
					currentState = "";
				}else{
					currentState = historyState.currentState;
				}

				addEventListener(StateChangeEvent.CURRENT_STATE_CHANGE,
				handleStateChange);

			}
			public function saveState() :Object{
				var historyState:Object = new Object();
				historyState.currentState = currentState;
				return historyState;
			}
		]]>
	</mx:Script>
	<mx:Text text="StartState" y="93" fontSize="18" id="text1" horizontalCenter="0"/>
	<mx:HBox x="96" y="268">

	<mx:Button click="currentState='STATE1'" label="State1"/>
	<mx:Button click="currentState='STATE2'" label="State2"/>
	<mx:Button click="currentState='STATE3'" label="State3"/>
	</mx:HBox>
</mx:Canvas>

This example shows you how to create and center a pop using the PopUpManager Class

This movie requires Flash Player 9

The Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	width="500" height="500"
	layout="absolute" viewSourceURL="srcview/index.html">
	<mx:Script>
		<![CDATA[

			import mx.managers.PopUpManager;

			private var popUp:ImageEx;

			[@Embed('assets/fx_appicon.jpg')]
			private var imgToDisplay:Class

			private function createPopUp():void{

				popUp=ImageEx(PopUpManager.createPopUp( this, ImageEx , true));
				popUp.source=imgToDisplay;
				PopUpManager.centerPopUp(popUp)

			}

		]]>
	</mx:Script>
	<mx:Button label="Click Me!!" click="createPopUp()"  x="210.5" y="468"/>
</mx:Application>

the Component - ImageEx:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox
	width="320" height="350"
	backgroundColor="#FFFFFF"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	horizontalAlign="center"
	paddingBottom="10" paddingLeft="10"
	paddingRight="10" paddingTop="10"
	verticalGap="10">

	<mx:Script>
		<![CDATA[
			import mx.managers.PopUpManager;
			[Bindable]
			public var source:Object;

		]]>
	</mx:Script>

	<mx:Image source="{source}"
		width="300" height="300"
		horizontalCenter="0" top="10"/>
	<mx:Button click="{PopUpManager.removePopUp(this)}"
		label="Close PopUp" x="10" width="300"
		bottom="10" height="20"/>

</mx:VBox>

Related article


This example shows you how to create an Air application without the classical layout;

A part if the xml config file:

<systemChrome>none</systemChrome>
<transparent>true</transparent>

the Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
	xmlns:mx="http://www.adobe.com/2006/mxml"
	backgroundAlpha="0"
	showTitleBar="false"
	showStatusBar="false"
	showGripper="false"
	borderStyle="none"
	layout="vertical" height="136" width="220">
	<mx:Script>
		<![CDATA[
			private function startDragApplication():void{
				stage.nativeWindow.startMove();

			}
		]]>
	</mx:Script>
	<mx:Panel title="Application title" borderAlpha="1" width="100%" height="100%" layout="absolute" />

</mx:WindowedApplication>

The relevant code in the application :

	backgroundAlpha="0"//this sets the alpha of the background of the application to 0
	showTitleBar="false"//this sets the visibility of the titlebar to false
	showStatusBar="false"//this sets the visibility of the statusbar to false
	showGripper="false"//this sets the visibility of the gripper to false
	borderStyle="none"//this sets the style of the border to none

This example shows you how to drag an Air window from its content:

The Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="vertical" height="136" width="220">
	<mx:Script>
		<![CDATA[
			private function startDragApplication():void{
				stage.nativeWindow.startMove();

			}
		]]>
	</mx:Script>
	<mx:Button mouseDown="startDragApplication()" label="Drag Me!!"/>

</mx:WindowedApplication>

the relevant code:

	stage.nativeWindow.startMove();/*tells the nativeWindow to
			follow the mouse;it stops automatically*/


This class uses Google Suggests to fill an array with its suggestions;

Note : this class doesn’t work on a server because of the sandbox violation;
But if you rum the application on your local machine or in an AIR project it works well

The Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
	applicationComplete="init()"
	layout="vertical" xmlns:ns1="it.creativesource.*"
	width="380" height="330">
<mx:Script>
	<![CDATA[
		import it.creativesource.GoogleSuggests;

		private var gs:GoogleSuggester ;

		private function init():void{

			gs=new GoogleSuggester ();
			gs.addEventListener(gs.SUGGESTED,displayResults)
		}

		private function findSuggests(e:KeyboardEvent):void{
			output.htmlText='';
			gs.findSuggests(e.currentTarget.text)

		}
		private function displayResults(e:Event):void{

			for(var i:int=0;i<e.currentTarget.dataProvider.length;i++)
			output.htmlText+=e.currentTarget.dataProvider[i]+'\n';
		}
	]]>
</mx:Script>	

	<mx:FormItem label="Write here :">

		<mx:TextInput keyUp="findSuggests(event)"  x="309" y="77"/>
	</mx:FormItem>
	<mx:FormItem label="Responses :">
		<mx:TextArea id="output" x="309" y="105" height="238"/>
	</mx:FormItem>
</mx:WindowedApplication>

The Class : - GoogleSuggester

package it.creativesource
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	[Event(name="suggested", type="flash.events.Event")]
	public  class GoogleSuggester extends Sprite
	{

		public const SUGGESTED:String='suggested'
		private var _dataProvider:ArrayCollection=new ArrayCollection();

		public function GoogleSuggester ()
		{

		}
		public function findSuggests(string:String):void{
			var hs:HTTPService=new HTTPService();
			hs.url="http://www.google.com/complete/search?hl=en&client=suggest&js=true&q="+string+"&cp="+string.length;
			hs.addEventListener(ResultEvent.RESULT,parseResults)
			hs.send()
		}

		protected function parseResults(e:ResultEvent):void{
			var tmp:Array=e.result.split('new Array')[1].split(', "');
			_dataProvider=new ArrayCollection();
			for (var i:int=1;i<tmp.length-1;i+=2){

				tmp[i]=tmp[i].replace('"',"");
				tmp[i]=tmp[i].replace('\\x27',"'");
				_dataProvider.addItem(tmp[i]);

			}
			dispatchEvent(new Event('suggested'));
		}

		public function get dataProvider():ArrayCollection{
			return _dataProvider;
		}

	}
}

This is an alpha version of a tool developed using Adobe Flex, Adobe Air that uses Google translator services to make the job;

here is the link where you can find the tool

As I said this is an alpha version and it’s name is not definitive;

If you have suggestions or comments about it’s name or it’s features you can write here,
we will be glad to respond and if it’s possible to fulfill your requests;

Cheers,
AboutFlex Team

Alternativa 3D - is ready for download !

Bunker


Alternativa 3D. the promising 3d engine discussed in the previous post here,
now is finally ready!

go there, get an account and download the package for free.
Alternativa 3D website

Adobe Max 2008/2009 is up

Posted by Magie Cannizzaro under AIR, ActionScript 3, Adobe, Events, Flash, Flex

Adobe Max 2008

The new Famous Adobe Max Event is near

this year the great Adobe Community event will be

MAX 2008 North America

San Francisco, California — November 16 - 19, 2008

MAX 2008 Europe

Milan, Italy — December 1 - 4, 2008

MAX 2008 Japan

Tokyo, Japan — January, 2009

All the informations and the Registration for the San Francisco date are opened!

Go and register yourself at http://max.adobe.com.

This example shows you a component that extends a Panel to give it the capacity of the rotation.

This movie requires Flash Player 9

Remember to embed fonts otherwise the text will not be displayed after a rotation

The Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

	 layout="absolute" xmlns:ns1="it.creativesource.*"
	  viewSourceURL="http://www.aboutflex.net/labs/rotablePanel/srcview/index.html">

	<ns1:RotablePanel id="rP" x="99" y="99"
		width="250" height="229" title="Rotate Me!!" >
	</ns1:RotablePanel>

</mx:Application>

the Component - RotablePanel

package it.creativesource
{
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.text.Font;
	import flash.text.TextFormat;

	import mx.containers.Panel;
	import mx.controls.Button;
	import mx.effects.Rotate;

	public class RotablePanel extends Panel
	{

		[Embed(source="/assets/fonts/verdana.TTF", fontName="verdana")]
		public static var verdana:Class;

		private var _rotator:Button=new Button();
		private var _rotator1:Button=new Button();
		private var _rotator2:Button=new Button();
		private var _rotator3:Button=new Button();

		private var _startGap:Number=0;
		private var _direction:Number=1;

		public function RotablePanel()
		{
			Font.registerFont(verdana);
			super();

		}

		override protected function createChildren():void{

			super.createChildren();
			super.rawChildren.addChild(_rotator);
			super.rawChildren.addChild(_rotator1);
			super.rawChildren.addChild(_rotator2);
			super.rawChildren.addChild(_rotator3);
			_rotator.addEventListener(MouseEvent.MOUSE_DOWN,startRotation)
			_rotator1.addEventListener(MouseEvent.MOUSE_DOWN,startRotation)
			_rotator2.addEventListener(MouseEvent.MOUSE_DOWN,startRotation)
			_rotator3.addEventListener(MouseEvent.MOUSE_DOWN,startRotation)

		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{

			_rotator.width=12;
			_rotator.height=12;
			_rotator.x=super.width-_rotator.width+12;
			_rotator.y=super.height-_rotator.height+12;

			_rotator1.width=12;
			_rotator1.height=12;
			_rotator1.x=super.width-_rotator1.width+12;
			_rotator1.y=-12;

			_rotator2.width=12;
			_rotator2.height=12;
			_rotator2.x=-12;
			_rotator2.y=-12;

			_rotator3.width=12;
			_rotator3.height=12;
			_rotator3.x=-12;
			_rotator3.y=super.height-_rotator3.height+12;

			var textFormat:TextFormat = new TextFormat();
			textFormat.font = "verdana";
			titleTextField.embedFonts=true;
			titleTextField.defaultTextFormat=textFormat;

			super.updateDisplayList(unscaledWidth,unscaledHeight);

		}

		private function startRotation(e:MouseEvent):void{

			stage.addEventListener(MouseEvent.MOUSE_MOVE,rotate)
			stage.addEventListener(MouseEvent.MOUSE_UP,stopRotation)

			var radVal:Number = Math.PI * this.rotation / 180;
			var originX:Number=unscaledWidth/2
			var originY:Number=unscaledHeight/2
			var centerX:Number = this.x + originX * Math.cos(radVal) - originY * Math.sin(radVal);
			var centerY:Number = this.y + originX * Math.sin(radVal) + originY * Math.cos(radVal);
			var a: Number = (originY-contentMouseY)
			var b: Number =(originX-contentMouseX)
			var ipo:Number = Math.sqrt((a*a)+(b*b))			

			_startGap=Math.round(180 * Math.asin(a/ipo) / Math.PI)

			if(b<0){
				_direction=-1
			}else{
				_direction=1
			}
		}

		private function stopRotation(e:MouseEvent):void{

			stage.removeEventListener(MouseEvent.MOUSE_UP,stopRotation)
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,rotate)

		}

		private function rotate(e:MouseEvent):void{

			var radVal:Number = Math.PI * this.rotation / 180;
			var originX:Number=unscaledWidth/2
			var originY:Number=unscaledHeight/2
			var centerX:Number = this.x + originX * Math.cos(radVal) - originY * Math.sin(radVal);
			var centerY:Number = this.y + originX * Math.sin(radVal) + originY * Math.cos(radVal);

			var a: Number = (originY-contentMouseY)
			var b: Number =(originX-contentMouseX)
			var ipo:Number = Math.sqrt((a*a)+(b*b))

			if(_direction>0)
				this.rotation+=-_startGap+Math.round(180 * Math.asin(a/ipo) / Math.PI)
			else
				this.rotation+=_startGap-Math.round(180 * Math.asin(a/ipo) / Math.PI)

			radVal = Math.PI * this.rotation/180;
			var originalOffsetX:Number = originX * Math.cos(radVal) - originY * Math.sin(radVal);
			var originalOffsetY:Number = originX * Math.sin(radVal) + originY * Math.cos(radVal);
			var newX:Number = Number((centerX - originalOffsetX).toFixed(1));
			var newY:Number = Number((centerY - originalOffsetY).toFixed(1));

			this.move(newX, newY);

		}
	}
}

other components that extend the Panel

This component is a ButtonBar for the ImageBtn component.

This example shows you an ImageBtnBar in action:

This movie requires Flash Player 9

the Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
	xmlns:creativesource="it.creativesource.*"
	viewSourceURL="srcview/index.html" width="550" height="130">

	<creativesource:ImageBtnBar width="444" direction="horizontal">
		<creativesource:ImageBtn
			id="imageBtn"
			backgroundImg="@Embed('./assets/base.png')"
			rollOverImage="@Embed('./assets/over.png')"
			selectedImage="@Embed('./assets/down.png')"
			enableSelection="true"
			width="130" height="50" />
		<creativesource:ImageBtn
			id="imageBtn1"
			backgroundImg="@Embed('./assets/base.png')"
			rollOverImage="@Embed('./assets/over.png')"
			selectedImage="@Embed('./assets/down.png')"
			enableSelection="true"
			width="130" height="50"
			/>
		<creativesource:ImageBtn
			id="imageBtn2"
			backgroundImg="@Embed('./assets/base.png')"
			rollOverImage="@Embed('./assets/over.png')"
			selectedImage="@Embed('./assets/down.png')"
			enableSelection="true"
			width="130" height="50"
			/>
	</creativesource:ImageBtnBar>

</mx:Application>

the ImageBtnBar:

package it.creativesource
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;

	import mx.collections.ArrayCollection;
	import mx.containers.Box;

	public class ImageBtnBar extends Box
	{

		protected var dataProvider:ArrayCollection
		public function ImageBtnBar(){

			super();
			dataProvider=new ArrayCollection();
		}
		override public function addChild(child:DisplayObject):DisplayObject{
			if(!(child is ImageBtn))
			throw new Error('The child must be an ImageBtn')
			child.addEventListener(MouseEvent.CLICK,deSelectOthers);
			dataProvider.addItem(child)
			return super.addChild(child)

		}

		override public function removeChild(child:DisplayObject):DisplayObject{
			for(var i:int=0;i<dataProvider.length;i++){
				if(child==dataProvider[i])
					removeChildAt(i);
			}
			return super.removeChild(child)

		}
		override public function removeChildAt(index:int):DisplayObject{
			dataProvider.removeItemAt(index)
			return super.removeChildAt(index);	

		}
		private function deSelectOthers(e:MouseEvent):void{

			for(var i:int=0;i<super.numChildren;i++){
				if(dataProvider[i]!=e.currentTarget)
					dataProvider[i].deSelect();

			}
		}

	}
}

the ImageBtn:

package it.creativesource
{
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.Text;

	public class ImageBtn extends Canvas
	{
		public const BASE_STATE:String='BASE';
		public const OVER_STATE:String='OVER';
		public const SELECTED_STATE:String='SELECTED';
		public const STATE_CHANGED:String="stateChanged"
		protected var textField:Text;
		protected var background:Image;
		protected var backgroundOver:Image;
		protected var backgroundSelected:Image;
		private var _state:String=BASE_STATE;

		public function ImageBtn()
		{

			super();
			buttonMode=true;
			background=new Image()
			backgroundOver=new Image()
			backgroundSelected=new Image();
			textField=new Text();
			state=BASE_STATE;
			activate();

		}

		override protected function createChildren():void{

			super.createChildren();
			addChild(background);
			addChild(backgroundOver);
			addChild(backgroundSelected);
			addChild(textField);

		}
		override public function set label(value:String):void{
			textField.text=value;
		}
		[Bindable]
		override public function get label():String{
			return textField.text;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{

			super.updateDisplayList(unscaledWidth,unscaledHeight);

			textField.x=this.width/2-textField.textWidth/2;
			textField.y=this.height/2-textField.textHeight/2;
			textField.mouseChildren=false;
			horizontalScrollPolicy="none"
			verticalScrollPolicy="none"
		}

		public function set enableSelection(b:Boolean):void{
			if(b==true)
				addEventListener(MouseEvent.CLICK,selectBtn)

		}

		public function set backgroundImg(s:String):void{
			background.source=s;
		}

		public function set rollOverImage(s:String):void{
			backgroundOver.source=s;
		}
		public function set selectedImage(s:String):void{
			backgroundSelected.source=s;
		}

		private function handleOut(e:MouseEvent=null):void{
			state=BASE_STATE;
		}
		private function handleOver(e:MouseEvent=null):void{
			state=OVER_STATE
		}

		private function selectBtn(e:MouseEvent=null):void{
			state=SELECTED_STATE
		}

		public function set state(state:String):void{
			switch(state){
				case BASE_STATE:
					setBaseState();
				break;
				case OVER_STATE:
					setOverState();

				break;
				case SELECTED_STATE:
					setSelectedState();
				break;
				default:
					throw new Error('This state does not exist, please use '+BASE_STATE+'or'+OVER_STATE+'or'+SELECTED_STATE)
					setBaseState();
				break;
			}
			_state=state
		}
		public function get state():String{
			return _state
		}
		private function setBaseState():void{
			background.visible=true
			backgroundOver.visible=false
			backgroundSelected.visible=false
			dispatchEvent(new Event(STATE_CHANGED))
		}

		private function setOverState():void{
			background.visible=false
			backgroundOver.visible=true
			backgroundSelected.visible=false
			dispatchEvent(new Event(STATE_CHANGED))
		}

		private function setSelectedState():void{
			if(state!=SELECTED_STATE){
				backgroundSelected.visible=true
				background.visible=false
				backgroundOver.visible=false
				removeEventListener(MouseEvent.MOUSE_OVER,handleOver)
				removeEventListener(MouseEvent.MOUSE_OUT,handleOut)
				dispatchEvent(new Event(STATE_CHANGED))
			}
		}

		public function deSelect(e:MouseEvent=null):void{

			state=BASE_STATE;
			activate()
			dispatchEvent(new Event(STATE_CHANGED))

		}

		private function activate():void{

			addEventListener(MouseEvent.MOUSE_OVER,handleOver)
			addEventListener(MouseEvent.MOUSE_OUT,handleOut)

		}

	}
}

This component is an alpha version of a custom button with a backgroundImage that changes depending on its state;

This Example shows you a simple ImageBtn in action:

This movie requires Flash Player 9

the Application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
	xmlns:creativesource="it.creativesource.*"
	viewSourceURL="srcview/index.html" width="190" height="130">
	<creativesource:ImageBtn
		id="imageBtn"
		backgroundImg="@Embed('./assets/base.png')"
		rollOverImage="@Embed('./assets/over.png')"
		selectedImage="@Embed('./assets/down.png')"
		enableSelection="true"
		width="130" height="50" />

	<mx:Button click="imageBtn.deSelect()" label="DESELECT IT"  x="140" y="89"/>
</mx:Application>


the Component:

package it.creativesource
{
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.Text;

	public class ImageBtn extends Canvas
	{
		public const BASE_STATE:String='BASE';
		public const OVER_STATE:String='OVER';
		public const SELECTED_STATE:String='SELECTED';
		public const STATE_CHANGED:String="stateChanged"
		protected var textField:Text;
		protected var background:Image;
		protected var backgroundOver:Image;
		protected var backgroundSelected:Image;
		private var _state:String=BASE_STATE;

		public function ImageBtn()
		{

			super();
			buttonMode=true;
			background=new Image()
			backgroundOver=new Image()
			backgroundSelected=new Image();
			textField=new Text();
			state=BASE_STATE;
			activate();

		}

		override protected function createChildren():void{

			super.createChildren();
			addChild(background);
			addChild(backgroundOver);
			addChild(backgroundSelected);
			addChild(textField);

		}
		override public function set label(value:String):void{
			textField.text=value;
		}
		[Bindable]
		override public function get label():String{
			return textField.text;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{

			super.updateDisplayList(unscaledWidth,unscaledHeight);

			textField.x=this.width/2-textField.textWidth/2;
			textField.y=this.height/2-textField.textHeight/2;
			textField.mouseChildren=false;
			horizontalScrollPolicy="none"
			verticalScrollPolicy="none"
		}

		public function set enableSelection(b:Boolean):void{
			if(b==true)
				addEventListener(MouseEvent.CLICK,selectBtn)

		}

		public function set backgroundImg(s:String):void{
			background.source=s;
		}

		public function set rollOverImage(s:String):void{
			backgroundOver.source=s;
		}
		public function set selectedImage(s:String):void{
			backgroundSelected.source=s;
		}

		private function handleOut(e:MouseEvent=null):void{
			state=BASE_STATE;
		}
		private function handleOver(e:MouseEvent=null):void{
			state=OVER_STATE
		}

		private function selectBtn(e:MouseEvent=null):void{
			state=SELECTED_STATE
		}

		public function set state(state:String):void{
			switch(state){
				case BASE_STATE:
					setBaseState();
				break;
				case OVER_STATE:
					setOverState();

				break;
				case SELECTED_STATE:
					setSelectedState();
				break;
				default:
					throw new Error('This state does not exist, please use '+BASE_STATE+'or'+OVER_STATE+'or'+SELECTED_STATE)
					setBaseState();
				break;
			}
			_state=state
		}
		public function get state():String{
			return _state
		}
		private function setBaseState():void{
			background.visible=true
			backgroundOver.visible=false
			backgroundSelected.visible=false
			dispatchEvent(new Event(STATE_CHANGED))
		}

		private function setOverState():void{
			background.visible=false
			backgroundOver.visible=true
			backgroundSelected.visible=false
			dispatchEvent(new Event(STATE_CHANGED))
		}

		private function setSelectedState():void{
			if(state!=SELECTED_STATE){
				backgroundSelected.visible=true
				background.visible=false
				backgroundOver.visible=false
				removeEventListener(MouseEvent.MOUSE_OVER,handleOver)
				removeEventListener(MouseEvent.MOUSE_OUT,handleOut)
				dispatchEvent(new Event(STATE_CHANGED))
			}
		}

		public function deSelect(e:MouseEvent=null):void{

			state=BASE_STATE;
			activate()
			dispatchEvent(new Event(STATE_CHANGED))

		}

		private function activate():void{

			addEventListener(MouseEvent.MOUSE_OVER,handleOver)
			addEventListener(MouseEvent.MOUSE_OUT,handleOut)

		}

	}
}