Flash / Flex / ActionScript/Development/SimpleButton
Add mouse click event to SimpleButton
package {
import flash.display.*;
import flash.events.*;
public class SimpleButtonDemo extends Sprite {
public function SimpleButtonDemo( ) {
var button:SimpleButton = new SimpleButton( );
button.x = 20;
button.y = 20;
button.upState = createCircle( 0x00FF00, 15 );
button.overState = createCircle( 0xFFFFFF, 16 );
button.downState = createCircle( 0xCCCCCC, 15 );
button.hitTestState = createCircle( 0x000000, 50 );
button.addEventListener( MouseEvent.CLICK, handleClick );
addChild( button );
}
private function createCircle( color:uint, radius:Number ):Shape {
var circle:Shape = new Shape( );
circle.graphics.lineStyle( 1, 0x000000 );
circle.graphics.beginFill( color );
circle.graphics.drawCircle( 0, 0, radius );
circle.graphics.endFill( );
return circle;
}
private function handleClick( event:MouseEvent ):void {
trace( "Mouse clicked on the button" );
}
}
}
Creating Simple Buttons
package {
import flash.display.*;
import flash.events.*;
public class SimpleButtonDemo extends Sprite {
public function SimpleButtonDemo( ) {
var button:SimpleButton = new SimpleButton( );
button.x = 20;
button.y = 20;
button.upState = createCircle( 0x00FF00, 15 );
button.overState = createCircle( 0xFFFFFF, 16 );
button.downState = createCircle( 0xCCCCCC, 15 );
button.hitTestState = button.upState;
button.addEventListener( MouseEvent.CLICK, handleClick );
addChild( button );
}
private function createCircle( color:uint, radius:Number ):Shape {
var circle:Shape = new Shape( );
circle.graphics.lineStyle( 1, 0x000000 );
circle.graphics.beginFill( color );
circle.graphics.drawCircle( 0, 0, radius );
circle.graphics.endFill( );
return circle;
}
private function handleClick( event:MouseEvent ):void {
trace( "Mouse clicked on the button" );
}
}
}
SimpleButton with up, over and down sprite
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var overSprite:Sprite = new Sprite();
overSprite.graphics.beginFill(0xff0000, 1);
overSprite.graphics.drawRect(0, 0, 100, 100);
overSprite.graphics.endFill();
var upSprite:Sprite = new Sprite();
upSprite.graphics.beginFill(0x00ff00, 1);
upSprite.graphics.drawRect(0, 0, 100, 100);
upSprite.graphics.endFill();
var downSprite:Sprite = new Sprite();
downSprite.graphics.beginFill(0xffffff, 1);
downSprite.graphics.drawRect(0, 0, 100, 100);
downSprite.graphics.endFill();
var btnOne:SimpleButton = new SimpleButton(upSprite, overSprite, downSprite, overSprite);
}
}
}