2014-03-19 36 views
0

是否有可能檢測一個節點是否與另一側碰撞?SKPhysics一側碰撞檢測

例如對另一隻動物跑的動物。在這種情況下,碰撞檢測方法會被調用。但是當動物跳過動物並「走動」時,碰撞檢測不會被激活。

我希望每一個答案。 感謝

我的代碼:

if (lionNode.frame.origin.y + lionNode.frame.size.height < animalNode.frame.origin.y) { 
     NSLog(@"Walk"); 
    } 
    else { 
     NSLog(@"dead"); 
    } 

回答

1

在碰撞檢測回調中檢查以下內容。

if(y-origin of the first animal + its height < the y-origin of the second animal){ 
    //then do the logic for walking on top of another animal (probably nothing would happen here depending on what you are trying to build) 
} 
else { 
    //forward to collision logic (e.g life loss, lose points, restart level ,etc..) 
} 

,如果有可能的第一個動物進入第二隻動物的下方,那麼你需要更新你的病情以下

if(firstAnimal.origin.y + firstAnimal.frame.size.height < secondAnimal.frame.origin.y 
|| firstAnimal.origin.y > secondAnimal.frame.origin.y + secondAnimal.frame.size.height) 

警惕的不同座標可能有時混淆系統。希望這可以幫助,隨時詢問是否有什麼不清楚的地方。

+0

感謝您的回答!我已經多次嘗試過,但還沒有奏效。我已經在 –

+0

之上發佈了我的代碼,嘗試記錄獅子的原始值和高度,以瞭解發生了什麼。如果沒有任何意義嘗試使用的位置,而不是如下代碼 – rockstarr

+0

我認爲我在代碼中發現問題,在skscene中原點位於左下角,因此條件應該更改爲 大於 if(lionNode.frame.origin。 y + lionNode.frame.size.height> animalNode.frame.origin.y) – rockstarr

0

碰撞檢測將被調用在醚的情況下,但在回調中可以比較的速度矢量;如果他們彼此離開,把它當作一個小姐。

+0

感謝您的回答!你能解釋一下你的想法嗎?我不太清楚你在回調中比較速度向量的含義。 –