Swift SpriteKit: Node is not removing from parent when it should be

I have a game (like Doodle Jump) where my player hits objects and bounces off of them to advance and gain points. As the player hits said objects, they should just disappear (.removeFromParent()) as it is called in a function in GameScene and in its own separate class. Can anyone tell me what Im doing wrong?

Here is my code for GameScene:

func createSnowAtPosition(position: CGPoint) -> SKNode {
  var sprite: SKSpriteNode!
   sprite = SKSpriteNode(imageNamed: "icons_winter_09")


    let node = Snowflake()
    let position = CGPoint(x: position.x, y: position.y)
    node.position = position
    node.name = "SNOW"

    node.addChild(sprite)
    sprite.physicsBody?.affectedByGravity = false
    sprite.physicsBody?.categoryBitMask = snowCategory
    sprite.physicsBody?.collisionBitMask = playerCategory
    sprite.physicsBody = SKPhysicsBody(circleOfRadius: snow.size.width / 7)
    sprite.physicsBody?.dynamic = false
    return node



}
  func didBeginContact(contact: SKPhysicsContact) {


    if player.physicsBody?.velocity.dy < 0 {
        // 2
        player.physicsBody?.velocity = CGVector(dx: player.physicsBody!.velocity.dx, dy: 250.0)
        // Play sound
        runAction(starSound) {
         snow.removeFromParent()
        }
        if snow.position.y < player.position.y {
            let pointsLabel = childNodeWithName("scoreLabel") as! Points
            pointsLabel.increment()
        }

        }

And here is my code for the separate class:

  import Foundation
   import SpriteKit
 let snow = SKSpriteNode(imageNamed: "icons_winter_09")
    class Snowflake: SKSpriteNode {

init() {
    let size = CGSizeMake(40, 50)
  super.init(texture: nil, color: UIColor.clearColor(), size: size)




   }

  required init?(coder aDecoder: NSCoder) {
     fatalError("init(coder:) has not been implemented")
  }

   func loadBodyWithSize(size: CGSize) {

    snow.physicsBody?.affectedByGravity = false
    snow.physicsBody?.categoryBitMask = snowCategory
    snow.physicsBody?.collisionBitMask = playerCategory
     snow.physicsBody = SKPhysicsBody(circleOfRadius: snow.size.width / 3)

   }

   func createSnowsAtPosition() {
   snow.name = "snow"
    snow.position =  CGPointMake(50, 200)

    snow.physicsBody?.dynamic = false
    snow.physicsBody?.usesPreciseCollisionDetection = true
    addChild(snow)

What is the specific problem you’re having? Does the snowflake remain on the screen after the player bounces on it?

A problem I see in your code is you declare a global snow constant in the Snowflake file.

let snow = SKSpriteNode(imageNamed: "icons_winter_09")

Why do you need this constant? Your spriteNode variable in createSnowAtPosition creates a sprite with the same image.

In the Snowflake class’s createSnowsAtPosition function you modify this constant (I don’t know how you’re able to modify the constant without getting a compiler error) and add it as a child of the Snowflake object. But I don’t see where you call createSnowsAtPosition. In didBeginContact, you remove the snow constant from the parent. But that isn’t going to remove the snowflake the player bounced on.

I recommend removing the snow constant. Change the createSnowAtPosition function to return Snowflake instead of SKNode because you’re working with snowflakes. Build a snowflake in createSnowAtPosition and return it. Add the snowflakes as children of the scene. In your collision detection code, determine which snowflake the player bounced on and remove that snowflake from the parent.

If you still have collision detection problems, set a breakpoint inside didBeginContact and step through the code line by line and see if things are behaving the way you expect.

2 Likes

Thank you for Suggestion sir,i will check with your view.

And i will Snowflake to SKNode,Ok

This topic was automatically closed after 166 days. New replies are no longer allowed.