【发布时间】:2017-03-05 14:56:46
【问题描述】:
我在网上四处寻找解决此问题的方法,但无法解决。
我想扩展精灵类来制作我自己的互动圈。一切正常,但设置自定义类的 X、Y、宽度和高度不起作用:
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 和宽度实际上什么都没有出现。然而,如果我手动设置它们,无论是在课堂上
this.graphics.drawCircle(200,200, 30);
或在 addChild 之前:
circle = new CircleDraw(10,10,100,200);
circle.x=100;
circle.y=100;
然后它出现在屏幕上。一旦在类中手动添加值而不是 this.x 等,也可以像这样添加它:
circle = new CircleDraw(10,10,100,200);
addChild(circle);
所以我的问题是,我如何扩展一个类(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 变量。
【问题讨论】: