这是如何实现的?
您创建架构以实现它。您的特定示例可以这样处理:
首先,您要定义这些迷你游戏或单独批次的内容将运行的基础。该基础将处理运行迷你游戏,以及跟踪它们的状态和与它们相关的信息,您希望将这些信息传递给后续的迷你游戏。
其次,您要定义代表这些迷你游戏之一的基类。此类将针对您想要创建并在平台上运行的每个新游戏进行扩展。这个基类将处理通知平台的变化。
最后,您想创建一些模型来表示迷你游戏中的变化状态。内置Event
系统是解决此问题的好方法,但您可以在此实例中轻松创建自己的数据模型,然后将其直接传递给平台。
代码示例。
您的平台的基础可能类似于:
public class Platform
{
private var _game:MiniGame;
private var _recentGameStateData:GameStateData;
public function loadGame(game:MiniGame):void
{
if(_game != null) game.unload();
_game = game;
_game.start(this, _recentGameStateData);
}
public function update():void
{
if(_game != null) _game.update();
}
internal function manageStateChange(gameStateData:GameStateData):void
{
_recentGameStateData = gameStateData;
// Do stuff with the new game state data, like save the current score
// to use in your next game.
//
}
}
你的小游戏:
public class MiniGame
{
private var _platform:Platorm;
private var _score:int = 0;
public function start(platform:Platform, previousGameStateData:GameStateData):void
{
_platform = platform;
// Use previous GameStateData here.
//
}
public function update():void{}
public function unload():void{}
public function notifyPlatform(gameStateData:GameStateData):void
{
_platform.manageStateChange(gameStateData);
}
protected function get score():int{ return _score; }
}
供平台管理的游戏状态数据:
public class GameStateData
{
private var _game:MiniGame;
private var _score:int;
public function GameStateData(game:MiniGame, score:int)
{
_game = game;
_score = score;
}
public function get game():MiniGame{ return _game; }
public function get score():int{ return _score; }
}
然后是在您自己的小游戏中将该信息发送到平台的示例:
public class TetrisGame extends MiniGame
{
override public function unload():void
{
// Let the Platform know about the current score when this game is
// being unloaded.
var state:GameStateData = new GameStateData(this, _score);
notifyPlatform(state);
}
}