所以我正在创建一个太空射击游戏。我的文档类是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函数!
发布于 2013-03-13 13:06:27
最快的方法可能是将引擎变量添加到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
{
...
}我希望这对你有帮助
发布于 2013-03-13 13:35:37
在您的Engine.as类中,您可以放置:
public static var instance:Engine;
public static function getInstance():Engine
{
return instance as Engine;
}在engine类的构造函数put中:
instance = this;现在,您可以通过以下方式在项目中的任何位置使用Engine类以及所有公共函数和变量的实例:
Engine.getInstance().startGame();它可以帮助你。
发布于 2013-03-13 13:46:09
有两种类型的解决这种情况。一种是使用父引用或特定引用来调用某个函数,如Ethan Worley和Worley,另一种是使用可自定义的公共clicker 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
{
...
}https://stackoverflow.com/questions/15377071
复制相似问题