2013-03-13 57 views
1

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

回答

2

可能最快的方法是在StartMenu類中添加一個Engine變量,並通過開始菜單的構造函數傳遞引擎。下面是一個簡短的代碼示例:

的StartMenu

public class StartMenu extends MovieClip 
{ 

    private var _engine:Engine // add a new variable to the start menu class 
    public function StartMenu(engine:Engine) // add a new parameter to the constructor 
    { 
     _engine = engine; // set the variable to the value passed through the constructor 
     button1.addEventListener(MouseEvent.CLICK, buttonClicked); 
    } 

    private function buttonClicked(e:MouseEvent) 
    { 
     _engine.startGame() 
    } 
} 

引擎

public function Engine() 
{ 
    startMenu = new StartMenu(this); 
    // pass through the current instance of engine using the this keyword 
    ... 
} 

public function startGame() // change private to public 
{ 
    ... 
} 

我希望幫助

0

在你Engine.as類,你可以把:

public static var instance:Engine; 

public static function getInstance():Engine 
{ 
    return instance as Engine; 
} 

和構造函數o ˚F引擎類的說:

instance = this; 

現在你可以使用引擎類的instace和所有的公共函數和變量在任何地方你的項目是:

Engine.getInstance().startGame(); 

它可以幫助你。

0

解決這種情況有兩種類型。一種是使用父引用或具體參考調用某些功能,如伊森沃利andwered,另一種是使用自定義的公共唱首歌setter方法是這樣的:

public class StartMenu extends MovieClip 
{ 

    private var button1:MovieClip; // or whatever type your button is 
    private var startGameFunction:Function; 
    public function StartMenu() 
    { 
     // some initialization code if needed, including allocating button1 
     startGameFunction=null; 
     button1.addEventListener(MouseEvent.CLICK, buttonClicked); 
    } 

    public function set startGameClicked(value:Function):void { 
     if (value==startGameFunction) return; // nothing to set 
     startGameFunction=value; 
    } 
    private function buttonClicked(e:MouseEvent) 
    { 
     if (startGameFunction) startGameFunction(); // if there's a function assigned, call it 
    } 
} 

引擎類:

public function Engine() 
{ 
    startMenu = new StartMenu(); 
    startMenu.startGameFunction=this.startGame; 
    // no "()" here, as we are giving a function reference 
    ... 
} 

public function startGame() // change private to public 
{ 
    ... 
} 
0

我有點驚訝,沒有人提到基於事件的方法。這就是我會用於這樣的要求,因爲我沒有真正發現只通過一個函數調用傳遞整個類實例是吸引人的想法(這意味着我可能有點偏向於這種方法請隨時指出它的缺點,如果有的話)。

內,您的引擎類:

public function Engine() 
{ 
    startMenu = new StartMenu(); 

    startMenu.addEventListner('StartGame', startGame); 
    stage.addChild(startMenu); 

    .. 

} 

private function startGame(e:Event) 
{ 

    startMenu.removeEventListner('StartGame', startGame); 

    .. 

} 

裏面你的StartMenu類:

private function buttonClicked(e:MouseEvent) 
{ 

    this.dispatchEvent(new Event('StartGame')); 

    .. 

}