Collision detected multiple times.

北慕城南 提交于 2019-12-12 19:24:11

问题


I am making a flappy bird game. I seem to be having a problem when my bird passes each wall/collects a coin. There are 2 problems. 1 the game lags for a millisecond after collecting. 2 My bird seems to have 2 or even 3 collisions each time creating a score of 2 or 3, I cannot get my head around this!

My bird is a 5 texture animation, with physics body that wraps around its complex shape with the texture: bird.texture! type of code.

I have been trying to figure this out for 4 days now its put the breaks on my app big time! Please 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:


I sort of found the answer to this, though it is not perfect... the physicsBody of my bird was changed from

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

to

 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)

The new path I created was a much simpler path which basically had a square front. So it made it impossible for the shape to collide with my scoreNode twice. This may not be very clear instructions but if anyone has the same problems don't hesitate to ask! This has been the biggest set back of making my flappy bird app!



来源:https://stackoverflow.com/questions/41153181/collision-detected-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!