2
votes

I'm new to objective c and swift and I created a small app where small circles are rendered and once the player collides with a circle, the game ends. I managed to get everything to work, but how do I remove the nodes after they collide. I tried removeAllChildren(), but none of them disappear. When I use removeFromParent(), only 1 disappears. I want a way to remove all 3 nodes that will be rendered in the code below

//addEvilGuys() is called first

func addEvilGuys()
    {
        addEvilGuy(named: "paul", speed: 1.3, xPos: CGFloat(self.size.height/3))
        addEvilGuy(named: "boris", speed: 1.7, xPos: frame.size.width/4 + 50)
        addEvilGuy(named: "natasha", speed: 1.5, xPos: frame.size.width/4 + 150)
    }

    func addEvilGuy(#named:String, speed:Float, xPos: CGFloat)
    {
        evilGuyNode = SKSpriteNode(imageNamed: named)

        evilGuyNode.zPosition = 10
        evilGuyNode.physicsBody = SKPhysicsBody(circleOfRadius: 16)
        evilGuyNode.physicsBody!.affectedByGravity = false
        evilGuyNode.physicsBody!.categoryBitMask = ColliderType.BadGuy.rawValue
        evilGuyNode.physicsBody!.contactTestBitMask = ColliderType.Hero.rawValue
        evilGuyNode.physicsBody!.collisionBitMask = ColliderType.Hero.rawValue

        evilGuyNodeCount++

        var evilGuy = EvilGuy(speed: speed, eGuy: evilGuyNode)
        evilGuys.append(evilGuy)
        resetEvilGuy(evilGuyNode, xPos: xPos)
        evilGuy.xPos = evilGuyNode.position.x
        addChild(evilGuyNode)
    }

    func resetEvilGuy(evilGuyNode:SKSpriteNode, xPos:CGFloat)
    {
        evilGuyNode.position.y = endOfScreenBottom
        evilGuyNode.position.x = xPos
    }
1

1 Answers

2
votes

It looks like in addEvilGuy you are recreating a stored property (i.e. that is visible for the entire class + whatever the access level allows) to create the SKSpriteNode that you're adding. This means that you are orphaning the previously created EvilGuy.

In addEvilGuy, replace

evilGuyNode = SKSpriteNode(imageNamed: named)

with

let evilGuyNode = SKSpriteNode(imageNamed: named)

and remove the property from your class (it doesn't seem like you have a need for in in a larger scope).

It also looks like you're creating EvilGuys and storing them in an array, which is good. So when you can remove all of them from the screen with a function like:

func removeAllEvilGuys(evilGuys: [EvilGuy]) {
  for evilGuy in evilGuys {
    evilGuy.eGuy.removeFromParent()
  }
}

As a best practice advice, since you mentioned you're a beginner:

  1. I'd recommend defining the characteristics of the evil guys in a .plist and then use the file to create an array of evil guys. This way you can easily make changes to the evil guys in that file without having to change anything in your code.

  2. The code that creates an EvilGuy object should be separated from the one that adds the evil guy to the screen. As long as you are storing the SKNode of each one, you'll be able to add/remove without unnecessarily recreating the entire object.