-3

我直奔主题。我将静态坐标存储为数组,我想将此坐标与用户触摸进行比较。

//touch handling
 UITouch *touch = [[event allTouches] anyObject];
 CGPoint touchPoint = [touch locationInView:touch.view];
//comparing touches
 if (CGRectContainsPoint((CGRectMake(x1, y1, w, h)) , touchPoint)) {
  // do something
            // this is where i got stuck coz i got 2 more sets of x & y. (x2-y2 & x3-y3)

但现在我被困在这里,因为我不知道如何构建我的代码,我想比较 3 个保存位置触摸与用户触摸,这样当他们到达正确的位置时,分数/分数将被添加,但当他们击中错误的位置时,生命将会被扣除。谢谢。

4

1 回答 1

1

如果你有这样存储的积分。. .

CGPoint p1 = CGPointMake(100,100);
CGPoint p2 = CGPointMake(200,200);

尝试这样的事情:

// Get the location of the user's touch
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];

float maxDistance = 10;

// Is it in the right place?
if (distanceBetween(touchPoint, p1) < maxDistance)
  NSLog(@"touched point 1");
else
if (distanceBetween(touchPoint, p2) < maxDistance)
  NSLog(@"touched point 2");

其中 distanceBetween 是一个看起来像(一些数学)的函数

// Distance between two CGPoints
float distanceBetween(CGPoint p1, CGPoint p2) {
  float dx = p1.x-p2.x;
  float dy = p1.y-p2.y;
  return sqrt( dx*dx + dy*dy);
}

希望有帮助,

山姆

于 2009-11-05T11:08:49.923 回答