Reputation: 189
I am currently working on a game that consists of three scenes, the GameScene, ArcheryScene, and GameOverScene. When I start the game the GameScene presents just fine, then I tap on the screen to start the game and the ArcheryScene presents just fine. The GameOverScene gets triggered when a contact is made in the game, and it works as well. However, once the GameOverScene is presented I would like to be able to tap on the screen to go back to the ArcheryScene and restart the game. However when I tap on the screen, nothing happens, at all. Any advice or suggestions would be great appreciated, thank you.
GameScene Code that presents the ArcheryScene when tapped: (works)
import UIKit
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
let welcomeNode = childNodeWithName("welcomeNode")
if (welcomeNode != nil) {
let fadeAway = SKAction.fadeOutWithDuration(1.0)
welcomeNode?.runAction(fadeAway, completion: {
let doors = SKTransition.pushWithDirection(SKTransitionDirection.Down, duration: 1.0)
let archeryScene = ArcheryScene(fileNamed: "ArcheryScene")
archeryScene.scaleMode = .AspectFill
self.view?.presentScene(archeryScene, transition: doors)
})
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
ArcheryScene code that presents the GameOverScene when a contact is made: (works)
let fadeOut = SKAction.sequence([SKAction.waitForDuration(3.0)])
let welcomeReturn = SKAction.runBlock({
let Transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let gameOverScene = GameScene(fileNamed: "GameOverScene")
gameOverScene.scaleMode = .AspectFill
self.scene!.view?.presentScene(gameOverScene, transition: Transition)
})
let sequence = SKAction.sequence([fadeOut, welcomeReturn])
self.runAction(sequence)
GameOverSceneCode that presents the ArcheryScene when tapped: (doesn't work)
import UIKit
import SpriteKit
class GameOverScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
let gameOverNode = childNodeWithName("gameOverNode")
if (gameOverNode != nil) {
let fadeAway2 = SKAction.fadeOutWithDuration(1.0)
gameOverNode?.runAction(fadeAway2, completion: {
let doors2 = SKTransition.pushWithDirection(SKTransitionDirection.Down, duration: 1.0)
let archeryScene = ArcheryScene(fileNamed: "ArcheryScene")
archeryScene.scaleMode = .AspectFill
self.view?.presentScene(archeryScene, transition: doors2)
})
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
Upvotes: 1
Views: 812
Reputation: 8130
gameOverNode doesnt exist. Youre saying let gameOverNode = childNodeWithName("gameOverNode")
.. gameOverNode is nil! It's nothing, and so that block of code in the conditional statement isnt doing anything.
you need to add a node named "gameOverNode" in order to reference it. childNodeWithName
references a node that already exists in your scene.. its not creating a new one.
Upvotes: 0