2016-10-03 65 views
1

因此,我創建了一個移動基於地圖的牆和圍繞地圖的牆以將玩家保持在操場上的玩家。兩者都有一個物理學的身體。我的猜測是,我的球員運動不合適,所以球員會陷入困境。讓我告訴你我的代碼:SpriteKit:SpriteKit正在相互移動,已經設定了物理體積

所以這是球員的physicsBody:

self.physicsBody!.usesPreciseCollisionDetection = true 
self.physicsBody!.categoryBitMask = PhysicsCategory.Player 
self.physicsBody!.contactTestBitMask = PhysicsCategory.House 
self.physicsBody!.collisionBitMask = PhysicsCategory.Wall 

這是華爾街的physicsBody:

self.physicsBody!.usesPreciseCollisionDetection = true 
self.physicsBody!.categoryBitMask = PhysicsCategory.Wall 
self.physicsBody!.contactTestBitMask = 0 
self.physicsBody!.collisionBitMask = PhysicsCategory.Player 
self.physicsBody!.isDynamic = false 

他們無論是從類繼承對象:

self.physicsBody = SKPhysicsBody(rectangleOf: size) 
self.physicsBody!.affectedByGravity = false 
self.name = name 

所以,只是讓我給你我的代碼如果運動員的運動原因:

func move(direction: String, width: CGFloat){ 
    //Choosing the direction based of the users touch 
    switch direction{ 
     case "South": 
     //Decreasing the position of the player by 1 Tile 
     pos.y -= width 

     //Flipping the picture the right way to make the player look in the right direction 
     yScale = 1.0 

     //Rotates the picture 
     let rotateAction = SKAction.rotate(toAngle: -1.57, duration: 0.0) 
     run(rotateAction) 

     //All the other directions just change the x and y value of pos corresponding to the direction 
    } 

    //Moves the player to the calculated position 
    let moveAction = SKAction.move(to: pos, duration: 0.0) 
    run(moveAction){ 
     //Sound 
    } 

編輯:

PhysicsCategory.PlayerPhysicsCategory.Wall

struct PhysicsCategory{ 
    static let Wall:UInt32 = 0x1 << 0 
    static let Player:UInt32 = 0x1 << 1 
    static let House:UInt32 = 0x1 << 2 
} 

編輯2的值:

的主要問題是獲得與SKAction碰撞正常工作

+0

什麼是價值:PhysicsCategory.Player,PhysicsCategory.House和PhysicsCategory.Wall? – Confused

+0

已修改。房子是玩家必須去的目標。同樣的錯誤也發生在那裏。 –

+0

我們需要比我更多的思想。我的困惑。 – Confused

回答

3

你實際上沒有描述你的p除了「精靈正在彼此移動...」和「所以玩家錯誤地進入了牆壁」之外,還有其他的問題。

要驗證所有聯繫人和collsions,請查看我的答案:iOS SpriteKit - collisions and contacts not working as expected並嘗試執行checkPhysics()函數並在您認爲已設置所有物理實體及其交互後調用它。

您是否確信:

  • 播放機和牆壁居然有物理的身體?
  • 場景是SKPhysicsContactDelegate
  • 您已設置physicsWorld.contactDelegate = self
  • 您已經實施的 SKPhysicsContactDelegate可選方法之一:
    • didBeginContact
    • didEndcontact

是這個更正等等:

  • 你想讓玩家碰壁嗎?
  • 你想讓牆壁與玩家碰撞嗎?
  • 當玩家 和房子有什麼聯繫時,你希望得到通知(didBegincontact叫)?
  • 如果牆壁碰到任何東西,您不希望收到通知嗎?
+0

是的,我做到了。上面我鏈接了一個聊天,我上傳了一個顯示問題的視頻。我會看看你的checkPhysics功能 –

+0

好的 - 讓聊天鏈接更加明顯。 –

+0

對不起。這是正確的。 –

3

它不是在這裏列出的問題,但爲什麼碰撞接觸是不會發生的原因是因爲該方法didBeginContact得到了改變,以didBegin

+0

關於collisons呢?那是好的還是有其他的東西? –