AS3 class / base class constructor
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 ba开发者_JS百科se 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.
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:
super(positionX, positionY);
OR
You can modify your GameButton constructor to have default values, like so:
public function GameButton(var x:int = 0, var y:int = 0)
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:
精彩评论