AS3使用静态类型类问题调用可能未定义的方法

时间:2013-03-25 21:19:19

标签: actionscript-3 flash oop class

收到错误 通过静态类型类的引用调用可能未定义的方法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;
        }       
    }
}

1 个答案:

答案 0 :(得分:5)

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

您可以将getStartButton更改为静态方法:

public static function getStartButton():Sprite

或者您可以创建StartMenu类的实例并将其称为实例方法。

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