在XNA中显示不同的屏幕

本文关键字:屏幕 显示 XNA | 更新日期: 2023-09-27 18:21:58

最近开始使用XNA(来自java),遇到了显示游戏屏幕的问题。当加载XNA时,我得到了一个game.cs类,我将其解释为一组函数,用于在游戏中绘制一个独立的屏幕。很明显,在这个类中输入所有不同屏幕的代码会很快变得非常混乱,所以我创建了下面的类来处理更改:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Colonies
{
    public class GameManager //manages game screens
    {
        Microsoft.Xna.Framework.Game currentScreen;
        public enum screens { menu, game, summary };
        public GameManager()
        {
            initialize();
        }
        public void initialize()
        {
            currentScreen = new Menu(this);
            ((Menu)currentScreen).Run();
        }
        public void changeScreen(int i) 
        {
            switch (i)
            {
                case 0:
                    currentScreen = new Menu(this);
                    ((Menu)currentScreen).Run();
                    break;
                case 1:
                    currentScreen = new World(this);
                    ((World)currentScreen).Run();
                    break;
                case 2:
                    currentScreen = new Summary(this);
                    ((Summary)currentScreen).Run();
                    break;
            }
        }
}

}

然而,当触发其中一个更改时,会出现一个错误标志,告诉我不能多次调用游戏运行。这是否意味着最初对拥有一个通用游戏屏幕的估计实际上是正确的?!管理器是否应该被询问类似游戏的屏幕,然后在主game.cs类中调用哪些方法?

在game.cs更新方法中,例如:

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();
    // TODO: Add your update logic here
    aGameManager.getAGameObject.DoAnUpdate();
    base.Update(gameTime);
}

所以基本上,我的主游戏类再也不会运行了,只是改变了它显示的内容。这是正确的解决方案吗?(游戏类的大部分内容都是隐藏的,我不确定使用它的正确方法是什么)

在XNA中显示不同的屏幕

Game类是整个游戏。这就是为什么它被称为Game。如果您愿意,您可以创建"屏幕"对象,每个对象控制不同的屏幕,并在尝试使用"游戏管理器"时使用Game类。

例如:

public static int currentScreen = 0; // Any screen can change this variable when needed

List<Screenobject> myscreens = new List<Screenobject>(); // Populate this with screens
// OR
menuscreen = new menuScreen();
otherscreen = new otherScreen();
// ...

protected override void Update(GameTime gameTime)
{
      myscreens[currentScreen].Update(gameTime);
      // OR
      switch (currentScreen)
      {
          case 1:
               menuscreen.Update(gameTime); break;
          // ...
      }
      base.Update(gameTime);
}

和CCD_ 4与CCD_

创建枚举器

enum gamestate
   mainmenu
   gameplay
   options
end enum

然后简单地在你的更新(绘制)主要功能

if gamestate = mainmenu then mainmenu.update();
if gamestate = gameplay then gameplay.update()