修改父 类 变量

Modifying parent classes variables

我在网上四处寻找这个问题的解决方案,但一直无法解决。

我想扩展精灵class来制作我自己的互动圈。一切正常,但设置自定义的 X、Y、宽度和高度 class 不起作用:

class CircleDraw extends Sprite
{

public function new(x:Int,y:Int,width:Int,height:Int)
{
    super();
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    drawCircle();
}

private function drawCircle()
{
    this.graphics.beginFill(0xffffff);
    this.graphics.drawCircle(this.x,this.y, this.width);
    this.graphics.endFill();
}
}

上述方法没有按预期工作,通过构造函数设置 x、y 和宽度实际上什么也没有出现。然而,如果我手动设置它们,要么在 class

this.graphics.drawCircle(200,200, 30);

或在 addChild 之前:

 circle = new CircleDraw(10,10,100,200);
 circle.x=100;
 circle.y=100;

然后它出现在屏幕上。在 class 而不是 this.x 等中手动添加值后,也像这样添加后:

circle = new CircleDraw(10,10,100,200);
addChild(circle);

所以我的问题是,如何扩展 class (Sprite) 并允许构造函数修改其父级默认变量并保留值?

编辑

只是按要求提供所有代码:

这不起作用:

    circle = new CircleDraw(10,10,100,200);
    addChild(circle);

画圆的时候是这样的:

class CircleDraw extends Sprite
{
    public function new(x:Int,y:Int,width:Int,height:Int)
    {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        drawCircle();
    }

    private function drawCircle()
    {
        this.graphics.beginFill(0xffffff);
        this.graphics.drawCircle(this.x, this.y, this.width);
        this.graphics.endFill();
    }

}

如果我修改方法,它确实有效:

private function drawCircle()
        {
            this.graphics.beginFill(0xffffff);
            this.graphics.drawCircle(200, 200, 30);
            this.graphics.endFill();
        }

或者如果在对象实例化过程中我设置了编辑前提到的 X 和 Y 变量。

根据您 post 编辑的(部分)代码,它看起来应该可以工作。

换句话说,像这样设置 parent 的变量应该可行。

我怀疑您没有 post 妨碍其他一些代码。我建议您在调试的 drawCircle 调用中打印出 x、y 值。

我也怀疑圆圈是画出来的off-screen所以你看不到它。因为你先把精灵移动到(x,y)然后在精灵里面的(x,y)画了一个圆。所以实际上圆在全局 space

中位于 (2x,2y)

编辑:

this.graphics.drawCircle(200,200, 30);

你声称这有效。应该在 (210,210) 处绘制,半径为 30

circle = new CircleDraw(10,10,100,200);
circle.x=100;
circle.y=100;

你声称这有效。应该在 (200,200) 处绘制,半径为 100

circle = new CircleDraw(10,10,100,200);
addChild(circle);

您声称这不起作用。应该在 (20,20) 处绘制,半径为 100

从上面三个版本来看,你其实是在不同的地方画的,大小不一。所以我仍然怀疑"didn't work"可能是因为你在一些看不见的地方画画。

所以我还是建议你在私有drawCircle函数中打印出x,y,width的值,看看发生了什么

据我推测,您似乎在尝试覆盖不带参数的构造函数(新)。

我不完全确定,但这可能无法满足您的需求。我认为您实际上在做的只是在扩展的 class 上创建一个方法 "new",然后在其中不向 parent 提供任何内容(super())。所以 parent 将没有构造函数参数。在调试器中查看 object 并查看实例属性与 parent 的实例属性会很有趣。

可能有解决方法,但我认为您可能希望通过组合而不是继承来实现这一点。例如,在 "new" 中,创建一个精灵的新实例,并操纵它的属性。

class MySprite
{
    public var sprite : Sprite;

    public function new ( ) {
        sprite = new Sprite ();
    }
}

然后您将对该精灵实例执行您的操作。本质上,您只是将一个精灵包裹在您自己的 class 中。装饰师,或多或少。

我在这里找到了一些关于该主题的参考资料(我已经有一段时间没有进行任何 Flash 编程了),它显示了 addChild 和 removeChild 实现等。

http://old.haxe.org/forum/thread/685

您确实成功地设置了基础 class 变量,只是 DisplayObject.width 有一个相当具体的(也许不是那么明显的)行为。

width:Float

Indicates the width of the display object, in pixels. The width is calculated based on the bounds of the content of the display object. When you set the width property, the scaleX property is adjusted accordingly, as shown in the following code:

Except for TextField and Video objects, a display object with no content(such as an empty sprite) has a width of 0, even if you try to set width to a different value.
OpenFL API: DisplayObject: width

从这个意义上说,OpenFL遵循了ActionScript 3.0 API

请注意,您正在处理 2 个坐标 space。一个是将添加的 Sprite 和该 Sprite 的图形对象。如果你设置 this.xthis.width 你实际上是在设置这个 Sprite 的属性。因此,在这种情况下,关于定位,您正在移动 Sprite 本身。 Sprite "graphics" 属性 有自己的坐标 space.

例如,如果您在 CircleDraw 构造函数中传递 x=100,您首先将精灵本身移动 x=100,然后绘制 x 偏移量为 100 的圆。因此,如果您要添加将 sprite 直接放到舞台上,您实际上会在 x=200 处开始绘制圆圈,因为您有效地应用了该位置两次。在我看来,这不是您真正想要的。

此外,您的 CircleDraw 实例(当然也是 Sprite)在构建时也是 "empty"。所以在构造函数中调用 this.width = width 没有任何效果。 this.width 将保持为 0,因为此时 Sprite 没有内容。因此,在 drawCircle 方法中,您正在绘制一个 width 为 0 的圆。

可能更好的解决方案是简单地将构造函数的参数传递给 drawCircle 函数,而不设置任何属性。