我在做泡泡包装游戏。一种你按下泡泡的方式,它们会在一小段时间后破裂。一切都很好,泡沫按计划破裂和破裂。不过,有一个问题。每当我点击并拖动气泡时,气泡仍会破裂。有什么方法可以让它们在我点击和拖动时不弹出?这是我的触摸位置代码。
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;
}
}
}发布于 2014-03-25 14:39:55
这是我所做的,我使用这个在我的主菜单上,但应该能够转移过来。
把这些加进去:
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;
}您需要在主类(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);发布于 2014-03-24 10:19:59
我还没有接触过触摸,但是所有输入方法的逻辑都应该是相似的。
我会创建一个变量来存储旧的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;https://stackoverflow.com/questions/22598157
复制相似问题