简体   繁体   中英

Swift 2, SpriteKit, I'm having trouble removing a node from parent

I'm sort of new to Swift so please forgive me if this is confusing.

I'm trying to get one of my nodes "guava" to disappear when "ninja" collides with it.

Currently my code it set up in my createScene() function like so:

        func spawnGuava(){

        //The way the Guava spawn and move

        let guavaNode = SKNode()

        let guava = SKSpriteNode(texture: guavaNodeTexture)

        guava.setScale(0.75)
        guava.position = CGPointMake (self.size.width + 160, self.size.height * 0.05)
        guava.physicsBody = SKPhysicsBody(rectangleOfSize: guava.size)
        guava.physicsBody?.affectedByGravity = false
        guava.physicsBody?.dynamic = true
        guava.alpha = 0.75
        guava.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
        guava.physicsBody?.collisionBitMask = PhysicsCatagory.None
        guava.physicsBody?.contactTestBitMask = PhysicsCatagory.Ninja

        guava.zPosition = 0

        guavaNode.addChild(guava)


        guava.runAction(GuavaMoveAndRemove)

        self.addChild(guavaNode)

        func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){

            guavaNode.removeFromParent()

        }

        func didCollideWithGuava(contact: SKPhysicsContact){
            var firstBody: SKPhysicsBody
            var secondBody: SKPhysicsBody

            if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
                firstBody = contact.bodyA
                secondBody = contact.bodyB
            }
            else{
                firstBody = contact.bodyB
                secondBody = contact.bodyA
            }

            if ((firstBody.categoryBitMask & PhysicsCatagory.Guava != 0) && (secondBody.categoryBitMask & PhysicsCatagory.Ninja != 0)) {
                ninjaAteGuava(firstBody.node as! SKSpriteNode, guava: secondBody.node as! SKSpriteNode)

            }
        }

Here's some info about the ninja

  let ninjaTexture = SKTexture(imageNamed:"ninja")
    ninjaTexture.filteringMode = SKTextureFilteringMode.Nearest

    ninja = SKSpriteNode(texture: NinjaTexture)
    ninja.setScale(0.5)
    ninja.position = CGPoint(x: self.frame.size.width * 0.50, y: self.frame.size.height * 0.6)

    ninja.physicsBody = SKPhysicsBody(circleOfRadius: ninja.size.height / 2)
    ninja.physicsBody?.categoryBitMask = PhysicsCatagory.Ninja
    ninja.physicsBody?.collisionBitMask = PhysicsCatagory.Ground |  PhysicsCatagory.Wall | 
    ninja.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Guava
    ninja.physicsBody!.dynamic = true
    ninja.physicsBody!.allowsRotation = false

    self.addChild(ninja)

Extra Info:

struct PhysicsCatagory {

static let None : UInt32 = 0
static let Boognish : UInt32 = 0x1 << 1
static let Ground : UInt32 = 0x1 << 2
static let Wall : UInt32 = 0x1 << 3
static let Guava : UInt32 = 0x1 << 4
static let Pepper: UInt32 = 0x1 << 5

}

The guavaNode should be guava in the ninjaAteGuava function

func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){

        guavaNode.removeFromParent()

}

Should be:

func ninjaAteGuava(ninja: SKSpriteNode, guava: SKSpriteNode){

        guava.removeFromParent()

}

This is because your parameter in the function is guava; not guavaNode. I don't know why Xcode isn't giving you an error, but this should work. Hope this helps.

Have you added guavaNode to the scene or guava ? Because you're removing guavaNode from the scene, but I suspect you may have added guava to the scene inside guava.runAction(GuavaMoveAndRemove)

or maybe there something else inside that action that could trigger your bug?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM