我想用ActionScript 3.0創建一個自定義按鈕。我以背景爲起點,但我對它的大小有問題。DrawRoundRect大小的問題
這是我的自定義按鈕:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
public class customButton extends Sprite
{
private var background:Shape;
public var bgColor:uint;
public var borderColor:uint;
public var borderSize:uint;
public var cornerRadius:uint;
private var label:TextField;
public function customButton(text:String)
{
super();
this.opaqueBackground = 0xFF0000;
background = new Shape();
borderSize = 1;
borderColor = 0x666666;
bgColor = 0xFFCC00;
cornerRadius = 9;
label = new TextField();
label.text = text;
var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0;
format.size = 38;
format.underline = true;
label.defaultTextFormat = format;
addChild(background);
addChild(label);
buttonMode = true;
mouseChildren = false;
}
public function draw():void
{
background.graphics.lineStyle(borderSize, borderColor);
background.graphics.beginFill(bgColor);
background.graphics.drawRoundRect(0, 0, this.width, this.height cornerRadius);
background.graphics.endFill();
}
}
}
這是用來顯示按鈕的代碼:
public function Test01()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
button = new customButton("Button");
button.x = 200;
button.y = 300;
button.width = 200;
button.height = 100;
button.draw();
addChild(button);
}
如果我設置大小的按鈕:
button.width = 200;
button.height = 100;
我收到以下內容:
但我將其設置爲按鈕的大小:
button.width = 40; button.height = 20;
(該尺寸與customButton類中使用的尺寸相同)。我得到:
我不知道爲什麼,當我使用一個大小爲(40,20),我得到比尺寸更小的矩形。
有什麼建議嗎?
謝謝您的回答。如果我使用它,將大小設置爲(40,20),我可以得到:http://img820.imageshack.us/img820/9576/errorbutton.png – VansFannel
這是正確的。 Bhe按鈕背景是40x20像素。但是你正在使用'opaqueBackground = 0xFF0000'屬性,所以它意味着displayObject的所有背景,在這種情況下,customButton將被填充紅色背景。你所擁有的大紅色區域是TextField的影響,因爲默認TF大小是100x100。爲防止出現這種情況,您應該使用:'label.autoSize = TextFieldAutoSize.LEFT;' –