我在Flash中製作遊戲,我遇到的問題是兩個班,一個名爲Menu的菜單,其中包含一個開始遊戲按鈕和引擎AS3:從另一個班級調用開關盒不起作用
public var state:int;
public const MENU:int = 0;
public const GAME:int = 1;
// create the variable for the menu
private var _menu:Menu;
public var sBg1:ScrollBg;
public var sBg2:ScrollBg;
public function Engine(menu:Menu)
{
_menu = menu;
//we add event listener when the engine is added to the stage
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
//then we remove the event listener and initiate the init function
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
init();
}
private function init():void {
//the init function set the game state at first to menu and run the drawUI function
state = MENU;
drawUI();
}
public function drawUI():void
{
//the drawUI function draw the needed elements on stage according to the state of the game
switch(state){
case MENU:
_menu = new Menu();
addChild(_menu);
break;
case GAME:
sBg1= new ScrollBg();
addChild(sBg1);
sBg1.x = 0;
sBg1.y = 0;
//if(contains(_menu)){
//removeChild(_menu);}
trace('this the game');
break;
}
}
,我從菜單中更改狀態使用菜單中的類此代碼實際的遊戲:當我運行遊戲的我第一次設置菜單,通過這個代碼會自動顯示它運行一切類:
public var start_btn:MovieClip;
private var _engine:Engine;
public function Menu()
{
addEventListener(Event.ADDED_TO_STAGE, displayMenu);
}
private function displayMenu(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, displayMenu);
start_btn = new startBtn();
start_btn.x = 100;
start_btn.y = 200;
addChild(start_btn);
start_btn.addEventListener(MouseEvent.CLICK, startGame);
}
private function startGame(e:MouseEvent):void
{
start_btn.removeEventListener(MouseEvent.CLICK, startGame);
_engine = new Engine(this);
_engine.state = 1;
_engine.drawUI();
}
我每次都使用drawUI函數爲我ü例如它放置的開始按鈕和遊戲它放置的背景,但由於某種原因,我只得到跟蹤聲明說(這個遊戲),但背景不顯示任何線索。
我花了4個多小時試圖找出問題所在,如果任何人都可以幫助我,那將會很棒。
感謝
請評論你的代碼並解釋神祕物體是什麼。 – iND 2012-04-28 16:30:33
此外,請在代碼中顯示更完整的課程(例如'class Menu extends ...')。 – iND 2012-04-28 16:36:05
背景(可能)正在添加。添加一個'trace()'來驗證wid/ht,x/y和'sBg1'的可見性。最後,添加一個「trace(this.getChildIndex(sBg1))'。這應該全部進入相關的切換命令。 – iND 2012-04-28 16:38:52