【发布时间】: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 感谢您的建议和回答,我会看看它。 ;)