我創建了2個SKSpriteNodes,並通過SKPhysicsJointFixed連接它們,以使它們粘在一起。問題是,當我將SKAction.move(by:, duration:)
應用到第一個時,它會單獨移動。爲什麼是這樣,我該如何將它們移動到一起?我搜索了很多,但似乎無法找到任何新的或有用的信息。請幫忙。提前致謝。SKPhysicsJointFixed在移動時不會將節點保持在一起
下面的代碼:
import SpriteKit
class myGame: SKScene {
var node1: SKSpriteNode!
var node2: SKSpriteNode!
func createNode(_ position: CGPoint, color: UIColor) -> SKSpriteNode {
let node = SKSpriteNode(color: color, size: CGSize(width: 50, height: 50))
node.position = position
node.physicsBody = SKPhysicsBody(rectangleOf: node.size, center: position)
node.physicsBody?.affectedByGravity = false
node.physicsBody?.allowsRotation = false
return node
}
func setup() {
node1 = createNode(.zero, color: .red)
node2 = createNode(CGPoint(x: 0, y: -50), color: .green)
self.addChild(node1)
self.addChild(node2)
let anchor = CGPoint(x: node1.size.width/2, y: -node1.size.height/2)
let joint = SKPhysicsJointFixed.joint(withBodyA: node1.physicsBody!, bodyB: node2.physicsBody!, anchor: anchor)
physicsWorld.add(joint)
}
override func didMove(to view: SKView) {
setup()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
node1.run(SKAction.move(by: CGVector(dx: 0, dy: 100), duration: 2))
}
}
嘗試移動那些由物理(力或脈衝),並看看會發生什麼...... – Whirlwind
我想可能是因爲您使用的是SKAction直接移動節點,而不是使用物理(即申請衝動或力量給物理學團體)。嘗試改變衝動。 –
很確定上面兩個是正確的。 – Fluidity