I know SpriteKit already handles pausing the game when the app enters the inactive state but what I'm trying to do is add a SKLabelNode "tap to resume" when the app re-enters the active state. Right now it's calling my functions correctly and pausing the game, but the text is not showing.
AppDelegate.swift
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
println("applicationWillResignActive")
NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self)
...
}
GameScene.swift
class GameScene: SKScene, SKPhysicsContactDelegate {
...
let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
...
override func didMoveToView(view: SKView) {
...
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil)
tapToResume.text = "tap to resume"
tapToResume.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
tapToResume.fontSize = 55
tapToResume.hidden = true
self.addChild(tapToResume)
...
}
func pauseGameScene() {
println("pause game")
self.view?.paused = true
}
func showPauseText() {
if self.view?.paused == true {
tapToResume.hidden = false
println("show text")
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
...
if self.paused {
self.view?.paused = false
if tapToResume.hidden == false {
tapToResume.hidden = true
}
}
}
...
}
EDIT:
Below is a screenshot of my terminal output with my latest edits to my above code:
So I "hacked" my solution here. Thanks to ABakerSmith with the suggestion of setting self.speed = 0.0, the actions were paused and the my label will appear but the physicsWorld was still active. So my solution was to set self.speed = 0.0 AND self.physicsWorld.speed = 0.0. When the app returns from the inactive state, I just reset self.speed = 1.0 and self.physicsWorld.speed = 1.0. I'm sure there are other solutions to this dilemma but since SpriteKit already handles interruptions, all I really needed to do was pause the actions and the physics.
GameScene.swift
class GameScene: SKScene, SKPhysicsContactDelegate {
let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
...
override func didMoveToView(view: SKView) {
...
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil)
}
func pauseGameScene() {
self.physicsWorld.speed = 0.0
self.speed = 0.0
}
func showPauseText() {
if self.physicsWorld.speed == 0.0 {
tapToResume.hidden = false
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
...
if self.physicsWorld.speed == 0.0 {
self.physicsWorld.speed = 1.0
self.speed = 1.0
if tapToResume.hidden == false {
tapToResume.hidden = true
}
}
}
...
}
AppDelegate.swift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self)
}
...
}
I believe your problem was setting self.view?.paused = true, as was pointed out by #Steve in a comment and #Linus G. in his answer. Therefore, when you tried to unhide the label, nothing happened because the view was paused.
I tried using self.paused to pause the SKScene instead. This solved the problems of showing the label, however it didn't actually pause the scene. This could be due to: since iOS8, SpriteKit automatically pauses your game when it enters the background and un-pauses the game when it enters the foreground. Therefore, trying to set self.paused = true, using applicationWillResignActive, had no effect because it was un-paused when entering the foreground.
To solve this you can observe UIApplicationWillResignActiveNotification. Then when the application is going to resign being active, you set self.speed = 0, which has the same effect as pausing the SKScene. This displays the label and pauses the scene as required.
For example:
class GameScene: SKScene {
let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
override func didMoveToView(view: SKView) {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("pauseScene"),
name: UIApplicationWillResignActiveNotification,
object: nil)
tapToResume.text = "tap to resume"
tapToResume.position = CGPoint(x: frame.midX, y: frame.midY)
tapToResume.fontSize = 55
tapToResume.hidden = true
self.addChild(tapToResume)
}
func pauseScene() {
self.speed = 0.0
tapToResume.hidden = false
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// Check the label was pressed here.
if labelWasPressed {
self.speed = 1.0
tapToResume.hidden = true
}
}
I think I got the problem. You should call pauseGameScene() first. Then view?.paused is true. Then you can call showPauseText().
Hope that helps :)
Related
I have set play/pause button to gameLayer - here: How to prevent run SKAction on paused scene (after unpaused), texture of node not change after pause/unpause scene
Everything works fine, but I'm helpless with app on background. When I pause gameLayer and app goes to background or when I lock device, after app going to foreground, game layer is automatically unpause. How to prevent this?
Thanks!
So this is what worked for me:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var gameLayer = SKNode()
override func didMove(to view: SKView) {
NotificationCenter.default.addObserver(self,
selector: #selector(GameScene.pause),
name: .UIApplicationDidBecomeActive,
object: nil)
let moveUp = SKAction.moveBy(x: 0, y: 100, duration: 2)
let sequence = SKAction.sequence([moveUp, moveUp.reversed()])
let loop = SKAction.repeatForever(sequence)
let sprite = SKSpriteNode(color: .purple, size: CGSize(width: 100, height: 100))
addChild(gameLayer)
gameLayer.addChild(sprite)
sprite.run(loop)
}
func pause(){
gameLayer.speed = 0
}
override func willMove(from view: SKView) {
super.willMove(from: view)
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive,
object: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
gameLayer.speed = gameLayer.speed == 0 ? 1 : 0
}
}
I haven't had time to test more, but I was expecting that something like this:
func pause(){
gameLayer.isPaused = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
gameLayer.isPaused = !gameLayer.isPaused
}
would work, but surprisingly it isn't (the node continued with moving after the pause() method was executed). I don't really know if this is a bug or a feature :) but IMHO, I would say it is a bug. Anyways, you can achieve what you want using the speed property. Just copy and paste this code to see how it works.
I've made a maybe not that beautiful, but a very easy workaround.
Just added var gamePaused = false to the Scene class, which is set to true in pause(), and then in
override func update(_ currentTime: TimeInterval) {
guard gamePaused == false else {
if playLayer.isPaused == false {playLayer.isPaused = true}
return
}
.....
}
So each time the app start from the background, the playLayer.isPaused is set to true just with the first rendered frame.
I'm making a game using sprite kit and I want my Character to move across the screen when you hold down the left/right move button. The problem is that he only moves when the button is tapped, not held. I have looked everywhere for a solution but nothing seems to work!
Here's my code;
class Button: SKNode
{
var defaultButton: SKSpriteNode // defualt state
var activeButton: SKSpriteNode // active state
var timer = Timer()
var action: () -> Void
//default constructor
init(defaultButtonImage: String, activeButtonImage: String, buttonAction: #escaping () -> Void )
{
//get the images for both button states
defaultButton = SKSpriteNode(imageNamed: defaultButtonImage)
activeButton = SKSpriteNode(imageNamed: activeButtonImage)
//hide it while not in use
activeButton.isHidden = true
action = buttonAction
super.init()
isUserInteractionEnabled = true
addChild(defaultButton)
addChild(activeButton)
}
//When user touches button
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
action()
//using timer to repeatedly call action, doesnt seem to work...
self.timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(getter: Button.action), userInfo: nil, repeats: true)
//swtich the image of our button
activeButton.isHidden = false
defaultButton.isHidden = true
}
code..........
In my game scene...
// *** RIGHT MOVEMENT ***
let rightMovementbutton = Button(defaultButtonImage: "arrow", activeButtonImage: "arrowActive", buttonAction:
{
let moveAction = SKAction.moveBy(x: 15, y: 0, duration: 0.1)
self.player.run(moveAction)
})
You know when the button is touched because touchesBegan is called. You then have to set a flag to indicate that the button is pressed.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if leftButton.containsPoint(touch.locationInNode(self)) {
leftButtonIsPressed = true
}
if rightButton.containsPoint(touch.locationInNode(self)) {
rightButtonIsPressed = true
}
}
In update(), call your function that flag is true:
update() {
if leftButtonIsPressed == true {
moveLeft()
}
if rightButtonIsPressed == true {
moveRight()
}
}
You set the flag to false when touchesEnded is called for that button:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if leftButton.containsPoint(touch.locationInNode(self)) {
leftButtonIsPressed = false
}
if rightButton.containsPoint(touch.locationInNode(self)) {
rightButtonIsPressed = flase
}
}
Edit:
As pointed out by KoD, a cleaner way to do this (for movement buttons) is with SKAction which removes the need for the flag:
Define SKActions for moveTo x:0 and moveTo x:frame.width in
didMoveTo(View:)
In touchesBegan, run the correct SKAction on the correct object
specifying a key for the SKAction.
In touchesEnded, remove the relevant SKAction.
You'll have to do some maths to calculate how many points your object will have to move and then set a duration for the SKAction based upon this distance and a movement speed (in points per second).
Alternatively, (thanks to KnightOfDragons for this) create a SKAction.MoveBy x: which moves a small distance (based upon your desired movement speed) and with a duration of 1/60s. Repeat this action forever (SKAction.repeatForever) when the button is touched and remove the repeating SKAction when the button is released.
Sometime ago I was having a problem with pausing my game when didBecomeActive, then I found a solution which I thought was working, until now.
I found out that iOS automatically pauses my game (all of it) when I leave, not terminate, a game; and when coming back (didBecomeActive), it unpauses. As my point was to pause a singular layer (gameLayer), I created a boolean variable and an if condition to check if my game is paused or not.
If checkPause == false (not paused) -> it'll call a pausing function (that works great) when coming back to the game (moments after?! being unpaused by the system)
If checkPause == true (paused) -> it'll will set gameLayer.paused = false (unpaused by the system) to true (once it was paused before leaving the game)
Basically gameLayer is not being paused when coming back. It looks like iOS unpauses my game after didBecomeActive function.
I made an example project with it's code below (it's all commented and the simplest it could get)
If you want, you can download here.
import SpriteKit
class GameScene: SKScene {
//Declarations
var gameLayer = SKNode()
var pauseLayer = SKNode()
var checkPause = Bool() //checkPause == true -> gameLayer is paused | checkPause == false -> gameLayer is not paused
var enemy = SKSpriteNode()
var pauseButton = SKSpriteNode()
var playButton = SKSpriteNode()
//"Cage" objects in the screen
func cageObjects(){
//"caging" every object
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
}
//Setup
func setupPauseButton(){
//Pause
pauseButton = SKSpriteNode (imageNamed: "pause")
pauseButton.setScale(1)
pauseButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 1.2)
}
func setupPlayButton(){
//Play
playButton = SKSpriteNode (imageNamed: "play")
playButton.setScale(1)
playButton.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 1.2)
}
func setupEnemy(){
//Enemy
enemy = SKSpriteNode(imageNamed: "enemy")
enemy.position = CGPointMake(self.frame.width / 1, self.frame.height / 2)
enemy.name = "enemy"
enemy.setScale(0.5)
enemy.physicsBody?.affectedByGravity = false
}
//Layers
func createGameLayer(){
//pauseButton
setupPauseButton()
gameLayer.addChild(pauseButton) //add pauseButton to gameLayer
}
func createPauseLayer(){
//playButton
setupPlayButton()
pauseLayer.addChild(playButton) //add playButton to pauseLayer
}
//Spawn
func spawnEnemy(){
//Start spawning, moving and removing
let spawnEnemy = SKAction.runBlock({
() in
//Spawn enemy
self.setupEnemy()
self.gameLayer.addChild(self.enemy)
//Move left and remove when go off screen
let frameWidth = CGFloat(self.frame.width)
let moveEnemy = SKAction.moveByX(-frameWidth - 50, y: 0, duration: NSTimeInterval(0.0028 * frameWidth)) //duration: faster or slower
let removeEnemy = SKAction.removeFromParent()
var moveAndRemove = SKAction()
moveAndRemove = SKAction.sequence([moveEnemy, removeEnemy])
self.enemy.runAction(moveAndRemove)
})
//Spawn enemy each 2 seconds
let spawnEnemyDuration = SKAction.repeatActionForever(SKAction.sequence([spawnEnemy, SKAction.waitForDuration(2.0)]))
gameLayer.runAction(spawnEnemyDuration)
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
print ("didMoveToView")
registerAppTransitionObservers()
cageObjects()
checkPause = false
createGameLayer()
createPauseLayer()
self.addChild(gameLayer)
spawnEnemy()
}
//Game states
func pauseState(){
//Pause game
pauseButton.hidden = true //hide pauseButton
gameLayer.paused = true //pause gameLayer
checkPause = true //game is paused
self.addChild(pauseLayer) //add pauseLayer
}
func playState(){
pauseLayer.removeFromParent() //remove pauseLayer
//Resume game
checkPause = false //game is not paused
gameLayer.paused = false //unpause gameLayer
pauseButton.hidden = false //show pauseButton
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
//When touch buttons/screen
for touch in touches{
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if node == pauseButton{
pauseState()
}
else if node == playButton{
playState()
}
}
}
//Functions from AppDelegate
func registerAppTransitionObservers(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameScene.applicationDidBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)
}
//Just launched
func applicationDidBecomeActive(){
print("DidBecomeActive")
//gameLayer unpausing problem solving attempt
if checkPause == true{
gameLayer.paused = true
}
//Pause when game is not paused and user leave the screen OR when game is launched
else if checkPause == false{
pauseState()
}
}
}
Now I have your source, I see your problem.
You need to preserve your pause state:
class GameScene : SKScene
{
...
override var paused: Bool
{
get{
return super.paused;
}
set{
let value = self.gameLayer.paused
super.paused = newValue;
self.gameLayer.paused = value;
}
}
}
For some reason, scene paused is deciding to unpause all nodes under it
Also, you should pause your game when you leave the app, not when you come back.
//Functions from AppDelegate
func registerAppTransitionObservers(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameScene.applicationWillResign), name: UIApplicationWillResignActiveNotification, object: nil)
}
func applicationWillResign(){
print("WillResignActive")
pauseState()
}
And you can get rid of that check paused variable, that is redundant bloat code.
I'm trying to fade in an object into my game when I first touch the screen, but I think that because it was hidden before (when game was launched), it won't fade in, but only show without any animation.
Do you have any suggestions?
This is an example code:
import SpriteKit
class GameScene: SKScene {
var myLabel = SKLabelNode()
var gameStarted = Bool()
func setupMyLabel(){
myLabel = SKLabelNode(fontNamed:"Chalkduster")
myLabel.text = "Hello, World!"
myLabel.fontSize = 35
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
setupMyLabel()
self.addChild(myLabel)
myLabel.hidden = true
gameStarted = false
}
func startGame(){
myLabel.hidden = false
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
if gameStarted == false{
gameStarted = true
startGame()
self.myLabel.runAction(SKAction.fadeInWithDuration(2.0))
}
else{
//do nothing
}
}
}
According to Apple's documentation on fadeInWithDuration it states that:
When the action executes, the node’s alpha property animates from its
current value to 1.0.
So you're right in thinking it's because your node is hidden when it starts. =)
One possible solution would be to instead of setting the node's hidden property to true, instead set it's alpha value to 0. Or you could even create your own method to perform that includes the runAction method that would set the alpha to 0, un-hide the node, and then call SKAction.fadeInWithDuration similar to something below (please forgive any syntax errors, this is free-hand pseudo code)...
startGame()
self.fadeIn(self.myLabel, duration: 2.0)
...
func fadeIn() {
self.myLabel.alpha = 0.0
self.myLabel.hidden = false
self.myLabel.runAction(SKAction.fadeInWithDuration(2.0))
}
I have the following code to pause game scene:
class GameProcessScene: SKScene {
...
var onPause: Bool = false {
willSet {
self.paused = newValue
self.view?.paused = newValue
}
}
...
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch in touches {
let location = touch.locationInNode(self)
let possiblePauseNode = self.nodeAtPoint(location)
if (possiblePauseNode.name == "pauseButton") {
if (self.paused) {
onPause = false
} else {
onPause = true
}
return
}
...
}
}
It works to pause by pressing button. But i also want to pause game when it appears from background. I am trying to do this from ViewController class:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGameScene", name: UIApplicationWillEnterForegroundNotification, object: nil)
}
func pauseGameScene() {
let view = self.view as SKView
let scene = view.scene
switch scene {
case is MainMenuScene:
println("MainMenu")
case let game as GameProcessScene:
game.onPause = true
default:
println("default")
}
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
But it doesnt work when game appears from background.
Can anybody help? Thanks!
UPD1: Also tried UIApplicationWillResignActiveNotification, UIApplicationDidEnterBackgroundNotification but game.onPause = true still doesnt work. It changes "SKScene.paused" property to true, but the actions are still executing when game enters foreground, even game scene was paused before it goes background.
UPD2: I moved observers and selectors from ViewController class to GameProcessScene: SKScene class - same effect, "paused" property changes, but in fact gameplay is continuing.
I solved my problem by creating a property called isPaused and overriding the paused property to always follow the isPaused property. Note : This disables the SKSCene from automatically pausing when going to background mode. You have to manually set the isPaused property. So only use it in scenes where you actually need the pause mode on resume.
class GameScene: SKScene,SKPhysicsContactDelegate {
// Initializers
var isPaused : Bool = false {
didSet {
self.paused = isPaused
}
}
override var paused : Bool {
get {
return isPaused
}
set {
super.paused = isPaused
}
}
}
In ViewController
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "pauseGameScene", name: UIApplicationWillResignActiveNotification, object: nil)
}
func pauseGameScene() {
println("will become inactive")
let view = self.view as SKView
let scene = view.scene
switch scene {
case let game as GameScene:
game.isPaused = true
default:
println("default")
}
}
Did you try put your code into
override func viewWillAppear(animated: Bool) {
//Put your pauseGameScene(); code here
}
This method called before your view appears every times. The viewDidLoad() run only once after your instance is initialized.