Actionscript 3 AS3绘制的精灵比我设置的要大(填充/边距?)
我创建了自己的按钮,它只是从Sprite扩展而来。 它能够显示3个不同的文本淡入淡出使用定时器。 在我的draw函数中,我首先用表示按钮背景的Sprites graphics对象绘制一个矩形 我添加了一个设置按钮宽度的函数。此属性用于绘制矩形。我知道精灵的大小是在矩形绘制之后更新的。但由于某些原因,精灵的宽度总是大于我通过setButtonWidth函数设置的宽度 现在我有一个简单的精灵作为一个按钮,有一个graphics.drawRectangle部分绘制一个简单的rect。比如说500px的宽度。但当我追踪按钮的宽度时,它总是多出10%左右。这10%是从哪里来的 我读到关于给validateNow打电话的消息。但这仅适用于标签或复选框。由于某些原因,我无法访问库以获取标签。这必须以某种方式与文本字段一起工作。但是怎么做呢Actionscript 3 AS3绘制的精灵比我设置的要大(填充/边距?),actionscript-3,flash,sprite,Actionscript 3,Flash,Sprite,我创建了自己的按钮,它只是从Sprite扩展而来。 它能够显示3个不同的文本淡入淡出使用定时器。 在我的draw函数中,我首先用表示按钮背景的Sprites graphics对象绘制一个矩形 我添加了一个设置按钮宽度的函数。此属性用于绘制矩形。我知道精灵的大小是在矩形绘制之后更新的。但由于某些原因,精灵的宽度总是大于我通过setButtonWidth函数设置的宽度 现在我有一个简单的精灵作为一个按钮,有一个graphics.drawRectangle部分绘制一个简单的rect。比如说500px的
// 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.
}
}
此.buttonWidth设置为500。对那个精灵没有什么特别的处理。没有要添加的子对象,只有这些绘图内容。但这会将精灵的宽度设置为550左右。像这样定义按钮
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;
并创建这样的实例
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;
您确定按钮宽度实际上是500px宽,并且没有缺少边框/文本字段/低alpha内容的宽度吗?是否要显示如何设置此.buttonWidth?所有这些代码谁是宽度是从一个名为buttonWidth+20像素的变量计算出来的,我怀疑这是造成问题的原因。buttonWidth是如何设置的?buttonWidth的类型是什么?使用int进行类型转换的用途是什么?请检查我上面更新的文本,了解更多的代码。我将buttonWidth转换为int,因为setButtonWidth函数可以获得像300.45这样的十进制数,所以我需要将其转换为int以获得300。这正是我的button类所做的。除了高度设置,我只使用静态高度,这不应该是一个问题。但是调用setButtonWidth后的按钮宽度仍然比我设置的要大。我跟踪了来自sprite对象的buttonWidth参数和按钮本身的width参数。宽度始终大于我设置的按钮宽度。