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