05Feb
[AS 3] A Button from the SimpleButton Class
4 comments so farI just wanted to figure out how buttons could be created in Actionscript 3. Nothing to exciting here, but this may be a bit of useful code in the future.
Here is the document class Button.as:
package {
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.display.Shape;
public class Buttons extends Sprite
{
public var button:SimpleButton;
public function Buttons()
{
button = new SimpleButton;
/*
downState: the state that the button is in when the user clicks the hitTestState object.
overState: the state that the button is in when the mouse is positioned over the button.
upState: the state that the button is in when the mouse is not positioned over the button.
hitTestState: Specifies a display object that is used as the hit testing object for the button.
useHandCursor: Boolean - displays the hand cursor when the mouse rolls over a button.
trackAsMenu: Boolean - Indicates whether other display objects that are SimpleButton or MovieClip objects can receive mouse release events. Default is false.
useHandCursor: Boolean - displays the hand cursor when the mouse rolls over a button.
trackAsMenu: Boolean - Indicates whether other display objects that are SimpleButton or MovieClip objects can receive mouse release events. Default is false.
*/
button.upState = colorButton(0xDAD8F3);
button.overState = colorButton(0x4F42C6);
button.downState = colorButton(0xDDF2FF);
button.hitTestState = colorButton(0xDDF2FF);
button.useHandCursor = true;
addChild(button);
}
private function colorButton(rgb:uint):Shape
{
var rect:Shape = new Shape();
rect.graphics.lineStyle(1,0xff3300);
rect.graphics.beginFill(rgb);
rect.graphics.drawRect((stage.stageWidth/2) - 50, (stage.stageHeight/2) - 50, 100, 100);
return rect;
}
}
}
Categories: Actionscript 3, Buttons
Tuesday, February 5th, 2008 at 10:41 am and is filed under Actionscript 3, Buttons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Can I also force a SimpleButton in the overState, e.g. by forcing a mouse event on an instance?
I am thinking that the solution to this would be to simply not use the SimpleButton and to use a MC or Sprite with applied MouseEvents… On the other hand you could just make the upState the overState when the movie loads and then toggle the upState function to something else when desired
hi!
how do I import the “Button.as” to my “.fla”??
thanks
Hi Eddy,
You could make this class your Main Document class or you could call it by first importing in the class file and then just setting something like:
var myButton:Buttons = new Buttons();
addChild ( myButton );
.. I just noticed in my code that the file should be named Buttons.as
Thanks, Morgan