[AS3] SimpleButton 오브젝트의 조작

Programming/ActionScript 3.0 2007. 6. 15. 18:02
ActionScript 3.0에서는 SimpleButton 클래스를 사용해 버튼의 동작을 정의할 수 있다. SimpleButton에는 다음의 3 개 상태가 있다.
upState
downState
overState

이것들은 SimpleButton 오브젝트의 프롭퍼티로 각각 DisplayObject 오브젝트이다. 예를 들어, 다음의 클래스는 단순한 텍스트 버튼을 정의한다.







import flash.display.*;
import flash.events.*;
public class TextButton extends SimpleButton {
public var selected:Boolean = false;
public function TextButton(txt:String) {
upState = new TextButtonState(0xFFFFFF, txt);
downState = new TextButtonState(0xCCCCCC, txt);
hitTestState = upState;
overState = upState;
addEventListener(MouseEvent.CLICK, buttonClicked);
}
public function buttonClicked(e:Event) {
trace("Button clicked.");
}
}
SimpleButton 오브젝트의 hitTestState 프롭퍼티는 버튼의 마우스 이벤트에 응답하는 DisplayObject 인스턴스다. 이 예의 TextButton 클래스는 버튼 상태 ( 업, 다운, 또는 오버) 에 사용한다. DisplayObject를 정의하는 다음의 클래스를 참조.

import flash.display.*;
import flash.text.TextFormat;
import flash.text.TextField;
class TextButtonState extends Sprite {
public var label:TextField;
public function TextButtonState(color:uint, labelText:String) {
label = new TextField();
label.text = labelText;
label.x = 2;
var format:TextFormat = new TextFormat("Verdana");
label.setTextFormat(format);
var buttonWidth:Number = label.textWidth + 10;
var background:Shape = new Shape();
background.graphics.beginFill(color);
background.graphics.lineStyle(1, 0x000000);
background.graphics.drawRoundRect(0, 0, buttonWidth, 18, 4);
addChild(background);
addChild(label);
}
}

    

설정

트랙백

댓글