所以我正在创造一个太空射击游戏。我的文档类是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函数!
答案 0 :(得分:2)
最快的方法可能是将一个Engine变量添加到StartMenu类中,并通过开始菜单的构造函数传递引擎。这是一个简短的代码示例:
的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
{
...
}
我希望有帮助
答案 1 :(得分:0)
在您的Engine.as类中,您可以输入:
public static var instance:Engine;
public static function getInstance():Engine
{
return instance as Engine;
}
并在引擎类的构造函数中放置:
instance = this;
现在您可以通过以下方式在项目的任何位置使用Engine类的instace以及所有公共函数和变量:
Engine.getInstance().startGame();
它可以帮助你。
答案 2 :(得分: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
{
...
}
答案 3 :(得分: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'));
..
}