Mostafa Mohsen
Mostafa Mohsen

Reputation: 43

How can I delete a node when it is out of the view?

I have a node falling down in a view. Once it is outside of the view, I need to delete it so I can use an if-statement to call a function to place another one. How do I delete it when it is no longer visible? It currently only calls the function once.

This is the function:

func dot() {
    var dotTexture = SKTexture (imageNamed: "dot")
    dotTexture.filteringMode = SKTextureFilteringMode.Nearest

    var dot = SKSpriteNode(texture: dotTexture)
    dot.setScale(0.5)
    dot.position = CGPoint(x: self.frame.size.width * 0.5 , y: self.frame.size.height * 1.1)

    dot.physicsBody = SKPhysicsBody (circleOfRadius: dot.size.height/2.0)
    dot.physicsBody?.dynamic = true
    dot.physicsBody?.allowsRotation = false

    self.addChild(dot)

    println("done")
}

And this is the if-statement that calls the function:

 override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if self.children.count == 0 {
        dot()
    }
}

Upvotes: 0

Views: 57

Answers (1)

Alex Reynolds
Alex Reynolds

Reputation: 6352

I find the easiest way to do this is to add a new SKNode at the bottom of the screen and use collision with it to remove your dot. Create a blank SKNode and set its position slightly offscreen and its width equal to the scenes width. Then set the contactTestBitMask so you can know when a dot has collided with the node. Then remove the dot in the didBeginContact(SKPhysicsContact: contact) method

Upvotes: 1

Related Questions