所以我正在创建一个太空射击游戏。我的文档类是 Engine,它看起来像这样:
package Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
public class Engine extends MovieClip
{
private var startMenu:StartMenu;
private var numberOfStars:int = 80;
public static var enemyList:Array = new Array();
private var spaceShip:Ship;
private var hud:HUD;
public function Engine()
{
startMenu = new StartMenu();
stage.addChild(startMenu);
startMenu.x = (stage.stageWidth / 2);
startMenu.y = (stage.stageHeight / 2);
}
private function startGame()
{
stage.removeChild(startMenu)
spaceShip = new Ship(stage);
stage.addChild(spaceShip);
spaceShip.x = (stage.stageWidth / 2);
spaceShip.y = (stage.stageHeight / 2);
spaceShip.addEventListener("hit", shipHit);
hud = new HUD(stage); //create the HUD
stage.addChild(hud); //and display it.
for (var i:int = 0; i < numberOfStars; i++)
{
stage.addChildAt(new Star(stage), 1);
}
addEventListener(Event.ENTER_FRAME, createFighter);
}
}
如您所见,我正在调用另一个名为 StartMenu 的类。这是我遇到麻烦的地方:这是代码(或缺少代码)
package Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.*;
public class StartMenu extends MovieClip
{
public function StartMenu()
{
button1.addEventListener(MouseEvent.CLICK, buttonClicked);
}
private function buttonClicked(e:MouseEvent)
{
}
}
}
(忽略缩进错误,在真实代码中是正确的)好吧,想象一下屏幕上显示了一个按钮。此按钮是 StartMenu 类的一部分,正在侦听 MouseEvent.CLICK。
单击按钮后,我需要以某种方式返回 Engine 类并调用函数 startGame() ,但我不能只执行 Engine.startGame() ,我尝试将函数设置为公共函数,并且我有尝试将函数设置为公共静态函数。没运气。请帮忙??任何方法都可以,我只需要一种方法让这个类在单击按钮后转到 startGame 函数!