Written by Philip
|
< Previous
|
|---|
When your creating a button in pure ActionScript 3 you will have first needed to create a graphic for your button to place your AS3 Cursor on. This graphic could be something like a simple yellow box. Your code could look like the following.
Remember to import in flash.display.Graphics
//Imports import flash.display.Graphics; //Draw the button Sprite var button:Sprite = new Sprite(); button.graphics.beginFill(0xFFCC00); button.graphics.drawRect(0,0,200,200); button.graphics.endFill(); //Add Button Sprite to stage this.addChild(button);
At this stage you would be thinking - 'how do I add my AS3 hand cursor to the AS3 button graphic that I've just created?' So that the graphic actaully gives the user experience of a button, when the user rolls over the graphic. This is done using a hand cursor, and is implemented with the following code:
//Add useHandCursor, buttonMode, and mouseChildren if required button.useHandCursor = true; button.buttonMode = true; button.mouseChildren = false;
Awesome! So now you've added buttonMode and useHandCursor to the AS3 button. The graphic you've created has a user experience of a button when the user hovers over it. You may also want to set mouseChildren = false; on the button. What does mouseChildren false do?
As this is quite a basic tutorial I will not go into that here, but check our Adobe Livedocs for a full explanation. (References below)
Here is the code for what we've just created; an AS3 Button with a hand cursor.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Graphics;
/**
* ...
* @author www.how-to-code.com
*/
public class Main extends Sprite
{
public function Main():void
{
//Draw the button Sprite
var button:Sprite = new Sprite();
button.graphics.beginFill(0xFFCC00);
button.graphics.drawRect(0,0,200,200);
button.graphics.endFill();
//Add useHandCursor, buttonMode, and mouseChildren if required
button.useHandCursor = true;
button.buttonMode = true;
button.mouseChildren = false;
//Add Button Sprite to stage
this.addChild(button);
}
}
}
If you wish to learn more about the AS3 Hand Cursor the following articles are an excellent resource.
| Related Articles | Link |
| mouseChildren on Adobe Livedocs |
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#mouseChildren |
| buttonMode on Adobe Livedocs |
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/Sprite.html#buttonMode |
| Flex Hand Cursor | http://cookbooks.adobe.com/post_Creating_a_hand_cursor_over_a_component-1687.html |
| AS3 Hand Cursor at dispatchevent.org |
http://dispatchevent.org/roger/hand-me-that-cursor-would-you/ |
|
< Previous
|
|---|