Sud*_*oot 7 c# xna input xna-4.0
我正在做一个Tic-Tac-Toe游戏.我需要检查玩家是否点击了他们已经点击过的广场.
问题是错误显示在第一次单击本身.我的更新代码是:
MouseState mouse = Mouse.GetState();
int x, y;
int go = 0;
if (mouse.LeftButton == ButtonState.Pressed)
{
showerror = 0;
gamestate = 1;
x = mouse.X;
y = mouse.Y;
int getx = x / squaresize;
int gety = y / squaresize;
for (int i = 0; i < 3; i++)
{
if (go == 1)
{
break;
}
for (int j = 0; j < 3; j++)
{
if (getx == i && gety == j)
{
if (storex[i, j] == 0)
{
showerror = 1;
}
go = 1;
if (showerror != 1)
{
loc = i;
loc2 = j;
storex[i, j] = 0;
break;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
showerror单击左键时设置为0.我的矩阵是一个3x3矩阵,用于存储信息.如果它是0表示它已被点击.所以在循环中我检查是否store[i,j] == 0然后设置showerror为1.现在在绘图函数中我做了这个调用淋浴
spriteBatch.Begin();
if (showerror == 1)
{
spriteBatch.Draw(invalid, new Rectangle(25, 280, 105, 19), Color.White);
}
spriteBatch.End();
Run Code Online (Sandbox Code Playgroud)
问题是每当我点击空方块时它变成交叉但错误显示出来.请帮帮我
Cyr*_*ral 11
怎么修:
添加一个新的全局变量来存储前一帧中的鼠标状态:
MouseState oldMouseState;
Run Code Online (Sandbox Code Playgroud)
在更新方法的最开始(或结束),添加此,
oldMouseState = mouse;
Run Code Online (Sandbox Code Playgroud)
并替换
if (mouse.LeftButton == ButtonState.Pressed)
Run Code Online (Sandbox Code Playgroud)
同
if (mouse.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
Run Code Online (Sandbox Code Playgroud)
这样做是检查您是否单击一下,然后按下释放键,因为有时您可能会按住多个帧的键.
回顾:
通过oldMouseState在更新之前设置currentMouseState(或者在完成之后),您保证oldMouseState将落后一帧currentMouseState.使用此功能,您可以检查按钮是否在前一帧中,但不再检查,并相应地处理输入.一个好主意,延长这是写一些扩展方法一样IsHolding(),IsClicking()等等.
在简单的代码中:
private MouseState oldMouseState, currentMouseState;
protected override void Update(GameTime gameTime)
{
oldMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
//TODO: Update your code here
}
Run Code Online (Sandbox Code Playgroud)