0

I'm trying to reset the whole scene when the game is over by loading the same scene again. But after it has loaded, the screen does not register any touches. Not even touchesBegan work. Everything else is loaded, sprites, animations etc. but touches does not register.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    player.physicsBody?.dynamic = true
    player.physicsBody?.velocity.dy = CGFloat(500)
    createLeftButton()
    createRightButton()

    createRightButtonFirst()
    createLeftButtonFirst()
}

func endGame() {
    gameOver = true
    scoreboardSetup()
    playButton()
    highscoreButton()
    label!.hidden = true
    timer2.invalidate()
    timer1.invalidate()
    player.physicsBody?.dynamic = false
}


func playButton() {
    let image = UIImage(named: "playButton") as UIImage?
    playbtn = UIButton();
    playbtn.setImage(image, forState: .Normal)
    playbtn.frame = CGRectMake(CGRectGetMidX(self.frame) - 152, CGRectGetMidY(self.frame) + 65, 133, 76) // X, Y, width, height

    playbtn.addTarget(self, action: #selector(GameScene.playBtn), forControlEvents: .TouchUpInside)
    self.view!.addSubview(playbtn)
}
func playBtn() {
    playbtn.removeFromSuperview()
    highscorebutton.removeFromSuperview()
    self.removeAllChildren()
    self.removeAllActions()
    self.scene?.removeFromParent()
    let gameScene:GameScene = GameScene(size: self.size) // create your new scene
    let transition = SKTransition.fadeWithDuration(1.0) // create type of transition (you can check in documentation for more transtions)
    self.view!.presentScene(gameScene, transition: transition)

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
NowYouKnow
  • 67
  • 9

1 Answers1

0

Solved! Had to remove all the subviews from the scene. Easiest way to do that was like this:

view.subviews.forEach({ $0.removeFromSuperview() }) // this gets things done
view.subviews.map({ $0.removeFromSuperview() }) // this returns modified array

source: How to remove all subviews of a view in Swift?

Community
  • 1
  • 1
NowYouKnow
  • 67
  • 9