Xna 触摸定位
我正在做一个泡泡纸游戏。你按下泡泡使其破裂,然后在一点之后将其解开。一切都很好,泡泡会像计划的那样爆裂和消失。但有一个问题。每当我点击并拖动时,气泡仍然会弹出。有没有办法让它们在我点击并拖动时不会弹出?这是我的触摸位置代码Xna 触摸定位,xna,Xna,我正在做一个泡泡纸游戏。你按下泡泡使其破裂,然后在一点之后将其解开。一切都很好,泡泡会像计划的那样爆裂和消失。但有一个问题。每当我点击并拖动时,气泡仍然会弹出。有没有办法让它们在我点击并拖动时不会弹出?这是我的触摸位置代码 foreach (TouchLocation tl in touches) { if (tl.State == TouchLocationState.Pressed) {
foreach (TouchLocation tl in touches)
{
if (tl.State == TouchLocationState.Pressed)
{
if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
{
popLocationX = (int)tl.Position.X;
popLocationY = (int)tl.Position.Y;
}
}
}
我还没有使用过touch,但是所有输入方法的逻辑应该是相似的 我将创建一个变量来存储旧的TouchLocationState。这样,您就可以检查用户是否正在单击和拖动。例如:
//TODO: instantiate this
TouchLocationState oldTLState;
...
// This condition will only be true when the user clicks once, dragging will return false as the oldTLSTate is not released
if (tl.State == TouchLocationState.Pressed && oldTLState.State == TouchLocationState.Released)
{
if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
{
popLocationX = (int)tl.Position.X;
popLocationY = (int)tl.Position.Y;
}
}
//At the end of the Update() method, update oldTLState with the current state
oldTLState = tl;
这就是我所做的,我在我的主菜单上使用这个,但应该能够转换过来 加入以下内容:
MouseState mouse;
public float mouseDelay = 0.01f;
public float mouseTime = 0.0f;
public bool mouseActive = true;
public bool mouseUsed = false;
public string mouseOn = "None";
public Vector2 mouseLocation;
- 鼠标有助于找到鼠标的位置
- 当鼠标处于按下位置时,鼠标播放将停止重复
- mouseTime会说嘿,如果我比mouseDelay大,鼠标可以再次使用
- mouseActive将显示是否按下。如果按下鼠标,则mouseActive为false
- 用鼠标将查看是否使用过它。如果鼠标除了背景之外没有点击任何东西,那么它将是错误的。如果是真的,那意味着它被用来做某事
- 鼠标在我的主菜单中被用来表示你按下了选项?制作
mouseOn=“选项”代码>并转到它
- 鼠标位置保留鼠标所在的位置
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
mouse = Mouse.GetState();
if (this.IsMouseVisible == true)
{
MouseActive(gameTime);
}
base.Update(gameTime);
}
当鼠标不在时,如果鼠标可见,可以阻止进程重复(处理能力降低)
记住这是为一个主菜单制作的,如果你想为一个游戏制作这个,你需要对它进行一些处理。
void MouseActive(GameTime gameTime)
{
mouseTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
mouse = Mouse.GetState();
mouseLocation = new Vector2(mouse.X, mouse.Y);
switch (gameState)
{
case GameStates.TitleScreen:
break;
case GameStates.Options:
break;
case GameStates.Credits:
break;
}
if (mouseOn != "None")
{
mouseUsed = true;
switch (gameState)
{
case GameStates.TitleScreen:
if (mouseOn == "Play")
{
}
if (mouseOn == "Quit")
this.Exit();
if (mouseOn == "Options")
gameState = GameStates.Options;
if (mouseOn == "Title")
{
}
break;
case GameStates.Options:
break;
case GameStates.Credits:
break;
}
mouseOn = "None";
}
if (mouse.LeftButton == ButtonState.Pressed)
{
mouseTime = 0.0f;
mouseActive = false;
}
if (mouse.LeftButton == ButtonState.Released && mouseTime > mouseDelay)
{
mouseActive = true;
mouseUsed = false;
}
}
上面的所有代码都将在主类中
TitleScreen titlescreen;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
titlescreen = new TitleScreen();
}
protected override void Initialize()
{
titlescreen.Initialize(this);
base.Initialize();
}
titlescreen.Update(gameTime);
这是一个名为TitleScreen的类。cs
我是否使用了主类中TitleScreen和鼠标上的图像:
您首先需要初始化对象,因此在TitleScreen中添加此方法:
Game1 game1;
public void Initialize(Game1 game1)
{
this.game1 = game1;
}
public void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (mouse.LeftButton == ButtonState.Pressed && game1.mouseUsed == false && game1.mouseActive == true)
{
if (play.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Play";
game1.mouseUsed = true;
}
else if (title.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Title";
game1.mouseUsed = true;
}
else if (options.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Options";
game1.mouseUsed = true;
}
else if (quit.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Quit";
game1.mouseUsed = true;
}
}
}
您需要在主类(Game1.cs)中添加初始值设定项。
然后在最后一步中,将更新添加到标题屏幕:
Game1 game1;
public void Initialize(Game1 game1)
{
this.game1 = game1;
}
public void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (mouse.LeftButton == ButtonState.Pressed && game1.mouseUsed == false && game1.mouseActive == true)
{
if (play.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Play";
game1.mouseUsed = true;
}
else if (title.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Title";
game1.mouseUsed = true;
}
else if (options.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Options";
game1.mouseUsed = true;
}
else if (quit.Contains(mouse.X, mouse.Y))
{
game1.mouseOn = "Quit";
game1.mouseUsed = true;
}
}
}
您还需要将其添加到主类中的更新中
TitleScreen titlescreen;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
titlescreen = new TitleScreen();
}
protected override void Initialize()
{
titlescreen.Initialize(this);
base.Initialize();
}
titlescreen.Update(gameTime);