Flash/AS3 中来自一个构造函数的多个(不同)对象
我必须为学校创建一个 Flash 游戏。然而,我有点卡住了。我知道一种可能有效的方法,但我想知道是否没有一种方法可以一体完成。就这样吧。
我的开始屏幕有 4 个按钮。这些按钮都链接到不同的级别,并且都有不同的图像。我们称它们为 btn1 btn2, .. btn4。我创建了一个名为“GameButton”的类:
package
{
import flash.display.SimpleButton;
public class GameButton extends SimpleButton
{
public function GameButton()
{
// x=
// y=
}
}
}
我在主类中启动它:
public var btn1:GameButton;
public function MainAteam()
{
btn1 = new GameButton();
addChild(btn1);
btn1.addEventListener(MouseEvent.CLICK, startGame1);
}
我的第一个按钮与 GameButton 类链接,并通过主类中的 addChild 将其放在我的舞台上。当然,现在 GameButton 将始终显示我与之链接的按钮的图像。我想知道是否有一种方法可以使用一个构造函数但使用不同的图像。也许在构造函数中使用参数。例如,我可以这样做
btn2 = new GameButton(2)
,然后它添加一个带有按钮 2 图像的按钮。我很困惑,我不知道这是否可能。
我看到的另一种方法是创建四个不同的按钮类并链接每个按钮都有一个不同的按钮,但是我必须为一些非常简单的东西创建文件,这似乎很麻烦......只需添加一个按钮。
自从我使用 AS3 以来已经有一段时间了。希望有人能在这里帮助我。提前致谢。
编辑: 我刚刚意识到这可能是一个愚蠢的问题。但是,是否有一种方法可以为空按钮创建一个构造函数,然后向其添加一个符号?没有把握。真的很困惑,我感谢任何帮助。
I have to create a game in flash for school. I'm kind of stuck, however. I know an approach that might work, but I'm wondering if there isn't a way to do it all-in-one. Here goes.
I have a start screen with 4 buttons. These buttons all link to different levels, and they all have a different image. Let's call them btn1 btn2, .. btn4. I made a class called 'GameButton':
package
{
import flash.display.SimpleButton;
public class GameButton extends SimpleButton
{
public function GameButton()
{
// x=
// y=
}
}
}
I initiate this in my Main class:
public var btn1:GameButton;
public function MainAteam()
{
btn1 = new GameButton();
addChild(btn1);
btn1.addEventListener(MouseEvent.CLICK, startGame1);
}
My first button is linked with the GameButton class and gets put on my stage by addChild in my Main class. Now, of course, GameButton will ALWAYS show the image of the button I linked it with. I was wondering if there is a way to use ONE constructor but use different images.. Perhaps with an argument in the constructor function. For example so I could do
btn2 = new GameButton(2)
And it then adds a button with the image for button 2. I'm confused and I don't know if this is even possible..
The other approach I see is to create four different button classes and link each of them to a different button, but then I'd have to create for files for something really simple, which seems like a lot of trouble for well.. just adding a button.
It's been a while since I worked with AS3. Hopefully someone can help me out here. Thanks in advance.
EDIT:
I just realised this might be a stupid question. However, isn't there a way to like.. make a constructor for an empty button, then maybe add a symbol to it? Not sure. Really confused, I appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要为每个按钮创建一个类。只需在链接选项中为每个按钮指定一个不同的名称,并将
GameButton
设置为它们的基类,而不是SimpleButton
。 Flash要求您为每个导出的符号指定一个唯一的名称,但它不会强制您为其编写一个类(它会在您编译时在幕后为您生成一个类,但您不必担心这一点) )。然后,您的按钮将继承GameButton
的行为,但您可以为每个按钮提供您想要的图形。You don't need to create a class for each button. Just give each button a different name in the linkage options and set
GameButton
as their base class instead ofSimpleButton
. Flash requires you to give each exported symbol a unique name, but it doesn't force you to write a class for it (it will generate a class for you under the covers when you compile, but you don't have to worry about that). Your buttons will then inherit the behavior ofGameButton
but you'll be able to give each of them the graphics you want.