3

LozengeButton in AS3

OK, so I grabbed a few minutes over lunch today to try a few button tests just to see what I could knock together without having to read too much documentation :P

Well here are the results:

[actionscript]package {
import flash.display.MovieClip;
import com.flashgen.examples.LozengeButton;

[SWF(width=”100″, height=”50″, backgroundColor=”#DFEEEF”)]
public class AS3LozengeButton extends MovieClip
{
private var _button :LozengeButton;

// This it the default constructor for this ‘main’ class
public function AS3LozengeButton()
{
// instantiate the displayObject class
_button = new LozengeButton();

// set the _button x & y position
_button.y = 10;
_button.x = 10;

// finally add it to the DisplayObjectContainer.
addChild(_button);
}
}
}[/actionscript]

The code above is the default ‘main’ class from my AS3 project, as you can see it imports two classes – MovieClip (this is required for this class to compile) and my “quick” LozengeButton class.

[actionscript]package com.flashgen.examples {
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.TextField;
import flash.display.SimpleButton;
import flash.text.*;

public class LozengeButton extends Sprite {

private var _button :SimpleButton;

public function LozengeButton() {
_button = new GenericLozengeButton();
addChild(_button);
}
}

private class GenericLozengeButton extends SimpleButton
{
private var _upColor :uint = 0xCCCCCC;
private var _overColor :uint = 0x999999;
private var _downColor :uint = 0x333333;
private var _width :uint = 80;
private var _height :uint = 22;
private var _radius :uint = 6;

public function GenericLozengeButton() {

downState = new ButtonState(_downColor, _width, _height, _radius);
overState = new ButtonState(_overColor, _width, _height, _radius);
upState = new ButtonState(_upColor, _width, _height, _radius);
hitTestState = new ButtonState(_upColor, _width, _height, _radius);
useHandCursor = true;
}
}

private class ButtonState extends Shape {

private var _bgColor :uint;
private var _width :uint;
private var _height :uint;
private var _radius :uint;
private var _borderSize :uint = 2;
private var _borderColor :uint = 0x666666;

public function ButtonState(bgColor:uint, width:uint, height:uint, radius:uint) {
this._bgColor = bgColor;
this._width = width;
this._height = height;
this._radius = radius
draw();
}

private function draw():Void {
graphics.beginFill(_bgColor);
graphics.lineStyle(_borderSize, _borderColor);
graphics.drawRoundRect(0, 0, _width, _height, _radius);
graphics.endFill();
}
}
}[/actionscript]

Have a play and any questions just post them below :)

Mike Jones

3 Comments

Comments are closed.