Why is my collision no longer recognized when I am still colliding with the sprite?

36 Views Asked by At

We are trying to create a charging station for a game that we are creating, with a health bar that increases when the player collides with the charger. In order to do this, we have created a category for the player, as well as a category for the charger. In our didBegin function, which is activated when a collision happens, which can be seen below, the health(which is a global variable) increases, and charging is set to true. In order to make sure that the health does not just continue to increase after the player stops colliding with the charger, we have a didEnd function that only occurs when the collision is no longer happening. This sets the charging variable to false. In our update function, we have conditional statements for what should happen in our code if charging is true or false. When it is true, the health increases by a bit, and doesn’t go above 1 or below zero in order to keep it within bounds. When it is false, the health decreases by a bit, and also does not go above 1 or below zero in order to keep it within bounds. The issue that we are running into is that when the player first collides with the charger, the charging variable is set to true, but as soon as it moves even a little, even if it is still colliding with the charger, the variable is set to false, and it no longer charges. Is there something that we are missing/is there a better way to go about setting up a charging station?

let chargerCategory: UInt32 = 0b1000 //6 if charge is in contact with player, then increase


func didBegin(_ contact: SKPhysicsContact) {
        var body1 = SKPhysicsBody()
        var body2 = SKPhysicsBody()
        
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
            body1 = contact.bodyA
            body2 = contact.bodyB
        }
        else{
            body1 = contact.bodyB
            body2 = contact.bodyA
        }
               
        if body1.categoryBitMask == playerCategory && body2.categoryBitMask == chargerCategory{
            //if player hit charger
            //health += 0.001
            //HealthBar.xScale = health
            print("CHARGING...", health)
            charging = true
        }

func didEnd(_ contact: SKPhysicsContact) {
        var body1 = SKPhysicsBody()
        var body2 = SKPhysicsBody()
        
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
            body1 = contact.bodyA
            body2 = contact.bodyB
        }
        else{
            body1 = contact.bodyB
            body2 = contact.bodyA
        }
        
        if body1.categoryBitMask == playerCategory && body2.categoryBitMask == chargerCategory{
            charging = false
        }
        

    }


override func update(_ currentTime: TimeInterval) {
        if charging == false{
            health -= 0.0001
            HealthBar.xScale = health
            if health < 0 {
                health = 0
            }
            if health > 1.0{
                health = 1.0
            }
        }
        if charging == true{
            health += 0.001
            HealthBar.xScale = health
//            print("CHARGING...", health)
            
            if health < 0 {
                health = 0
            }
            if health > 1.0{
                health = 1.0
            }
        }
        print(health)
        print(charging)
    }
0

There are 0 best solutions below