简体   繁体   English

Swift 2,SpriteKit,我无法从父节点删除节点

[英]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. 我是Swift的新手,如果这令人困惑,请原谅我。

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: 目前,我的代码是在我的createScene()函数中设置的,如下所示:

        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 guavaNodeninjaAteGuava function应为guava

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. 不是guavaNode。 I don't know why Xcode isn't giving you an error, but this should work. 我不知道为什么Xcode没有给你一个错误,但这应该可以。 Hope this helps. 希望这可以帮助。

Have you added guavaNode to the scene or guava ? 您是否已将guavaNode添加到场景或guava Because you're removing guavaNode from the scene, but I suspect you may have added guava to the scene inside guava.runAction(GuavaMoveAndRemove) 因为您要从场景中删除guavaNode ,但我怀疑您可能已在guava.runAction(GuavaMoveAndRemove) guava添加到了场景中

or maybe there something else inside that action that could trigger your bug? 还是该动作中还有其他可能触发您的错误的东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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