2014-10-07 67 views
1

我在SpriteKit中製作遊戲。 我有一個巨大的問題,我無法在網上找到,甚至在Apple網站上找到能夠在兩個位置(CGPoint)之間找到節點的方法。 方法UiSwipeGesture你找不到任何方向的滑動,但只有左,右,上,下。從兩點(cgpoint)精靈套件檢測精靈 - swift

因此,我發現其他方法(請參閱下面的代碼)製作一張幻燈片,但我無法找到理解這兩個位置之間存在某個節點的方式。

有人可以幫助我嗎?感謝

代碼在這裏:

var locationBegan : CGPoint = CGPoint() 
var locationEnded : CGPoint = CGPoint() 
var objectbegan : SKNode = SKNode()  

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    for touch: AnyObject in touches 
    { 
     let location: CGPoint! = touch.locationInNode(self) 
     self.locationBegan = location 
     var nodeAtPoint : SKNode = self.nodeAtPoint(location) 
     self.objectbegan = nodeAtPoint 

    } 
} 

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { 
    for touch: AnyObject in touches { 
     let location: CGPoint! = touch.locationInNode(self) 
     self.locationEnded = location 
     var nodeAtPoint : SKNode = self.nodeAtPoint(location) 

     var finalDestination : CGPoint = GetFinalDestination(location, nodeAtPoint: self.objectbegan) 
     nodeAtPoint.runAction(SKAction.moveTo(finalDestination, duration: 1)) 

    } 
} 

如何找到locationBegan和locationEnded之間的節點? 謝謝

如果這是不可能的,你可以告訴我一個方法來找到在所有方向上滑動的節點,而不僅僅是左,右,上和下?感謝

UPDATE 設置節點此屬性來看:

node.physicsBody = SKPhysicsBody(circleOfRadius: 5) 
node.physicsBody?.dynamic = false 

回答

1

你必須有SKPhysicsBody S設定您要查找的節點上,但是,一旦你做,你可以使用SKPhysicsWorldbodyAlongRayStart:end:

bodyAlongRayStart返回一個SKPhysicsBody?,所以你必須打開它才能使用它。如果您之後需要SKNode,請使用SKPhysicsBodynode財產(它將返回SKNode?,因此您還需要解開)。使用您的SKScenephysicsWorld財產致電bodyAlongRayStart。如果你把調用該函數和可選的綁定,它看起來是這樣的:

if let physicsBody = scene.physicsWorld.bodyAlongRayStart(locationBegan, end: locationEnded) { 
    if let node = physicsBody.node { 
     /* do something with node here */ 
    } 
} 

如果你希望有更比一個SKNode沿該線(這你可能有),然後你會使用enumerateBodiesAlongRayStart:end:usingBlock:改爲:

scene.physicsWorld.enumerateBodiesAlongRayStart(locationBegan, end: locationEnded) { 
    (physicsBody, point, normal, stop) in 
    if let node = physicsBody.node { 
     /* do something with node */ 
    } 
} 
+0

我能想到的唯一3種可能性是:1.您的節點上沒有設置'physicsBody'; 2.節點的'physicsBody'偏離您在視圖中看到節點的位置(這實際上相當普遍); 3.從'locationBegan'到'locationEnded'的射線確實不會與任何東西相交。你可以做的一件事就是設置['showPhysics'](https://developer.apple.com/library/IOs/documentation/SpriteKit/Reference/SKView/index.html#//apple_ref/occ/你可以將'SKView'的屬性(instp/SKView/showsPhysics)設置爲'true',這樣你就可以看到'physicsBody'的位置。 – 2014-10-08 18:51:55

+0

非常非常感謝,問題是節點上的physicsBody。! Now Works – 2014-10-08 18:57:55

+0

最後一個問題,我必須根據我用幻燈片和最後一次觸摸的位置(locationEnded)選取的節點進行計算。 我的問題是,作爲一個初學者,我還不能進入,所以我想知道如何設置我在SKNode中找到的nodo.physicsworld。 var node = self.scene? .physicsWorld.bodyAlongRayStart(locationBegan,end:location) var nodeNow:SKNode = node? .node 但我不工作,給了我一個錯誤,要求我穿上去掉!在行末。 謝謝 – 2014-10-08 19:30:51