swift - SpriteKit - didBegin contact 被调用 30 次而不是 1 次

标签 swift sprite-kit collision-detection skphysicsbody

我正在制作一个小小的 FlappyBird 克隆,并且一切都正常工作,直到我将鸟的物理主体更改为与纹理精确一致。现在,当它飞过管道间隙时,它会计 30 分,而不是 1 分。

只有当我使用我需要的纹理精确物理体时才会发生这种情况,因为鸟既不是圆形也不是矩形。

我将如何进行碰撞,以便它只与每个间隙节点碰撞一次。我尝试在接触后将 CategoryBitBask 设置为 0,但之后的所有间隙根本不再添加到点数中。

这是完整的游戏代码:

var score = 0

class GameScene: SKScene, SKPhysicsContactDelegate {

var bird = SKSpriteNode()
var bg = SKSpriteNode()
var ground = SKSpriteNode()

var scoreLabel = SKLabelNode(fontNamed: "Candice")
var gameOverLabel = SKLabelNode(fontNamed: "Candice")

var countbg = SKSpriteNode()

var timer = Timer()

enum ColliderType: UInt32 {
    case Bird = 1
    case Object = 2
    case Gap = 4
}

var gameOver = false

let swooshSound = SKAction.playSoundFileNamed("sfx_swooshing.wav", waitForCompletion: false)
let pointSound = SKAction.playSoundFileNamed("sfx_point.wav", waitForCompletion: false)
let hitSound = SKAction.playSoundFileNamed("sfx_hit.wav", waitForCompletion: false)

@objc func makePipes() {

    let movePipes = SKAction.move(by: CGVector(dx: -2 * self.frame.width, dy: 0), duration: TimeInterval(self.frame.width / 150))
    let removePipes = SKAction.removeFromParent()
    let moveAndRemovePipes = SKAction.sequence([movePipes, removePipes])

    let gapHeight = bird.size.height * 2.8

    let movementAmount = arc4random() % UInt32(self.frame.height) / 2

    let pipeOffset = CGFloat(movementAmount) - self.frame.height / 4

    let pipeTexture = SKTexture(imageNamed: "pipe1.png")
    let pipe1 = SKSpriteNode(texture: pipeTexture)
    pipe1.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY + pipeTexture.size().height / 2 + gapHeight / 2 + pipeOffset)
    pipe1.zPosition = 2
    pipe1.run(moveAndRemovePipes)

    pipe1.physicsBody = SKPhysicsBody(rectangleOf: pipeTexture.size())
    pipe1.physicsBody!.isDynamic = false

    pipe1.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    pipe1.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    pipe1.physicsBody!.collisionBitMask = ColliderType.Object.rawValue

    self.addChild(pipe1)

    let pipe2Texture = SKTexture(imageNamed: "pipe2.png")
    let pipe2 = SKSpriteNode(texture: pipe2Texture)
    pipe2.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY - pipeTexture.size().height / 2 - gapHeight / 2 + pipeOffset)
    pipe2.zPosition = 2
    pipe2.run(moveAndRemovePipes)

    pipe2.physicsBody = SKPhysicsBody(rectangleOf: pipe2Texture.size())
    pipe2.physicsBody!.isDynamic = false

    pipe2.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    pipe2.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    pipe2.physicsBody!.collisionBitMask = ColliderType.Object.rawValue

    self.addChild(pipe2)

    let gap = SKNode()

    gap.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY + pipeOffset)
    gap.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 1, height: gapHeight))
    gap.physicsBody!.isDynamic = false
    gap.run(moveAndRemovePipes)

    gap.physicsBody!.contactTestBitMask = ColliderType.Bird.rawValue
    gap.physicsBody!.categoryBitMask = ColliderType.Gap.rawValue
    gap.physicsBody!.collisionBitMask = ColliderType.Gap.rawValue

    self.addChild(gap)
}

func didBegin(_ contact: SKPhysicsContact) {

    if gameOver == false {

    if contact.bodyA.categoryBitMask == ColliderType.Gap.rawValue || contact.bodyB.categoryBitMask == ColliderType.Gap.rawValue {

        score += 1
        scoreLabel.text = String(format: "%05d", score)
        run(pointSound)

    } else {

        self.speed = 0
        run(hitSound)
        gameOver = true
        timer.invalidate()
        bird.removeFromParent()

        let changeSceneAction = SKAction.run(changeScene)
        self.run(changeSceneAction)
        }
    }
}

//MARK: Change to Game Over Scene
func changeScene(){

    let sceneToMoveTo = GameOverScene(size: self.size)
    sceneToMoveTo.scaleMode = self.scaleMode
    let myTransition = SKTransition.fade(withDuration: 0.5)
    self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}

override func didMove(to view: SKView) {

    self.physicsWorld.contactDelegate = self

    setupGame()
}

