使用 Xcode 的飞鸟游戏上的碰撞和得分标签问题

问题描述

我是 Xcode 的新手,我正在尝试制作一个飞翔的鸟类游戏,但我在得分和碰撞代码方面遇到了麻烦,游戏运行但它是无限的,因为角色在接触墙壁或接触墙壁时永远不会死亡地面,当我拿硬币时,分数水平不会增加,有人可以帮我吗?

func createScene(){
    
    self.physicsWorld.contactDelegate = self
    
    for i in 0..<2 {
        let background = SKSpriteNode(imageNamed: "Background")
        background.anchorPoint = CGPoint.zero
        background.position = CGPoint(x: CGFloat(i) * self.frame.width,y: 0)
        background.name = "background"
        background.size = (self.view?.bounds.size)!
        self.addChild(background)
        
    }
    
    scoreLbl.position = CGPoint(x: self.frame.width / 2,y: self.frame.height / 2 + self.frame.height / 2.5)
    scoreLbl.text = "\(score)"
    scoreLbl.zPosition = 6
    scoreLbl.fontSize = 60
    self.addChild(scoreLbl)
    
    Ground = SKSpriteNode(imageNamed: "Ground")
    Ground.setScale(0.5)
    Ground.position = CGPoint(x: self.frame.width / 2,y: 0 + Ground.frame.height / 2)
    
    Ground.physicsBody = SKPhysicsBody(rectangleOf: Ground.size)
    Ground.physicsBody?.categoryBitMask = PhysicsCatagory.Ground
    Ground.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost
    Ground.physicsBody?.contactTestBitMask  = PhysicsCatagory.Ghost
    Ground.physicsBody?.affectedByGravity = false
    Ground.physicsBody?.isDynamic = false
    
    Ground.zPosition = 3
    
    self.addChild(Ground)
    
    
    
    Ghost = SKSpriteNode(imageNamed: "Ghost")
    Ghost.size = CGSize(width: 60,height: 70)
    Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width,y: self.frame.height / 2)
    
    Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 2)
    Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
    Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
    Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.score
    Ghost.physicsBody?.affectedByGravity = false
    Ghost.physicsBody?.isDynamic = true
    
    Ghost.zPosition = 2
    
    
    self.addChild(Ghost)
    
    
    
    

    
}

override func didMove(to view: SKView) {
    /* Setup your scene here */
    
    createScene()
    
}

func createBTN(){
    
    restartBTN = SKSpriteNode(imageNamed: "RestartBtn")
    restartBTN.size = CGSize(width: 200,height: 100)
    restartBTN.position = CGPoint(x: self.frame.width / 2,y: self.frame.height / 2)
    restartBTN.zPosition = 6
    restartBTN.setScale(0)
    self.addChild(restartBTN)
    restartBTN.run(SKAction.scale(to: 1.0,duration: 0.3))
    
}

func didBeginContact(contact: SKPhysicsContact) {
    let firstBody = contact.bodyA
    let secondBody = contact.bodyB
    
  
    if firstBody.categoryBitMask == PhysicsCatagory.score && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
        
        score+=1
        scoreLbl.text = "score"
        firstBody.node?.removeFromParent()
        
    }
    else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.score {
        
        score+=1
        scoreLbl.text = "score"
        secondBody.node?.removeFromParent()
        
    }
    
    else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Wall || firstBody.categoryBitMask == PhysicsCatagory.Wall && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
        
        enumerateChildNodes(withName: "wallPair",using: ({
            (node,error) in
            
            node.speed = 0
            self.removeAllActions()
            
        }))
        if died == false{
            died = true
            createBTN()
        }
    }
    else if firstBody.categoryBitMask == PhysicsCatagory.Ghost && secondBody.categoryBitMask == PhysicsCatagory.Ground || firstBody.categoryBitMask == PhysicsCatagory.Wall && secondBody.categoryBitMask == PhysicsCatagory.Ghost{
        enumerateChildNodes(withName: "wallPair",error) in
            node.speed = 0
            self.removeAllActions()   
        }))
        if died == false{
            died = true
            createBTN()
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)