removeFromParent() Doesn't Work in SpriteKit

被刻印的时光 ゝ 提交于 2019-12-11 23:16:23

问题


I have a loop in my SpriteKit game that randomly produces duplicates of an object and I'd like to use removeFromParent() to remove my object on contact. However, when I add it to the code, the object isn't removed. Here is my code for the loop that generates my object:

for i = 0; i<1000; ++i {
        var randomNumber = CGFloat(arc4random_uniform(3000)) * 200
        bubbleDuplicate.position = (CGPointMake(randomNumber, 400))
        bubbleDuplicate = bubble.copy() as! SKSpriteNode
        bubbleDuplicate.physicsBody?.categoryBitMask = bubbleCategory
        bubbleDuplicate.physicsBody?.collisionBitMask = 0
        bubbleDuplicate.physicsBody?.contactTestBitMask = 0

        addChild(bubbleDuplicate)

        func BubbleContact(){
            bubbleDuplicate.removeFromParent()
            bubble.removeFromParent()
            bubbleDuplicate.setScale(0)
            bubble.setScale(0)

        }

    }

and here's my code for the collision that is supposed to remove my object:

func didBeginContact(contact: SKPhysicsContact) {
    let collision:UInt32 = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask)

    if collision == (playerCategory | crateCategory) {

        gameOver()
    }
    if collision == (playerCategory | bubbleCategory) {
        NSLog("Bubble Contact")
        bubbleDuplicate.removeAllActions()
        bubbleDuplicate.removeFromParent()
        bubble.removeAllActions()
        bubble.removeFromParent()
        bubbleDuplicate.setScale(0)
        bubble.setScale(0)
        bubbleDuplicate.alpha = 0
        bubble.removeFromParent()
        bubbleDuplicate.removeFromParent()

               }


}

How can I fix my code?

EDIT: Thanks to @NikitaLeonov's answer, my bubble sprite now functions the way it's supposed to. However, I'm trying to apply the same functionality to another sprite node and simply duplicating his code will not produce the same effect. What do I do? Here's the new code:

if (contact.bodyA.categoryBitMask == bubbleCategory) {
        let node = contact.bodyB.node
        //Other remove routine
        NSLog("Bubble Contact")
        node?.removeAllActions()
        node?.removeFromParent()
    } else if (contact.bodyB.categoryBitMask == bubbleCategory) {
        let node = contact.bodyB.node
        //Other remove routine
        NSLog("Bubble Contact")
        node?.removeAllActions()
        node?.removeFromParent()
    }else if (contact.bodyA.categoryBitMask == wormCategory) {
        let node = contact.bodyB.node
        //Other remove routine
        NSLog("Worm Contact")
        node?.removeAllActions()
        node?.removeFromParent()
    }else if (contact.bodyB.categoryBitMask == wormCategory) {
        let node = contact.bodyB.node
        //Other remove routine
        NSLog("Worm Contact")
        node?.removeAllActions()
        node?.removeFromParent()
    }

回答1:


You use bubbleDuplicate and bubble as global variables and call removeFromParent to them, so only last generated bubble and bubbleDuplicate will be removed. As you can see it is not a right approach. You should take a node that need to be removed from a contact.bodyA and contact.bodyB variables in a contact method.

func didBeginContact(contact: SKPhysicsContact) {
    let collision:UInt32 = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask)

    if collision == (playerCategory | crateCategory) {
        gameOver()
    }

    if (contact.bodyA.categoryBitMask == bubbleCategory) {
      let node = contact.bodyA.node
      //Other remove routine
      node.removeAllActions() 
      node.removeFromParent()
    } else if (contact.bodyB.categoryBitMask == bubbleCategory) {
      let node = contact.bodyB.node
      //Other remove routine
      node.removeAllActions() 
      node.removeFromParent()
    }
}

Also If bubble and bubbleDouble somehow related I would try to setup a system with child nodes but will not manage them separately in a contact method. Also removal as well I would recommend as Action to simplify management of everything. So there are a lot of opportunities how to improve this code besides obvious fights with code duplication.

ps It seems I am contributing a lot into this game on SO :) You should give me a promo code when it will be published :D



来源:https://stackoverflow.com/questions/31152755/removefromparent-doesnt-work-in-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!