2013-03-13 67 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')); 

    .. 

}