2014-09-24 18 views
5

Tôi đang cố kết nối hai sốsử dụng SKPhysicsJointPin tại anchorPoint được đánh dấu là chấm màu xanh lục trên ảnh chụp màn hình bên dưới. Sau đó, tôi muốn bật physicsBody!.dynamic = true trên object2 để có được "hình động xoay" của đối tượng2.Cách kết nối hai SKSpriteNode bằng SKPhysicsJointPin - swift

enter image description here

enter image description here

Tôi gặp khó khăn ngay từ đầu rất trong khi tạo ra các SKPhysicsJointPin thậm chí tôi không nhận được một lỗi trong Xcode nó không biên dịch.

Dưới đây là các mã:

import SpriteKit 

class GameScene: SKScene, SKPhysicsContactDelegate { 

let object1 = SKSpriteNode(imageNamed: "white") 
let object2 = SKSpriteNode(imageNamed: "black") 

override func didMoveToView(view: SKView) { 
    // Setup background image 
    self.backgroundColor = UIColor(hex: 0x60c0f3) 

    // Setup physics body to the scene (borders) 
    self.physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame) 

    // Change gravity settings of the physics world 
    self.physicsWorld.gravity = CGVectorMake(0, -9.8) 
    self.physicsWorld.contactDelegate = self 

    //=========================================== 

    // White object properties 
    object1.physicsBody = SKPhysicsBody(rectangleOfSize: object1.frame.size) 
    object1.physicsBody!.dynamic = false 
    object1.position = CGPointMake(size.width/2 - object1.size.width/2, size.height/2) 

    // Black object properties 
    object2.physicsBody = SKPhysicsBody(rectangleOfSize: object2.frame.size) 
    object2.physicsBody!.dynamic = true 
    object1.anchorPoint = CGPointMake(0, 0) 
    object2.position = CGPointMake(size.width/2 + object2.size.width + 2, size.height/2 + object2.size.height/2) 

    // Create joint between two objects 
    var myJoint = SKPhysicsJointPin.jointWithBodyA(object1.physicsBody, bodyB: object2.physicsBody, anchor: CGPoint(x: CGRectGetMaxX(self.object1.frame), y: CGRectGetMaxY(self.object2.frame))) 

    self.physicsWorld.addJoint(myJoint) 
    self.addChild(object1) 
    self.addChild(object2) 

} 

override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
} 
} 

Các lỗi Xcode tôi nhận được là enter image description here

Xin cho biết những gì là sai trong mã của tôi. Cảm ơn bạn.

Trả lời

2

Hai vấn đề: 1) bạn cần thêm các nút chứa vật thể vật lý vào cảnh trước khi kết nối chúng với khớp và 2) bạn cần kết nối các nút ở giá trị Y tối thiểu không phải là cực đại (nếu bạn muốn khớp để hành xử như thể hiện trong sơ đồ của bạn), vì nguồn gốc của cảnh là góc dưới cùng/bên trái của khung nhìn và Y dương là lên.

// Do this prior to adding the joint to the world 
    self.addChild(object1) 
    self.addChild(object2) 

    // Create joint between two objects. Edit: changed MaxY to MinY to attach bodies 
    // at bottom of the nodes 
    var myJoint = SKPhysicsJointPin.jointWithBodyA(object1.physicsBody, bodyB: object2.physicsBody, anchor: CGPoint(x: CGRectGetMaxX(self.object1.frame), y: CGRectGetMinY(self.object2.frame))) 

    self.physicsWorld.addJoint(myJoint) 
+0

Cảm ơn bạn! Nó đã làm việc :) – Omen

Các vấn đề liên quan