AS3 类/基类构造函数
我的上一个问题的后续问题。
我的舞台上有一个按钮,其中有一个名为 Game1 的类(我没有定义该类,只是将其链接起来,就像胡安回答我的其他问题一样)。该按钮以“GameButton”为基类,目前包含一些简单的 x、y 语句。
我的所有按钮都有一个虚拟类“GameX”和一个基类 GameButton;这样它们继承自基类,但它们仍然可以具有不同的图形。
我有一个主类,其中包含添加此按钮的代码:
public class MainAteam extends MovieClip
{
public var btn1:Game1;
public function MainAteam()
{
btn1 = new Game1();
addChild(btn1);
btn1.addEventListener(MouseEvent.CLICK, startGame1);
}
// startGame 1 function here
}
现在,我希望能够通过参数给出 x、y 值,以便我可以将每个按钮放在不同的位置。但是,当我尝试 new Game(5,5) 并将以下内容放入 GameObject 构造函数中时:
package
{
import flash.display.SimpleButton;
public class GameButton extends SimpleButton
{
public function GameButton(startX:Number, startY:Number)
{
x = startX;
y = startY;
}
}
}
我收到以下错误:
1203: No default constructor found in base class GameButton
我真的不知道知道如何解决这个问题,因为我让 Flash 为我定义 GameX 类(我想它们只是空的)并使用基类来设置一些属性。如果我只是输入 x=5; y=5,工作正常。多谢。
A follow-up to my previous question.
I have a button on my stage, which has a class called Game1 (I didn't define this class, just linked it like Juan answered to my other question). The button has 'GameButton' as base class, which at the moment contains some simple x, y statements.
All my buttons will have a dummy class 'GameX' and a base class of GameButton; this way they inherit from the base class, but they can still have a different graphic.
I have a main class which contains code to add this button:
public class MainAteam extends MovieClip
{
public var btn1:Game1;
public function MainAteam()
{
btn1 = new Game1();
addChild(btn1);
btn1.addEventListener(MouseEvent.CLICK, startGame1);
}
// startGame 1 function here
}
Now, I would like to be able to give x, y values through parameters so I can place each button on a different spot. However, when I try new Game(5,5) and I put the following in the GameObject constructor:
package
{
import flash.display.SimpleButton;
public class GameButton extends SimpleButton
{
public function GameButton(startX:Number, startY:Number)
{
x = startX;
y = startY;
}
}
}
I get the following error:
1203: No default constructor found in base class GameButton
I don't really know how to fix this, since I let Flash define the GameX classes for me (I suppose they're just empty) and use the base class to set some properties. If I just put x=5; y=5
, it works fine. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是两件事之一,要么你需要调用 super();从你的构造函数中,或者你没有在初始化时实现所需的类属性/方法。尝试扩展 Button,而不是 SimpleButton,看看它是否更适合您。编辑
我的第一个答案是正确的,但仍然是错误的。问题是您正在创建 Game1、Game2 等类,而不是调用 GameButton 类的构造函数。当你扩展一个具有必需的构造函数参数的类时,你必须使用 super(); 提供它们。因此,在这种情况下,您需要在 Game1 类构造函数中调用:
或者
您可以修改 GameButton 构造函数以具有默认值,如下所示:
如果您不知道, super() 只是访问构造函数的一种方法基类,也可以称为超类,这就是该方法称为 super 的原因:)。希望这有帮助。
ALSO
另请注意,如果您要从 Flash IDE(在库中)创建这些 Game1、Game2 类等,那么您将需要向Flash 的 GameButton 构造函数会自动生成库对象的类。或者,您仍然可以在预编译的库剪辑中创建图形,而不是在 Export For Actionscript 设置中定义类 + 基类,而是像为 GameButton 所做的那样手动创建实际的 Game1/Game2 等类,然后在 Export For Actionscript 区域放置完整的限定类名。像这样:
This is one of two things, either you need to call super(); from within your constructor, or you havn't implemented required class properties/methods on initialization. Try extending Button, not SimpleButton see if that works better for you.EDIT
My first answer was on the right track but still wrong. The problem is that you're creating your Game1, Game2 etc classes and not calling the constructor of the GameButton class. When you extend a class that has REQUIRED constructor arguments you must provide them using super(); So in this case, inside your Game1 class constructor you need to call:
OR
You can modify your GameButton constructor to have default values, like so:
In case you don't know, super() is just a way to access the constructor of the base class, which can also be called the super class, which is why the method is called super :). Hope this helps.
ALSO
Also please note that if you're creating these Game1, Game2 classes etc from within the Flash IDE (in the library) then you're going to need to go the route of adding default values to the GameButton constructor as Flash automatically generates classes for library objects. Alternatively, you can still create the graphics in precompiled library clips, and instead of defining a class + base class in the Export For Actionscript settings, create an actual Game1/Game2 etc class manually like you did for GameButton, and then in the Export For Actionscript area put the full qualified class name. Like so: