用SKAction更新動態物理體的位置並不好。這就像是在說 - 嘿,不管什麼物理世界的規則說,只是像我想要的那樣移動我的節點。
第一種解決方案 - 使一個節點跟着另一個節點使用SKConstraint。
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let nodeA = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width: 50, height: 50))
nodeA.name = "nodeA"
addChild(nodeA)
let nodeB = SKSpriteNode(color: SKColor.redColor(), size: nodeA.size)
nodeB.name = "nodeB"
addChild(nodeB)
let followConstraint = SKConstraint.distance(SKRange.init(lowerLimit: nodeB.size.width * 1.5, upperLimit: nodeB.size.width * 1.5), toNode: nodeA)
nodeB.constraints = [followConstraint]
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else {
return
}
childNodeWithName("nodeA")!.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 2))
}
}
如果仍要物理機構和SKActions ...
- 「動態」每一個物理體的假財產。
- 如果它們重疊,請檢入didBeginContact(SKPhysicsContactDelegate協議)。如果這是真的,那麼就移除所有正在改變其立場的行動。提示 - 使物理體比它的節點大一點。
幾乎沒有足夠的信息對物體的重量。 4發佈了答案,什麼都不接受? –
我在這裏與需要更多信息的人聊天。我沒有時間來完善建議的方法,但我仍然在嘗試。當我到達我的電腦時,我可以添加一些示例代碼。如果您發現某些信息適用,請詢問@DanielKanaan –