Anthony Miller
Anthony Miller

Reputation: 15920

AS3 Call to possibly undefined method with a static type Class issue

Receiving an error Call to possibly undefined method getStartButton through a reference of static type class This is also happening for getQuitButton

I'm still lost on how to utilize classes as I'm new to OOP. Can someone please help me to understand what I'm doing incorrectly?

Here the function in my main class:

function getStartMenu():void {
    var bFormat:TextFormat, bStart:Sprite, bQuit:Sprite;

    bStart = StartMenu.getStartButton();
    bQuit = StartMenu.getQuitButton();

    bStart.addEventListener(MouseEvent.CLICK, bStartPress);
    bQuit.addEventListener(MouseEvent.CLICK, bQuitPress);
}

in my external class file StartMenu.as

package
{
    import flash.display.*;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public class StartMenu extends Sprite
    {

        public function StartMenu():void
        {

        }

        public function getStartButton():Sprite {
            var bFormat:TextFormat, bStart:Sprite, bStartText:TextField;
            bFormat = getFormat();
            bStart = getMenuButton(uint("0X00FF00"));
            bStart.x = stage.stageWidth / 2 - bStart.width - 100;
            bStart.y = stage.stageHeight - bStart.height - 100;
            bStartText = getTextButton(bFormat, "Start");
            bStartText.defaultTextFormat = bFormat;
            return bStart;
        }

        public function getQuitButton():Sprite {
            var bFormat:TextFormat, bQuit:Sprite, bQuitText:TextField
            bFormat = getFormat();
            bQuit = getMenuButton(uint("0X0000FF"));
            bQuit.x = stage.stageWidth / 2 + 100;
            bQuit.y = stage.stageHeight - bQuit.height - 100;
            bQuitText = getTextButton(bFormat, "Quit");
            bQuitText.defaultTextFormat = bFormat;
            return bQuit;
        }


        public function getFormat():TextFormat {
            var bFormat:TextFormat = new TextFormat()
            bFormat.font = "Arial";
            bFormat.bold = true;
            bFormat.color = 0x000000;
            bFormat.size = 28;
            bFormat.align = "center";
            return bFormat;
        }

        public function getMenuButton(bColor:uint):Sprite {
            var bButton:Sprite = new Sprite();
            bButton.graphics.beginFill(bColor, 1);
            bButton.graphics.drawRect(0, 0, 100, 50);
            bButton.graphics.endFill();
            bButton.buttonMode = true;
            bButton.mouseChildren = false;
            return bButton
        }

        public function getTextButton(bFormat:TextFormat, sText:String):TextField {
            var bText:TextField = new TextField()
            bText.defaultTextFormat = bFormat
            bText.text = sText;
            bText.x = -4;
            bText.y = 4;
            return bText;
        }       
    }
}

Upvotes: 2

Views: 9510

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

StartMenu.getStartButton(); is how you would call a static method, since getStartButton isn't static you are getting this error.

You can turn getStartButton to be a static method by changing it to :

public static function getStartButton():Sprite

Or you can create an instance of the StartMenu class and call it's instance methods.

var startMenu:StartMenu = new StartMenu();
var bStart:Sprite = startMenu.getStartButton();

Upvotes: 5

Related Questions