所以我創建了一個太空射擊遊戲。我的文檔類是發動機,它看起來像這樣:AS3 - 遇到基本遊戲類問題
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函數!