我正在做一个泡泡包装游戏。你按下气泡弹出它们然后稍微松开的那种。一切正常,气泡按计划弹出和发射。但有一个问题。每当我点击并拖动气泡仍然弹出。当我点击并拖动时,有没有办法让它们不弹出?这是我的触摸位置代码。
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;
}
}
}
答案 0 :(得分:0)
我还没有使用过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;
答案 1 :(得分:0)
这是我的工作,我在主菜单上使用它,但应该可以转移。
将这些添加到:
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;
mouseOn = "Options";
然后转到它。考虑到这一切,将所有这些放在一起将帮助您实现在按下鼠标时不使用鼠标的目标。
更新:
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.cs
的类,我使用TitlesScreen上的图像和主类中的鼠标:
首先需要初始化事物,所以在TitleScreen中添加此方法:
Game1 game1;
public void Initialize(Game1 game1)
{
this.game1 = game1;
}
您需要将一个初始化程序添加到Main类(Game1.cs)
TitleScreen titlescreen;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
titlescreen = new TitleScreen();
}
protected override void Initialize()
{
titlescreen.Initialize(this);
base.Initialize();
}
然后,在最后一步中,将Update添加到TitleScreen:
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.Update(gameTime);