碰撞多次检测到。

时间:2016-12-14 22:16:50

标签: swift collision-detection

我正在制作一个飞扬的鸟类游戏。当我的小鸟经过每个墙/收集硬币时,我似乎遇到了问题。有两个问题。 1收集后游戏滞后一毫秒。 2我的小鸟每次创造2或3分时似乎有2次甚至3次碰撞,我无法理解这一点!

我的鸟是一个5纹理动画,物理身体围绕其复杂的形状包裹着纹理:bird.texture!代码类型。

我一直在努力解决这个问题4天,现在它让我的应用程序休息了很长时间!请Helpp !!!

func createScene(){

let bird1 = SKTexture(imageNamed: "1")
let bird2 = SKTexture(imageNamed: "2")
let bird3 = SKTexture(imageNamed: "3")
let bird4 = SKTexture(imageNamed: "4")
let bird5 = SKTexture(imageNamed: "5")



let birdAnimation = SKAction.repeatForever(SKAction.animate(with: [bird1, bird2, bird3, bird4, bird5], timePerFrame: 0.1))
let flyForever = SKAction.repeatForever(birdAnimation)

bird = SKSpriteNode(texture: bird1)
bird = CGSize(width: 65, height: 65)
bird = CGPoint(x: self.frame.width / 1.5 - bird, y: self.frame.height / 2)
bird(flyForever, withKey: "birdFly")

bird = SKPhysicsBody(texture: bird.texture!, size: CGSize(width: bird.size.width, height: bird.size.height))


bird.physicsBody?.categoryBitMask = physicsCategory.bird
bird.physicsBody?.collisionBitMask = physicsCategory.ground | physicsCategory.wall
bird.physicsBody?.contactTestBitMask = physicsCategory.ground | physicsCategory.wall | physicsCategory.score
bird?.affectedByGravity = false
bird.physicsBody?.isDynamic = true


self.addChild(bird)


 func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == physicsCategory.score && secondBody.categoryBitMask == physicsCategory.bird{



    score += 1
    scoreLabel.text = "\(score)"
    firstBody.node?.removeFromParent()

    run(SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false))

    if score > UserDefaults().integer(forKey: "HIGHSCORE") {
        saveHighScore()


    }

}
else if firstBody.categoryBitMask == physicsCategory.bird && secondBody.categoryBitMask == physicsCategory.score{

    score += 1
    scoreLabel.text = "\(score)"
    secondBody.node?.removeFromParent()

    run(SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false))

    if score > UserDefaults().integer(forKey: "HIGHSCORE") {
        saveHighScore()


    }

}


 func createWalls(){



let scoreNode = SKSpriteNode(imageNamed: "bird")
scoreNode.size = CGSize(width: 40, height: 40)
scoreNode.position = CGPoint(x: self.frame.width + 25, y: self.frame.height / 2)
scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.isDynamic = false
scoreNode.physicsBody?.categoryBitMask = physicsCategory.score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = physicsCategory.bird

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的答案,虽然它并不完美......我的鸟类的物理身体已经改变了

bird = SKPhysicsBody(texture: bird.texture!, size: CGSize(width: bird.size.width, height: bird.size.height))

 let path = CGMutablePath()
    path.addLines(between: [CGPoint(x: -8, y: -28),
                  CGPoint(x: -30, y: 9), CGPoint(x:-11, y: 14), CGPoint(x: -10, y: 27),
                  CGPoint(x: 26, y: 22), CGPoint(x: 32, y: 20),
                  CGPoint(x: 30, y: 14), CGPoint(x: 23, y: -17), CGPoint(x: 15, y: -31)])
    path.closeSubpath()
    bird.physicsBody = SKPhysicsBody(polygonFrom: path)

我创建的新路径是一条更简单的路径,基本上有一个正方形。因此它使得形状不可能与我的scoreNode碰撞两次。这可能不是非常明确的说明,但如果有人有同样的问题,请不要犹豫!这是制作我的飞鸟应用程序的最大挫折!