C# TouchLocation对象工作不正常
我试图实现一个按下和释放按钮的功能,但得到了一个无法工作的结果,我不知道为什么C# TouchLocation对象工作不正常,c#,ios,xna,xamarin,monogame,C#,Ios,Xna,Xamarin,Monogame,我试图实现一个按下和释放按钮的功能,但得到了一个无法工作的结果,我不知道为什么 public class Button { public TouchLocation oldTouch, currentTouch; public virtual void Update(GameTime gameTime) { TouchCollection touchCollection = TouchPanel.GetState (); if (tou
public class Button
{
public TouchLocation oldTouch, currentTouch;
public virtual void Update(GameTime gameTime)
{
TouchCollection touchCollection = TouchPanel.GetState ();
if (touchCollection.Count > 0) {
currentTouch = touchCollection [0];
if (currentTouch.Position.X > position.X && currentTouch.Position.X < position.X + width &&
currentTouch.Position.Y > position.Y && currentTouch.Position.Y < position.Y + height) {
if (currentTouch.State == TouchLocationState.Released && oldTouch.State == TouchLocationState.Pressed) {
buttonEvent.Invoke (this, new EventArgs ());
}
}
}
oldTouch = currentTouch;
}
}
我明白了
“类型或参数的数量不正确”
“本地调试”窗口中的值为红色(无错误,仅为红色文本值)。但是,它仍然正确地通过了位置检查IF语句,但没有通过按下的
/释放IF语句。如果我用touchCollection[0]
替换currentTouch
,则一切正常。
我是否错误地使用/分配了TouchLocation
对象?
或者我可能只是忽略了一个简单的错误?我总是使用类似于touchCollection.Last()
或touchCollection.First()
的方法来只获取touchCollection
的一个组件。不管怎样,我看不出你在那里做的有任何错误
对于你的问题,我认为你不能只做
oldTouch.State==TouchLocationState.Pressed
但您必须检查:
oldstouch.State==TouchLocationState.Pressed | | oldstouch.State==TouchLocationState.Moved
因为你不知道XNA是如何检测到最后一次触摸的
作为一个建议,为什么不为按钮的碰撞器使用矩形
s呢?通过这种方式,您可以简单地调用:
Rectangle.Contains(新点(int)currentTouch.Position.X,(int)currentTouch.Position.Y))
而不是执行if
语句
currentTouch = touchCollection [0];