Search code examples
swiftxcodeswift3sprite-kit

Need my 'enemyBall' variable instances with function scope, to be made accessible outside


So basically, I have a function spawnEnem() that spawns the enemy and runs the code at certain intervals using NSTimer. I also have a contact function that handles all the contact between objects in my game. In the other function func didBegin(_ contact: SKPhysicsContact), I try to call upon my enemyBall variable instantiated inside spawnEnem() but for obvious reason it can't grab it. I have declared the enemyBall instance inside spawnEnum() as I need to create and process new instances of it every time the function is called. Anyway to get the enemyBall instance to travel beyond the function it's in?

SpawnEnem Function

@objc func spawnEnem() {

        let enemyBall = SKSpriteNode(imageNamed: "enemyBall")


        self.addChild(enemyBall)
        enemyBall.size = CGSize(width: 25.0, height: 25.0)
        enemyBall.physicsBody = SKPhysicsBody(circleOfRadius: enemyBall.size.width/2)
        enemyBall.physicsBody?.affectedByGravity = true


        // Physics of our enemy
        enemyBall.physicsBody = SKPhysicsBody(circleOfRadius: enemyBall.size.width / 2)
        enemyBall.physicsBody?.categoryBitMask = PhysicsCatagroy.EnemyHere // setting enemyBall to our physics
        enemyBall.physicsBody?.contactTestBitMask = PhysicsCatagroy.smallBall | PhysicsCatagroy.mainBall 
        enemyBall.physicsBody?.collisionBitMask = PhysicsCatagroy.smallBall | PhysicsCatagroy.mainBall 
        enemyBall.physicsBody?.isDynamic = false // we dont want enemy effected by gravity
        enemyBall.name = "Enemy"
}

didBegin contact function

       func didBegin(_ contact: SKPhysicsContact) {
        let firstBody = contact.bodyA.node as! SKSpriteNode
        let secondBody = contact.bodyB.node as! SKSpriteNode


           if ((firstBody.name == "Enemy") && (secondBody.name == "smallBall")) {

            NSLog("ENEMY & SMALL Ball TOUCHED!")
            let emitter = SKEmitterNode(fileNamed: Emitter.enemyDead)!
            emitter.zPosition = 4
            emitter.targetNode = self
            emitter.position = enemyBall.position // ERROR IS HERE
            enemyBall.removeFromParent() // ERROR IS HERE
            addChild(emitter)

            collisonOnSmallBall(enemyBall: firstBody, smallBall: secondBody)

           } else if ((firstBody.name == "smallBall") && (secondBody.name == "Enemy")){
               score+=1
               NSLog("SMALL BALL & ENEMY TOUCHED!\(score)")
               collisonOnSmallBall(enemyBall: secondBody, smallBall: firstBody)
           } else if ((firstBody.name == "mainBall") && (secondBody.name == "Enemy")) {
               lives-=1
               NSLog("MAINBALL & ENEMY TOUCHED! \(lives)")

           }

       }

I've been trying a lot to fix it, and I've had no luck. It seemed like a simple issue but turns out it's not for me.


Solution

  • Try this:

    func didBegin(_ contact: SKPhysicsContact) {
        let firstBody = contact.bodyA.node as! SKSpriteNode
        let secondBody = contact.bodyB.node as! SKSpriteNode
    
    
           if ((firstBody.name == "Enemy") && (secondBody.name == "smallBall")) {
    
            NSLog("ENEMY & SMALL Ball TOUCHED!")
            let emitter = SKEmitterNode(fileNamed: Emitter.enemyDead)!
            emitter.zPosition = 4
            emitter.targetNode = self
            emitter.position = firstBody.position // <<<<<<<<<<<<<< LOOK
            firstBody.removeFromParent() // <<<<<<<<<<<<<< LOOK
            addChild(emitter)
    
            collisonOnSmallBall(enemyBall: firstBody, smallBall: secondBody)
    
           } else if ((firstBody.name == "smallBall") && (secondBody.name == "Enemy")){
               score+=1
               NSLog("SMALL BALL & ENEMY TOUCHED!\(score)")
               collisonOnSmallBall(enemyBall: secondBody, smallBall: firstBody)
           } else if ((firstBody.name == "mainBall") && (secondBody.name == "Enemy")) {
               lives-=1
               NSLog("MAINBALL & ENEMY TOUCHED! \(lives)")
    
           }
    
       }
    

    Hope this helps, let me know how you get on.

    Happy coding