WEB开发网
开发学院图形图像Flash Understanding Flash button component 阅读

Understanding Flash button component

 2009-10-27 00:00:00 来源:WEB开发网   
核心提示:When I am designing a Flash application or a game, I have to say I really hate the process of button creation.That’s why I am using, from now on, Flash bu

When I am designing a Flash application or a game, I have to say I really hate the process of button creation.

That’s why I am using, from now on, Flash built in button component. It’s fully skinnable, you can add it in a click and supports some interesting features such as some key controls:

Shift+Tab: Moves focus to the previous object.

Spacebar: Presses or releases the button and triggers the click event.

Tab: Moves focus to the next object.

Enter/Return: Moves focus to the next object if a button is set as the FocusManager’s default Button.

To use a button component, just drag the button component from the Components panel to the current document’s Library panel. This adds the component to the library, but doesn’t make it visible in the application.

This is an example that uses multiple instances of the button component:

package {
 import flash.display.Sprite;
 import fl.controls.Button;
 import flash.events.MouseEvent;
 import flash.text.TextField;
 public class buttoncomp extends Sprite {
  var my_text:TextField = new TextField();
  var my_button:Button;
  public function buttoncomp() {
   for (var i:int=0; i<5; i++) {
    my_button=new Button();
    addChild(my_button);
    my_button.label="Button "+i;
    my_button.toggle=true;
    my_button.move(50, i*30+10);
    my_button.addEventListener(MouseEvent.CLICK, clickHandler);
   }
   addChild(my_text);
   my_text.text="Click on a button";
   my_text.width=250;
   my_text.x=50;
   my_text.y=160;
  }
  function clickHandler(event:MouseEvent):void {
   my_text.text="You clicked on: "+event.currentTarget.label;
  }
 }
}

Line 3: Importing the required library to use button component.

Line 8: Declaring the my_button variable, Button type.

Line 10: Cycle to generate five buttons.

Line 11: Creating the button

Line 12: Adding the button to stage

Line 13: Setting the button label

Line 14: Defining the toggle attribute. One advantage of using a Button component over a Button symbol is that the component has the built-in toggle behavior. What is a toggle? Let’s start by talking about a regular button which has two stages; selected or unselected. When you click on a regular button, it will be in the selected stage until you release your mouse. With the release of your mouse, the regular button will automatically go back to the unselected state.

A toggle button also has two states; selected and unselected. The first time that you click on a toggle button, it will be set to the selected state, just like a regular button. However, unlike a regular button that goes back to the unselected state when you release your mouse, a toggle button stays in the selected state. To turn off the toggle, you must click on the toggle button again which will set the toggle back to the unselected state.

Line 15: Positioning the button on the stage

Line 16: Adding a listener to the button

Lines 24-26: The function called by the listener outputs the label of the button I just clicked. Obviously you can perform different actions according to the button you pressed.

That’s what I am going to be in my next game.

Meanwhile play with it:

本文示例源代码或素材下载

Tags:Understanding Flash button

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接