尝试添加已在swift中拥有父级的SKNode

时间:2015-02-27 19:45:42

标签: ios xcode swift sprite-kit

我想生成一个可以通过点击删除的节点,我可以生成它,有时甚至点击它并将其删除,但随后它会回来或者它开始做出奇怪的动作,直到游戏崩溃。这是控制台中出现的内容:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'cuadradoRojo' (164 x 164)] position:{362.90328979492188, 790.66668701171875} size:{54.666667938232422, 54.666667938232422} rotation:0.00'
*** First throw call stack:
(0x183dea59c 0x19453c0e4 0x183dea4dc 0x18841c0fc 0x1000e73d0 0x1000e7d34 0x1000e8038 0x1000e8d44 0x18840e9ac 0x18840bfd8 0x188409038 0x188435fd8 0x100364a9c 0x187f3d280 0x187f3d118 0x184f398d0 0x183d8d5e4 0x183da2200 0x183da2160 0x183da00e0 0x183ccd0a4 0x18ce675a4 0x1885feaa4 0x10010f0c4 0x10010f104 0x194baaa08)
libc++abi.dylib: terminating with uncaught exception of type NSException

我没有经验编程,所以我不知道如何解释。这是我的代码,希望你能帮忙。感谢。

import SpriteKit
enum BodyType : UInt32 {

    case colorAzul  = 1
    case circuloAzul = 2
    case colorRojo = 4
    case circuloRojo = 8
    case cuadrado = 16

}
class Scene: SKScene , SKPhysicsContactDelegate{

    var cuadradoRojo = SKSpriteNode(imageNamed: "cuadradoRojo")
    var lastYieldTimeInterval:NSTimeInterval = NSTimeInterval()
    var lastUpdateTimerInterval:NSTimeInterval = NSTimeInterval()
    var velocity:CGFloat = 0
    var gameOver = false
    var score = 0

    override func didMoveToView(view: SKView) {

    }
    func addAlien(){
        cuadradoRojo.physicsBody = SKPhysicsBody(circleOfRadius: cuadradoRojo.size.width)
        cuadradoRojo.physicsBody?.dynamic = true
        cuadradoRojo.physicsBody?.categoryBitMask = BodyType.cuadrado.rawValue
        cuadradoRojo.physicsBody?.contactTestBitMask = BodyType.colorAzul.rawValue | BodyType.colorRojo.rawValue
        cuadradoRojo.physicsBody?.collisionBitMask = 0

        var actionArray:NSMutableArray = NSMutableArray()
        if gameOver == false{


            let minX = cuadradoRojo.size.width/2
            let maxX = self.frame.size.width - cuadradoRojo.size.width/2
            let rangeX = maxX - minX
            let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)
            cuadradoRojo.position = CGPointMake(position, self.frame.size.height + cuadradoRojo.size.height)
            addChild(cuadradoRojo)

            let minDuration = 3
            let duration = Int(minDuration)
            actionArray.addObject(SKAction.moveTo(CGPointMake(position, -cuadradoRojo.size.height), duration: NSTimeInterval(duration)))
            cuadradoRojo.runAction(SKAction.sequence(actionArray))

        }



    }

    func updateWithTimeSinceLastUpdate(timeSinceLastUpdate:CFTimeInterval){
        var randomNum = Double(arc4random_uniform(20))
        var xTime = ((randomNum / 20) + 0.4)
        lastYieldTimeInterval += timeSinceLastUpdate
        if (lastYieldTimeInterval > xTime){
            lastYieldTimeInterval = 0
            randomNum = Double(arc4random_uniform(25))
            addAlien()
        }

    }
    override func update(currentTime: CFTimeInterval) {

        var timeSinceLastUpdate = currentTime - lastUpdateTimerInterval
        lastUpdateTimerInterval = currentTime
        velocity = CGFloat(score*3)

        if (timeSinceLastUpdate > 1){
            timeSinceLastUpdate = 1/60
            lastUpdateTimerInterval = currentTime
        }
        self.physicsWorld.gravity = CGVectorMake(0, -velocity)
        updateWithTimeSinceLastUpdate(timeSinceLastUpdate)


    }
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.cuadradoRojo{

                cuadradoRojo.removeFromParent()
            }

        }
    }

}

1 个答案:

答案 0 :(得分:2)

在调用addChild(cuadradoRojo)之前,请调用cuadradoRojo.removeFromParent(),因此不要将其两次添加到其父级SKScene