2015-05-19 125 views
0

我有一個名爲Player的SKNode的子類,它幾乎包括這個:http://hub.ae/blog/2014/03/26/soft-body-physics-jellyusing-spritekit/轉換爲swift(有一些變化)。我允許用戶用他的手指用下面的代碼移動玩家節點:locationInNode返回錯誤的結果

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    player.runAction(SKAction.moveTo(touch.locationInNode(world), duration: 1)) 
} 

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    player.runAction(SKAction.moveTo(touch.locationInNode(world), duration: 1)) 
} 

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { 
    player.removeAllActions() 
} 

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    player.removeAllActions() 
} 

*作爲一個說明,在「世界」變種你看到的只是另一個SKNode。 'player'是這個節點的孩子。

這看起來應該可以工作,但是節點會沿非常奇怪的方向移動。這是它的外觀示例: http://gyazo.com/87b0d09101bbbfd3ac0f2a3cdbf42e4c

我該如何解決這個問題?我發現將場景的定位點設置爲0.5可以解決此問題,但是,'player'節點的物理主體會變得混亂。

回答

0

我有同樣的問題,但我不記得,如果它是我如何修復它。嘗試改變這一點:

世界>自

player.runAction(SKAction.moveTo(touch.locationInNode(world), duration: 1)) 

要:

player.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 1)) 

如果我錯了,讓我知道:)

編輯

將錨點設置爲(0.5,0.5)並將這兩種方法寫入到節點的中心。

override func didSimulatePhysics() { 
    camera.position = CGPointMake(player.position.x, player.position.y); 
    centerOnNode(camera) 
} 

func centerOnNode(node: SKNode){ 
    var positionInScene : CGPoint = convertPoint(node.position, fromNode: node.parent) 
    world.position = CGPointMake(world.position.x - positionInScene.x, world.position.y - positionInScene.y) 
} 
+0

這部分工作。當我點擊播放器節點上方時,它會向錯誤的方向移動,但是當我點擊它時,節點會正確移動。 下面是發生了什麼的示例:http://gyazo.com/b968913c3ebd482dbc20bf54b869d071 我很感激幫助。 – Aaron

+0

您是否檢查了觸摸的座標?他們是否正確? 必須嘗試'moveBy'動作? – Darvydas

+0

經過一番調查,我認爲也許問題是我的'節點中心'功能(這被稱爲將相機對準播放器)。我相信這正在改變正確的座標(並且在沒有運行該功能的情況下進行測試後,它似乎正在工作)。你知道解決這個問題的方法嗎?功能在這裏:http://pastebin.com/QRpQmLab(由於字符限制而粘貼) – Aaron