Add mouseHandlers with a Object
@author Victor Potasso
www.victorpotasso.com
/**
* Add mouseHandlers with a Object
* by www.victorpotasso.com
*/
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
/**
*
* @author Victor Potasso
* @since 14 march 2011
*/
public class AddEvents extends Sprite
{
/**
* Constructer.
*
*/
public function AddEvents()
{
this.createObject("botao1", 50, 160, 200);
this.createObject("botao2", 80, 320, 200);
this.addMouseHandlers( {
{button:Sprite(this.getChildByName("botao1")), event:"click"},
{button:Sprite(this.getChildByName("botao1")), event:"rollOver"},
{button:Sprite(this.getChildByName("botao1")), event:"rollOut"},
{button:Sprite(this.getChildByName("botao2")), event:"click"},
{button:Sprite(this.getChildByName("botao2")), event:"rollOver"},
{button:Sprite(this.getChildByName("botao2")), event:"rollOut"}},
this.onMouseHandler
);
}
/**
* Create Object
* @param objN
* @param objW
* @param objH
* @param objX
* @param objY
*/
private function createObject (objN:String, objS:Number, objX:Number, objY:Number):void
{
var obj:Sprite = new Sprite ();
obj.graphics.beginFill(0x000000);
obj.graphics.drawCircle(objX, objY, objS);
obj.graphics.endFill();
addChild( obj );
obj.name = objN;
}
/**
*addEventMouse
* @param objs
* @param onComplete
*
*/
private function addMouseHandlers (objs:Object, onComplete:Function):void
{
var iEvent:Object;
for ( iEvent in objs){
objs[iEvent].button.addEventListener (objs[iEvent].event, onComplete, false, 0, false);
}
}
/**
*onMouseHandler
*/
private function onMouseHandler (evt:MouseEvent):void
{
switch (evt.type)
{
case MouseEvent.CLICK:
this.linkEnter( evt.target.name );
break
case MouseEvent.ROLL_OVER:
evt.target.alpha=0.5;
break
case MouseEvent.ROLL_OUT:
evt.target.alpha=1;
break
}
}
/**
*linkEnter
*/
private function linkEnter (objName:String):void
{
var request:URLRequest = new URLRequest();
switch (objName)
{
case "botao1":
request.url="http://www.google.com/search?q=botao1";
break
case "botao2":
request.url="http://www.google.com/search?q=botao2";
break
}
navigateToURL( request, "_blank" );
}
}
}