2015-04-21 91 views
0

哎,所以我有一個問題檢測碰撞。我有這個項目是基於Ray Wenderlich的關於如何製作像Mega Jump這樣的遊戲的教程。所以一開始平臺和恆星在一個地方是靜止的,一切都很好。但是當你在比賽中走得更遠,恆星和平臺開始移動(我使用SKActions此),試圖做到這一點使它有點困難。但是當我到達那裏時,碰撞不會被檢測到,玩家只是通過像他們不在那裏的物體。我一直在網上閱讀,我正在使用精確的碰撞檢測,但我仍然沒有看到任何區別。關於什麼可能是錯誤的或者我可以做什麼的任何想法?下面是我如何做這一點代碼:如何檢測兩個快速移動物體之間的碰撞?

player = SKSpriteNode(texture: firstFrame) 
    player.position = (CGPoint(x: self.size.width/2, y: 50.0)) 
    player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width/2) 
    player.physicsBody?.dynamic = true 
    player.physicsBody?.allowsRotation = false 
    player.physicsBody?.restitution = 1.0 
    player.physicsBody?.friction = 0.0 
    player.physicsBody?.angularDamping = 0.0 
    player.physicsBody?.linearDamping = 0.0 
    player.physicsBody?.usesPreciseCollisionDetection = true 
    player.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Player 
    player.physicsBody?.collisionBitMask = 0 
    player.physicsBody?.contactTestBitMask = CollisionCategoryBitmask.Star | CollisionCategoryBitmask.Platform | CollisionCategoryBitmask.Monster 
    foregroundNode.addChild(player) 

func createPlatformAtPosition(position: CGPoint, ofType type: PlatformType) -> PlatformNode { 

    let node = PlatformNode() 
    let thePosition = CGPoint(x: position.x * scaleFactor, y: position.y) 
    node.position = thePosition 
    node.name = "NODE_PLATFORM" 
    node.platformType = type 

    var sprite: SKSpriteNode 
    var spriteFrames : [SKTexture]! 
    if type == .Break { 

     let spriteAnimatedAtlas = SKTextureAtlas(named: "CloudBreak") 
     var cloudFrames = [SKTexture]() 
     spriteFrames = cloudFrames 
     let firstFrame = SKTexture(imageNamed: "Cloud02") 
     sprite = SKSpriteNode(texture: firstFrame) 

     let move = SKAction.moveToX(self.position.x - 160.0, duration:2.0) 
     let back = SKAction.moveToX(self.position.x, duration:2.0) 
     let sequence = SKAction.sequence([move, back, move, back]) 
     sprite.runAction(SKAction.repeatActionForever(sequence)) 

    } else {  
     let spriteAnimatedAtlas = SKTextureAtlas(named: "Cloud") 
     var cloudFrames = [SKTexture]() 
     spriteFrames = cloudFrames 
     let firstFrame = SKTexture(imageNamed: "Cloud") 
     sprite = SKSpriteNode(texture: firstFrame) 

    } 

    node.addChild(sprite) 
    node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size) 
    node.physicsBody?.dynamic = false 
    node.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Platform 
    node.physicsBody?.contactTestBitMask = CollisionCategoryBitmask.Player 
    node.physicsBody?.collisionBitMask = 0 

    return node 
} 

func didBeginContact(contact: SKPhysicsContact) { 

    var updateHUD = false 
    let whichNode = (contact.bodyA.node != player) ? contact.bodyA.node : contact.bodyB.node 
    let other = whichNode as GameObjectNode 
    updateHUD = other.collisionWithPlayer(player) 

} 

    struct CollisionCategoryBitmask { 
     static let Player: UInt32 = 0x00 
     static let Star: UInt32 = 0x01 
     static let Platform: UInt32 = 0x02 
     static let Monster: UInt32 = 0x03 
      } 
+0

發佈您的代碼。 –

+0

在那裏,它是@Alan_s – Thesaurus03

回答

0

碰撞檢測不到,因爲你沒有設置collisionBitMask

player.physicsBody?.collisionBitMask = CollisionCategoryBitmask.Star | CollisionCategoryBitmask.Platform | CollisionCategoryBitmask.Monster 
node.physicsBody?.collisionBitMask = CollisionCategoryBitmask.Player 

接觸和碰撞不是一回事。您可以在網上找到更多信息。

如果它仍然不起作用,請告訴我們您的CollisionCategoryBitmask以確保您正確創建它。

編輯:

struct CollisionCategoryBitmask { 
    static let Player: UInt32 = 0 
    static let Star:  UInt32 = 0b1 
    static let Platform: UInt32 = 0b10 
    static let Monster: UInt32 = 0b100 
} 

下面是我爲another question做了一桌,可以幫助你太: http://goo.gl/7D8EGY

+0

嘿@lchamp謝謝!但是那也行不通。我添加了我的Collision CategoryBitmask讓你看到它。 – Thesaurus03

+0

你的'Monster'位掩碼是_incorrect_。檢查我的編輯,讓我知道它是否工作。 – lchamp

+0

謝謝@lchamp,但仍然沒有解決我的問題!玩家仍然通過移動物體而沒有檢測到碰撞。事情是,一切正常開始時正常工作,當物體開始移動,當碰撞停止工作。 – Thesaurus03

0

慢下來的對象,看看他們是否發生碰撞。我遇到了一個問題,那就是我在快速移動節點時,在新幀被渲染時它們已經過了碰撞點。如果他們移動得太快,他們會在幀週期內傳遞彼此的物理體,他們不會註冊一個命中。

爲了檢測這種情況下發生碰撞時,你可以比較他們在每一幀,如果他們通過對方或自己的X/Y平面值重疊,你可以執行你的碰撞代碼。

+0

謝謝,我該怎麼做?事情是我從Plist加載對象,我已經試圖減慢它們幾乎不動,它仍然不工作! – Thesaurus03