程序清单8-18 声明_playerGems数组及_playerNextGems数组
// Declare an array to hold the pair of gems that are dropping under player
// control
private CObjGem[] _playerGems = new CObjGem[2];
// Declare an array to hold the next gems that will be brought into play
private CObjGem[] _playerNextGems = new CObjGem[2];
我们每次添加一对玩家控制的宝石时,就从_playerNextGems数组中将它们的颜色复制过来。这样可以确保预告的下一对来到的宝石的实际颜色。为了确保宝石的预览信息能够出现在游戏的右上方,在生成玩家控制的宝石之前就对它执行初始化。
预览宝石是在名为InitNextGems的函数中进行初始化的,如程序清单8-19所示。在游戏的Reset函数中添加对该函数的调用。
程序清单8-19 对Next Gem对象进行初始化
/// <summary>
/// Create the two "next piece" gems to display in the corner of the screen.
/// This should be called just once per game as it is being reset.
/// </summary>
private void InitNextGems()
{
// Instantiate two new gems.
// The gems have Y positions of 0 and 1 so that they appear one above
// the other in the Next Piece display.
// We also generate initial random colors for the two gems here too.
_playerNextGems[0] = new CObjGem(this, 0, 0, GenerateRandomGemColor());
_playerNextGems[1] = new CObjGem(this, 0, 1, GenerateRandomGemColor());
// These are the 'next' gems -- this affects their position within the
// screen
_playerNextGems[0].GemType = CObjGem.GemTypes.NextGem;
_playerNextGems[1].GemType = CObjGem.GemTypes.NextGem;
// Add the gems to the game
GameObjects.Add(_playerNextGems[0]);
GameObjects.Add(_playerNextGems[1]);
}
既然我们知道了下一对出现的宝石使用的颜色,就可以对玩家控制的宝石进行初始化。这些操作已经创建在一个名为InitPlayerGems的函数中,如程序清单8-20所示。在Reset函数中,当调用了InitNextGems函数后也调用了它。游戏开始后,每次为玩家显示一对新的宝石时都会调用它;稍后您就会看到如何对它进行调用。