Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove SKSpriteNode after 5 seconds

How can i remove my SKSpriteNode after 5 seconds in my function like this. I have tried with a NSTimer called a func delete my BonusSprite but after 5 sec my application crash :

let timerApparitionBonus = NSTimer.scheduledTimerWithTimeInterval(13, target: self, selector: Selector("ApparitionBonus"), userInfo: nil, repeats: true)

    }

    func ApparitionBonus() {

        var BonusApparitionX = UInt32(self.frame.size.width)
        var BonusApparitionY = UInt32(self.frame.size.height)

        BonusApparitionX = arc4random() % BonusApparitionX
        BonusApparitionY = arc4random() % BonusApparitionY
        BonusSprite.position = CGPointMake(CGFloat(BonusApparitionX),CGFloat(BonusApparitionY))
        BonusSprite.setScale(0.8)
        self.addChild(BonusSprite)
    }

EDIT :

This is my code from DidMoveToView to function who delete my sprite.

override func didMoveToView(view: SKView) {

    physicsWorld.contactDelegate = self

    //BackGround

    self.scene?.backgroundColor = UIColor.blackColor()
    self.addChild(SKEmitterNode(fileNamed: "MyParticle")!)
    self.scene?.size = CGSize(width: 640, height: 1136)

    //Placement du Vaisseau :

    Vaisseau.setScale(2)
    Vaisseau.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
    Vaisseau.physicsBody = SKPhysicsBody(rectangleOfSize: Vaisseau.size)
    Vaisseau.physicsBody?.affectedByGravity = false
    Vaisseau.physicsBody?.categoryBitMask = PhysicsCategories.Vaisseau
    Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Meteorites
    Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Bonus
    Vaisseau.physicsBody?.dynamic = false
    self.addChild(Vaisseau)

    //Timer créer enemis

    CreationEnemisTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("CreationMeteorites"), userInfo: nil, repeats: true)

    //Score

    timerScore = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: Selector("ScoreUpper"), userInfo: nil, repeats: true)
    ScoreLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width / 3, height: 20))
    ScoreLabel.center = CGPoint(x : self.frame.size.width / 2,y : self.frame.size.height / 4)
    ScoreLabel.text = "Score : \(Score)"
    ScoreLabel.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 0.3)
    ScoreLabel.textColor = UIColor.whiteColor()
    self.view?.addSubview(ScoreLabel)

    //Aparition des Bonus (timer)

    let myFunction = SKAction.runBlock({self.ApparitionBonus()})
    let wait = SKAction.waitForDuration(5)
    let remove = SKAction.runBlock({self.removeBonus()})

    self.runAction(SKAction.sequence([myFunction, wait, remove]))
}

 

func ApparitionBonus() {

    var BonusApparitionX = UInt32(self.frame.size.width)
    var BonusApparitionY = UInt32(self.frame.size.height)

    BonusApparitionX = arc4random() % BonusApparitionX
    BonusApparitionY = arc4random() % BonusApparitionY

    BonusSprite.position = CGPointMake(CGFloat(BonusApparitionX),CGFloat(BonusApparitionY))
    BonusSprite.setScale(0.8)

    BonusSprite.physicsBody?.categoryBitMask = PhysicsCategories.Bonus
    BonusSprite.physicsBody?.contactTestBitMask = PhysicsCategories.Vaisseau
}

 

func removeBonus() {

    BonusSprite.removeFromParent()
}
like image 907
Mathis Delaunay Avatar asked Mar 14 '23 09:03

Mathis Delaunay


1 Answers

try this

EDIT: realized it wouldn't remove the sprite, this will work.

class GameScene: SKScene {
override func didMoveToView(view: SKView) {
      let myFunction = SKAction.runBlock({()in self.ApparitionBonus()})
      let wait = SKAction.waitForDuration(5)
      let remove = SKAction.runBlock({() in self.removeSprite()})
      self.runAction(SKAction.sequence([myFunction, wait, remove]))
    }
    func ApparitionBonus() {
       self.addChild(bonusSprite)
    }
    func removeSprite() {
       bonusSprite.removeFromParent()
    }
}
like image 88
Daniel Mihaila Avatar answered Mar 31 '23 20:03

Daniel Mihaila