【问题标题】:Close XNA game window on rectangle click单击矩形关闭 XNA 游戏窗口
【发布时间】:2015-06-03 13:19:56
【问题描述】:

我是 XNA 的新手,我正在尝试创建一个简单的游戏菜单,并且我使用矩形作为菜单项。我有一个名为Game1.cs 的主类和一个用于矩形的不同类,它应该在单击时关闭游戏,称为_exitGame.cs。到目前为止,我已经得到了这个-

在主类中我初始化了一个类变量:

_exitGame exitGame;

我加载纹理和矩形:

exitGame = new _exitGame(Content.Load<Texture2D>("exitGame"), new Rectangle(50, 250,300,50));

我已经为这个类创建了一个更新代码:

exitGame.Update(gameTime);

然后我画了矩形:

exitGame.Draw(spriteBatch);

在我的_exitGame 课程中,我有这个:

class _exitGame
    {
        Texture2D texture;
        Rectangle rectangle;

        public _exitGame(Texture2D newTexture, Rectangle newRectangle)
        {
            texture = newTexture;
            rectangle = newRectangle;

        }
        public void LoadContent()
        {

        }
        public void Update(GameTime gametime)
        {
            var mouseState = Mouse.GetState();
            var mousePosition = new Point(mouseState.X, mouseState.Y);
            var recWidth = rectangle.Width;
            var recHeight = rectangle.Height;
            if (rectangle.Contains(mousePosition))
            {
                rectangle.Width = 310;
                rectangle.Height = 60;
            }
            else
            {
                rectangle.Width = 300; 
                rectangle.Height = 50;
            }

        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rectangle , Color.White);
        }
    }

所以现在我拥有的是一个在鼠标悬停时改变其大小的矩形。早些时候我使用代码this.Close(); 关闭键盘按钮点击游戏,但由于我不能在这种情况下使用它,我有点困惑如何实现这个功能。关于如何做到这一点的任何提示?

【问题讨论】:

  • 我确信 Discosultan 的回答对你有用,但仍然考虑让 exitGame 扩展 drawableGameComponent。它会让你的生活更轻松。看看吧
  • @RobinDijkhof 感谢您的建议和回答,我会看看它。 ;)

标签: c# xna


【解决方案1】:

可以通过调用Exit() method in your Game class 来关闭XNA 游戏。

在你的情况下,你可以在你的 _exitGame 类中引发一个事件

class _exitGame
{
    public event EventHandler ExitRequested = delegate {};

    Texture2D texture;
    Rectangle rectangle;

    public _exitGame(Texture2D newTexture, Rectangle newRectangle)
    {
        texture = newTexture;
        rectangle = newRectangle;

    }
    public void LoadContent()
    {

    }
    public void Update(GameTime gametime)
    {
        var mouseState = Mouse.GetState();
        var mousePosition = new Point(mouseState.X, mouseState.Y);
        var recWidth = rectangle.Width;
        var recHeight = rectangle.Height;
        if (rectangle.Contains(mousePosition))
        {
            rectangle.Width = 310;
            rectangle.Height = 60;
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                ExitRequested(this, EventArgs.Empty);
            }
        }
        else
        {
            rectangle.Width = 300; 
            rectangle.Height = 50;
        }

    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle , Color.White);
    }
}

并在您的 Game 类中订阅该事件

exitGame = new _exitGame(Content.Load<Texture2D>("exitGame"), new Rectangle(50, 250,300,50));
exitGame.ExitRequested += (s, e) => Exit();

几点说明:

  • 为了在引发事件时不总是执行无效性检查,使用空委托public event EventHandler ExitRequested = delegate {}; 通常很方便
  • 只要鼠标左键按下,mouseState.LeftButton == ButtonState.Pressed 表达式就会返回 true,而不仅仅是在第一次单击时。只要你用它退出游戏就可以了,但是对于更新周期将继续运行的其他场景,你应该存储上一个更新周期的鼠标状态,并另外检查鼠标状态是否在上一个周期中没有按下并按下当前捕获点击事件。

【讨论】:

  • 谢谢,这行得通,但我不太明白这部分:exitGame.ExitRequested += (s, e) => Exit();它到底有什么作用?
  • 这一行正在订阅一个使用 lambda 语法的匿名函数的事件。这意味着每当 exitGame 实例引发 ExitRequested 事件时,都会通知 Game 类并执行该函数。 (s, e) =&gt; Exit(); 就是所谓的匿名函数。它相当于void (object s, EventArgs e) { Exit(); }。抱歉,无法在 cmets 中进行漂亮的格式化 :)
【解决方案2】:

只是为您指明正确的方向:

首先,exitGame 在我看来就像一个游戏组件。那么你为什么不把它变成一个游戏组件呢?由于您要执行绘图,因此它必须是 drawableGameComponent。您可以使用 Components.Add(new MyDrawableGameComponent); 将组件添加到您的游戏中

一个gameComponent 就像你的Game1 类一样持有gmae。所以现在只需键入Game.Close() 即可关闭您的游戏。

祝你好运,搜索一下游戏组件和 drawableGameComponents。

【讨论】:

    【解决方案3】:

    事件通常是实现此目的的好方法。现在,由于您已经找到了一种方法来知道何时以您自己的方式单击了按钮,我们可以通过调用您的 Game 对象的 Close 函数来关闭游戏。因此,对于此解决方案,您基本上需要引用 Game 或您所称的 Game 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多