2014-10-07 86 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