2017-02-14 67 views
0

我有一個遊戲,我正在移動方塊頂層上方的塊超出下方的圓圈,這是不可移動的。所以,當一個塊的拖動停止時,我想運行一個檢查或if語句來查看我正在移動的塊(myBlocks [objectDragging])是否在我圓的中心的x個像素範圍內(myCircles [objectDragging ])。 objectDragging只是獲取點擊圖像的標籤。可匹配的圓將具有相同的標籤。一切工作正常,我只是不知道如何檢查我下降的區塊(它的中心點)是否在圓心點的許多像素內。確定正在拖動的圖像是否在特定區域內

一些什麼我的工作:

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 

回答

0

討論在這裏找到兩個CGPoints之間的距離:

How to find the distance between two CG points?

每盧修斯(答案2)

您可以使用hypot()或hypotf()函數來計算斜邊。給定兩個點P1和P2:

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

子在myBlocks.center和myCircles.center爲P1和P2,然後

if distance < 25 { 
    ... 
    }