簡體   English   中英

嘗試添加已經有父級的SKNode

[英]Attemped to add a SKNode which already has a parent in swift

我想生成一個可以通過單擊刪除的節點,我可以生成它,有時甚至可以單擊並刪除它,但是隨后又回來了,或者它開始做出奇怪的動作,直到游戲崩潰。 這就是控制台中顯示的內容:

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()
            }

        }
    }

}

在調用addChild(cuadradoRojo)之前,請調用cuadradoRojo.removeFromParent(),因此您無需將其兩次添加到其父級SKScene

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM