-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 に答える