2

通过静态类型类的引用接收错误调用可能未定义的方法 getStartButton 这也发生在 getQuitButton

由于我是 OOP 新手,所以我仍然不知道如何利用类。有人可以帮我理解我做错了什么吗?

这是我的主类中的函数:

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);
}

在我的外部类文件 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;
        }       
    }
}
4

1 回答 1

5

StartMenu.getStartButton();是您如何调用静态方法,因为getStartButton不是静态的,您会收到此错误。

您可以getStartButton通过将其更改为:

public static function getStartButton():Sprite

或者你可以创建一个StartMenu类的实例并调用它的实例方法。

var startMenu:StartMenu = new StartMenu();
var bStart:Sprite = startMenu.getStartButton();
于 2013-03-25T21:40:47.833 回答