0

I have a game where I'm moving square Blocks on a top layer overtop circles underneath, which are non-moveable. So when the dragging of a block ceases, I want to run a check or an if statement to see if the block I'm moving (myBlocks[objectDragging]) is within x amount of pixels of the center of my circle (myCircles[objectDragging]). objectDragging is just getting the tag of the image clicked. The matchable circle will have the same tag. Everything is working fine, I just cannot figure out how to check if the block I'm dropping (it's center point) is within so many pixels of the circles center point.

Some of what I'm working with:

var myBlocks = [UIImageView]()
var myCircles = [UIImageView]()

let objectDragging = recognizer.view?.tag

if myBlocks[objectDragging!].center.x == myCircles[objectDragging!].center.x {
        ...
        } //this checks for an exact match of center.x where-as I want to check
//if the center.x for myBlocks[objectDragging!] is <= we'll say,
//25, pixels of the myCircles[objectDragging!].center.x
AVerguno
  • 1,277
  • 11
  • 27
idlehand
  • 421
  • 2
  • 15

1 Answers1

0

Discussion here to find distance between two CGPoints:

How to find the distance between two CG points?

per Lucius (answer 2)

You can use the hypot() or hypotf() function to calculate the hypotenuse. Given two points p1 and p2:

CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);

sub in your myBlocks.center and myCircles.center for p1 and p2 and then

if distance < 25 {
    ...
    }
Community
  • 1
  • 1