我创建了自己的Button,它只是从Sprite扩展而来。 它能够使用计时器显示3种不同的文本淡入淡出。 在我的绘图功能中,我首先绘制一个矩形,其中Sprites图形对象代表按钮的背景。
我添加了一个设置按钮宽度的功能。此属性用于绘制矩形。我知道在绘制矩形后精灵的大小会更新。但由于某种原因,精灵的宽度总是超过我通过“setButtonWidth”函数设置的宽度。
现在我有一个简单的精灵作为一个按钮,有一个graphics.drawRectangle部分绘制一个简单的矩形。用我们说500px宽度。但是当我跟踪那个按钮的宽度时,它总是大约多10%。这些10%来自哪里?
我读过有关调用validateNow()的内容。但这仅适用于标签或复选框。由于某种原因,我无法访问Label的库。这必须以某种方式与TextFields一起工作。但是如何?
// this function is part of MyButtonClass.as file
function drawButton()
{
this.graphics.clear();
this.graphics.beginFill( currentBackColor);
this.graphics.drawRoundRect(0, 0, int(this.buttonWidth)+20, 30, 10, 10);
this.graphics.endFill();
}
// this code is in main action code frame
// the buttonWidth is set this way
stage.addEventListener( Event.RESIZE, updateBtn);
function updateBtn( event:Event)
{
// access the container in which the buttons are in like ...
for( var i = 0; i < buttonContainer.numChildren; i++) {
var btn = buttonContainer.getChildAt( i) as MyButtonClass;
// MyButtonClass is the class which extends from Sprite drawing my button
// and using buttonWidth to draw the backgrounds width.
btn.setButtonWidth( (stage.stageWidth / 2) - 20);
// i set half of stageWidth because i have two columns with a list of buttons
// after setButtonWidth is called in MyButtonClass, the draw function is called
// which does the graphics stuff above.
}
}
this.buttonWidth设置为500.该精灵没有什么特别的功能。没有孩子可以添加,只有这个绘图的东西。但是这会将精灵的宽度设置为550或者其他东西。
答案 0 :(得分:0)
像这样定义Button
package {
import flash.display.Sprite;
public class Button extends Sprite {
private var _buttonWith: int = 0;
private var _buttonHeight: int = 0;
public function Button() {
// constructor code
}
public function set buttonWidth(w: int): void {
_buttonWith = w;
updateButton();
}
public function get buttonWidth(): int {
return _buttonWith;
}
public function set buttonHeight(h: int): void {
_buttonHeight = h;
updateButton();
}
public function get buttonHeight(): int {
return _buttonHeight;
}
protected function updateButton() {
graphics.clear();
graphics.beginFill(0xff00ff);
graphics.drawRoundRect(0, 0, buttonWidth, buttonHeight, 10, 10);
graphics.endFill();
}
}
}
并创建像这样的实例
var button:Button = new Button();
addChild(button);
button.buttonWidth = 100;
button.buttonHeight = 30;