func setupGame() {

    timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.makePipes), userInfo: nil, repeats: true)

    let groundTexture = SKTexture(imageNamed: "ground.png")
    let moveGroundAnimation = SKAction.move(by: CGVector(dx: -groundTexture.size().width, dy: 0), duration: 7)
    let shiftGroundAnimation = SKAction.move(by: CGVector(dx: groundTexture.size().width, dy: 0), duration: 0)
    let moveGroundForever = SKAction.repeatForever(SKAction.sequence([moveGroundAnimation, shiftGroundAnimation]))

    var i: CGFloat = 0
    while i < 3 {

        ground = SKSpriteNode(texture: groundTexture)
        ground.position = CGPoint(x: self.size.width * i, y: self.size.height / 7.65)
        ground.zPosition = 3
        ground.run(moveGroundForever)
        self.addChild(ground)

        i += 1
    }

    let bottom = SKNode()
    bottom.position = CGPoint(x: self.frame.midX, y: self.size.height / 7)
    bottom.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.width, height: 1))
    bottom.physicsBody!.isDynamic = false

    bottom.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    bottom.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    bottom.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
    self.addChild(bottom)

    let bgTexture = SKTexture(imageNamed: "bg.png")
    bg = SKSpriteNode(texture: bgTexture)
    bg.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bg.size = self.frame.size
    bg.zPosition = 1
    self.addChild(bg)

    let birdTexture = SKTexture(imageNamed: "flappy1.png")
    let bird2Texture = SKTexture(imageNamed: "flappy2.png")
    let bird3Texture = SKTexture(imageNamed: "flappy3.png")
    let bird4Texture = SKTexture(imageNamed: "flappy4.png")
    let bird5Texture = SKTexture(imageNamed: "flappy5.png")
    let bird6Texture = SKTexture(imageNamed: "flappy6.png")

    let animation = SKAction.animate(with: [birdTexture, bird2Texture, bird3Texture, bird4Texture, bird5Texture, bird6Texture], timePerFrame: 0.1)
    let makeBirdFlap = SKAction.repeatForever(animation)


    bird = SKSpriteNode(texture: birdTexture)
    bird.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    bird.setScale(1)
    bird.zPosition = 6

    bird.run(makeBirdFlap)

    self.addChild(bird)

    bird.physicsBody = SKPhysicsBody.init(circleOfRadius: birdTexture.size().height / 2)
    //bird.physicsBody = SKPhysicsBody(texture: birdTexture, size: birdTexture.size())
    bird.physicsBody!.isDynamic = false
    bird.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    bird.physicsBody!.categoryBitMask = ColliderType.Bird.rawValue
    bird.physicsBody!.collisionBitMask = ColliderType.Bird.rawValue

    let countbg = SKSpriteNode(imageNamed: "count_bg.png")
    countbg.position = CGPoint(x: self.size.width / 4.8, y: self.size.height * 0.94)
    countbg.setScale(0.8)
    countbg.zPosition = 4
    addChild(countbg)

    scoreLabel.fontSize = 80
    scoreLabel.text = String(format: "%05d", score)
    scoreLabel.fontColor = SKColor(red: 218/255, green: 115/255, blue: 76/255, alpha: 1)
    scoreLabel.position = CGPoint(x: self.size.width / 4, y: self.size.height * 0.94)
    scoreLabel.zPosition = 5
    addChild(scoreLabel)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if gameOver == false {
        bird.physicsBody!.isDynamic = true
        bird.physicsBody!.velocity = CGVector(dx: 0, dy: 0)
        bird.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 280))
        //run(swooshSound)
    } else {
        gameOver = false
        score = 0
        self.speed = 1
        self.removeAllChildren()
        setupGame()
    }
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}
}

最佳答案

如果您使用 RxSwift,您可以通过使用 debounce()throttle() 轻松摆脱这些额外事件uniqueUntilChanged() 。如果您愿意尝试这种方法,请尝试一下 RxSpriteKit 框架。否则,存储最后一次联系人的时间戳并忽略后续联系人,直到经过一段时间。

关于swift - SpriteKit - didBegin contact 被调用 30 次而不是 1 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651676/

相关文章:

objective-c - iOS Pong 开发,碰撞检测

swift - 如何在搜索后打勾

uiview - 我如何从 SKScene 切换到 UIView

ios - 我如何缓入/缓出 SKEmitterNode.particleBirthRate

ios - 我想用 IOS SpriteKit 制作一个足球

python - 计算二维碰撞后的合成速度矢量

json - 将 JsonObject 传递给模型 SWIFT

ios - 使用 swift 3 通过键盘移动 View

ios - 在 Firebase 返回结果后为变量赋值?

c# - 无法使用 Rectangle.Intersects() 正确检测碰撞