[英]XNA for WP7 and multitouch update every frame
我在使用觸摸/多點觸摸輸入時遇到問題。
我想在用戶按下的位置(完成任務)繪制一個尺寸為100x100的小矩形,但是我也希望它們隨着用戶移動手指而移動(這不會發生在atm上)。
除了靜止不動的部分外,我的行為也很奇怪,比方說,我先用拇指觸摸,然后再用中指觸摸。 每個手指下面都有兩個立方體,但是如果我移開手指(在這種情況下為拇指),我放在第二個手指(中指)下面的立方體將消失,而拇指所在的那個立方體仍將在那里。 我想這個問題一旦解決就可以解決,只要有動作就可以正確更新。
這是“繪制”和“更新”片段。 任何幫助表示贊賞:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
TouchCollection touchLocations = TouchPanel.GetState();
i = 0;
foreach (TouchLocation touchLocation in touchLocations)
{
if (touchLocation.State == TouchLocationState.Pressed)
{
pos[i] = touchLocation.Position;
}
i++;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
for (j = 0; j < i; j++)
{
spriteBatch.Draw(whiteRectangle, new Rectangle(((int)pos[j].X - 50), ((int)pos[j].Y - 50), 100, 100), Color.Chocolate);
}
spriteBatch.End();
base.Draw(gameTime);
}
如果您想將觸摸位置與其位置相關聯,我建議您使用詞典。 密鑰應為TouchLocation.Id
對於給定的交互,TouchLocation.Id將保持不變。 不能保證TouchLocations的順序從一幀到另一幀是相同的,因此您必須使用它們的ID,而不是它們在集合中出現的順序。
我在這方面有點遲了,但好在這里是哪里出了問題以及我如何解決...
foreach (TouchLocation touchLocation in touchLocations)
{
if (touchLocation.State == TouchLocationState.Pressed)
{
pos[i] = touchLocation.Position;
}
i++;
}
我想我在編寫代碼的這一部分時很困(凌晨2點后什么也沒有發生...甚至沒有好的代碼)我不知道為什么我要檢查是否按下了位置狀態...這就是為什么當我移動時不會移動...確實已按下了第一幀,但是一旦開始移動它便是.Moved ...總而言之,將其移除,如果它像一個吊飾一樣工作...
foreach (TouchLocation touchLocation in touchLocations)
{
pos[i] = touchLocation.Position;
i++;
}
總而言之,它的行為符合預期。
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.