1

所以我正在创建一个太空射击游戏。我的文档类是 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 函数!

4

4 回答 4

2

最快的方法可能是在 StartMenu 类中添加一个 Engine 变量,并通过开始菜单的构造函数传递引擎。这是一个简短的代码示例:

开始菜单

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
{
    ...
}

我希望这会有所帮助

于 2013-03-13T05:06:26.943 回答
0

这种情况有两种解决方法。一种是使用父引用或特定引用来调用某个函数,如 Ethan Worley 所说,另一种是使用可自定义的公共点击器设置器,如下所示:

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
{
    ...
}
于 2013-03-13T05:46:08.920 回答
0

在您的 Engine.as 类中,您可以放置​​:

public static var instance:Engine;

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

并在引擎类的构造函数中放置:

instance = this;

现在您可以通过以下方式在项目中的任何位置使用 Engine 类的实例以及所有公共函数和变量:

Engine.getInstance().startGame();

它可以帮助你。

于 2013-03-13T05:35:36.740 回答
0

我有点惊讶没有人提到基于事件的方法。这就是我对这种要求所使用的,因为我真的不觉得为函数调用传递整个类实例的想法有那么吸引人(这意味着我可能对这种方法有点偏见,所以请随时指出它的缺点(如果有的话)。

在您的 Engine 类中:

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'));

   ..

}
于 2013-03-14T10:39:22.153 回答