开发者

Multiple (different) objects from one constructor in Flash/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 开发者_StackOverflowdo

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.


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 of SimpleButton. 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 of GameButton but you'll be able to give each of them the graphics you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