【问题标题】:Collision detected multiple times.多次检测到碰撞。
【发布时间】:2017-04-30 09:47:11
【问题描述】:

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

我的鸟是一个 5 纹理动画,物理主体用纹理包裹在它的复杂形状上:bird.texture!代码类型。

我已经尝试了 4 天来解决这个问题,这让我的应用程序中断了很多时间!请帮忙!!!

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

【问题讨论】:

    标签: swift collision-detection


    【解决方案1】:

    我找到了答案,虽然它并不完美......我的鸟的物理身体是从

    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 发生两次碰撞。这可能不是很清楚的说明,但如果有人有同样的问题,请不要犹豫!这是我制作 Flappy Bird 应用程序的最大挫折!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多