Attemped to add a SKNode which already has a parent and Thread 1 : signal SIGABRT

0

I try to use my class Asteroid but I keep getting this error message. I tried everything but I cant seem to figure it out.

There is just one asteroid that appears and then appears a thread on

AppDelegate : Thread 1: signal SIGABRT

I am a beginner in swift and any help would be welcome.

class Asteroides:SKSpriteNode {

    init(img:String){
        let texture = SKTexture(imageNamed: img)
        super.init(texture: texture, color: .clear, size: texture.size())
        self.name = "Asteroid"
        self.setScale(0.2)
        self.zPosition = 2

        self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/3.3, center: self.anchorPoint)
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.categoryBitMask = GameScene.physicsCategories.asteroid
        self.physicsBody!.collisionBitMask = GameScene.physicsCategories.none
        self.physicsBody!.contactTestBitMask  = GameScene.physicsCategories.planet | GameScene.physicsCategories.bullet
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func random() -> CGFloat {
        return CGFloat(Float(arc4random()) / 0xFFFFFFFF) //générer un nombre aléatoire
    }


    func random(min: CGFloat, max: CGFloat) -> CGFloat {
        return random() * (max - min) + min
    }


    func addAsteroid (parent: GameScene)  {

        let randomStart1 = random(min: self.size.height/2, max: parent.size.height - self.size.height/2)
        let startPoint1 =  CGPoint(x:randomStart1 , y:parent.size.width + self.size.width/2 )
        let endPoint1   =  CGPoint(x: randomStart1, y: -self.size.width/2)

        self.position = startPoint1
        parent.addChild(self)

        let moveAsteroid = SKAction.move(to: endPoint1 , duration: 3.5)
        let deleteAsteroide = SKAction.removeFromParent()
        let asteroidSeq = SKAction.sequence([moveAsteroid,deleteAsteroide])

        self.run(SKAction.repeatForever(asteroidSeq))
    }
 }

In the GameScene :

 private var asteroide = Asteroides(img: "Asteroid")

    func newlevel(){
        func parametersLevel(spawn : SKAction){
            let spawning = spawn
            let waitSpawn = SKAction.wait(forDuration: 0.8)
            let sequence = SKAction.sequence([waitSpawn,spawning])//sequence:asteroides+attendre 0.8 sec
            let spawnForever = SKAction.repeatForever(sequence)
            self.run(spawnForever)
        }
        parametersLevel(spawn:SKAction.run(asteroids))
    }

    func asteroids (){
        asteroide.addAsteroid(parent: self)
     }
ios
swift
sprite-kit
sigabrt
asked on Stack Overflow Apr 13, 2019 by hanna • edited Apr 14, 2019 by Anand

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0