无法更改 AS3 中形状的宽度和高度值

Can't change width and height values of a shape in AS3

这是我的实体 class。

package  {
    import flash.display.Shape;
    public class Entity extends Shape{
        public var color:int;
        public function 
        Entity(xA:Number,yA:Number,widthA:Number,heightA:Number,c:int) {
            this.x=xA;
            this.y=yA;
            this.width=widthA;
            this.height=heightA;
            color=c;
            graphics.beginFill(color);
            graphics.drawRect(x,y,width,height);
            graphics.endFill();
        }
    }
}

这是我的主要代码。

var rectangle:Entity = new Entity(10,10,100,100,0xFF0000);
addChild(rectangle);
trace(rectangle.x+" "+rectangle.y+" "+rectangle.width+" "+rectangle.height+" "+rectangle.color);

我期望 widthA 和 heightA 改变形状的宽度和高度,但是当我 运行 代码时,我得到了这个

10 10 0 0 16711680

从轨迹来看,这表明x和y位置在变化,但高度没有变化。这是为什么?

问题是您正在设置没有图形的显示对象的宽度和高度。由于该点的宽度和高度为零,您要求 Flash 将比例值设置为无穷大。空显示对象的宽度和高度不能为零。

试试这个:

        this.x=xA;
        this.y=yA;
        color=c;
        graphics.beginFill(color);
        graphics.drawRect(0,0,widthA,heightA); // I suspect you don't want x and y here.
        graphics.endFill();

宽度和高度将自动设置。