Wednesday, May 18, 2011
How to create a clickable button in Flash Actionscript:
Here is an example on how to create a clickable button with listener events in Flash Actionscript:
/**
* How to create a button
*/
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
var button:Sprite;
var clickUrl:String = "http://thong-vu.blogspot.com";
if( stage == null ) {
this.addEventListener( Event.ADDED_TO_STAGE, handleStageReady);
}
else {
handleStageReady();
}
function handleStageReady(e:Event=null):void {
// text message
var textField:TextField = new TextField();
textField.text = "Click Me!!!";
textField.selectable = false;
textField.mouseEnabled = false;
textField.multiline = false;
textField.wordWrap = false;
textField.width = textField.textWidth + 5;
textField.height = textField.textHeight + 5;
textField.antiAliasType = AntiAliasType.NORMAL;
// click button
button = new Sprite();
button.addChild(textField);
button.width = textField.width;
button.height = textField.height;
button.buttonMode = true;
button.mouseChildren = false;
button.mouseEnabled = true;
button.addEventListener(MouseEvent.MOUSE_OUT, handleButtonEvents);
button.addEventListener(MouseEvent.MOUSE_OVER, handleButtonEvents);
button.addEventListener(MouseEvent.CLICK, handleButtonEvents);
addChild(button);
}
function handleButtonEvents(me:MouseEvent):void {
switch(me.type) {
case MouseEvent.MOUSE_OVER:
button.alpha = 0.5;
break;
case MouseEvent.MOUSE_OUT:
button.alpha = 1;
break;
case MouseEvent.DOUBLE_CLICK:
default:
// navigate to url
navigateToURL( new URLRequest(clickUrl), "_blank" );
}
}
Labels:
Flash,
Flash 101: Beginner
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment