Let me preface this question by saying I know it has been asked a few times, but the most upvoted post here was from back in 2014 and the top answer doesn't seem to work anymore.
Issue
In my game, I have all my 'moving' nodes as children of pauseNode. When the pauses button is pressed or when the player leaves the app, pauseScene() is called:
func pauseScene() {
self.node.speed = 0
self.physicsWorld.speed = 0
self.node.isPaused = true
}
This pauses all sprite nodes and emitter nodes. This keeps the sprite nodes paused if the app transitions from background to foreground state, but the particle emitters seem to resume animating.
Current solution
I solved this issue by triggering pauseScene() after a small delay when the app became active.
override func didMove(to view: SKView) {
NotificationCenter.default.addObserver(self, selector: #selector(GameScene.applicationDidBecomeActive(notification:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: app)
}
#objc func applicationDidBecomeActive(notification: NSNotification) {
NSDelay(0.01) {
pauseScene()
}
}
func NSDelay(_ delay:Double, closure:#escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
Goal
While this solution pauses the particles, it is not perfect because they are visible moving for the split second it takes to pause them again (this is the shortest delay that actually re-pauses them).
So, after reading the post I linked above, I tried to override applicationDidBecomeActive() in an SKView subclass as was suggested, but I couldn't get the method to be called. I want to prevent these particles from being unpaused in the first place, if possible. Thanks
So, it's been a little over a day and I've found a better solution. A comment in this post suggested having a variable that recorded the status of the scene, and updating it along with the pause/play functions:
var sceneIsPaused = false
func pauseScene() {
self.node.speed = 0
self.physicsWorld.speed = 0
self.node.isPaused = true
sceneIsPaused = true
}
func playScene() {
self.node.speed = 1
self.physicsWorld.speed = 1
self.node.isPaused = false
sceneIsPaused = false
}
Then, override SKView's update method and check the state of that variable. If the scene should be paused, pause it. This will mean that if the scene is automatically unpaused it will re-pause the next frame. This is much faster and cleaner than setting a delay:
override func update(_ currentTime: TimeInterval) {
if (sceneIsPaused == true) {
self.node.speed = 0
self.physicsWorld.speed = 0
self.node.isPaused = true
}
}
Related
Desired behavior is: when an action is removed from a node (with removeAction(forKey:) for instance) it stops to animate and all the changes caused by action are discarded, so the node returns back to pervious state. In other words, I want to achieve behavior similar to CAAnimation.
But when a SKAction is removed, the node remains changed. It's not good, because to restore it's state I need to know exactly what action was removed. And if I then change the action, I also will need to update the node state restoration.
Update:
The particular purpose is to show possible move in a match-3 game. When I show a move, pieces start pulsating (scale action, repeating forever). And when the user moves I want to stop showing the move, so I remove the action. As the result, pieces may remain downscaled. Later I would like to add more fancy and complicated animations, so I want to be able to edit it easily.
Thanks to the helpful comment and answer I came to my own solution. I think the state machine would be bit too heavy here. Instead I created a wrapper node, which main purpose is run the animation. It also has a state: isAimating property. But, first of all, it allows to keep startAnimating() and stopAnimating() methods close to each other, incapsulated, so it's more difficult to mess up.
class ShowMoveAnimNode: SKNode {
let animKey = "showMove"
var isAnimating: Bool = false {
didSet {
guard oldValue != isAnimating else { return }
if isAnimating {
startAnimating()
} else {
stopAnimating()
}
}
}
private func startAnimating() {
let shortPeriod = 0.2
let scaleDown = SKAction.scale(by: 0.75, duration: shortPeriod)
let seq = SKAction.sequence([scaleDown,
scaleDown.reversed(),
scaleDown,
scaleDown.reversed(),
SKAction.wait(forDuration: shortPeriod * 6)])
let repeated = SKAction.repeatForever(seq)
run(repeated, withKey: animKey)
}
private func stopAnimating() {
removeAction(forKey: animKey)
xScale = 1
yScale = 1
}
}
Usage: just add everything that should be animated to this node. Works well with simple animations, like: fade, scale and move.
As #Knight0fDragon suggested, you would be better off using the GKStateMachine functionality, I will give you an example.
First declare the states of your player/character in your scene
lazy var playerState: GKStateMachine = GKStateMachine(states: [
Idle(scene: self),
Run(scene: self)
])
Then you need to create a class for each of these states, in this example I will show you only the Idle class
import SpriteKit
import GameplayKit
class Idle: GKState {
weak var scene: GameScene?
init(scene: SKScene) {
self.scene = scene as? GameScene
super.init()
}
override func didEnter(from previousState: GKState?) {
//Here you can make changes to your character when it enters this state, for example, change his texture.
}
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Run.Type //This is pretty obvious by the method name, which states can the character go to from this state.
}
override func update(deltaTime seconds: TimeInterval) {
//Here is the update method for this state, lets say you have a button which controls your character velocity, then you can check if the player go over a certain velocity you make it go to the Run state.
if playerVelocity > 500 { //playerVelocity is just an example of a variable to check the player velocity.
scene?.playerState.enter(Run.self)
}
}
}
Now of course in your scene you need to do two things, first is initialize the character to a certain state or else it will remain stateless, so you can to this in the didMove method.
override func didMove(to view: SKView) {
playerState.enter(Idle.self)
}
And last but no least is make sure the scene update method calls the state update method.
override func update(_ currentTime: TimeInterval) {
playerState.update(deltaTime: currentTime)
}
I'm making a card game with Swift 4 and SpriteKit. I made a custom subclass of SKScene, and inside this class is a SKNode which I'm using as a layer (gameLayer), to which I attach all Nodes, which shall be stopped.
I also have made a button, which calls a function (pauseGame) that toggles isPaused of the gameLayer, which works perfectly.
The problem is: I want that when the game moves to the scene, it starts already stopped, the user shall begin the game by pressing a button. But when I call the pauseGame function inside the didMove function, it doesn't work, the gameLayer remains active.
I made some observations putting ' print (gameLayer.isPaused) ' and frame count inside the update function. It showed that actually the scene starts with gameLayer.isPaused set to true inside the didMove as it should be, but after the 2nd frame it gets set to false. I have really no idea where it can happen, as I don't appeal to gameLayer.isPaused anywhere else in code.
Of course a solution would be to call pauseGame after the 2nd frame, but I think its not a clean way.
class BMScene: SKScene {
let gameLayer = SKNode()
func pauseGame(){
if gameLayer.isPaused {
gameLayer.isPaused = false
} else {
gameLayer.isPaused = true
}
}
override func didMove(to view: SKView) {
self.pauseGame()
}
isPaused is a bugged up mess in the world of SpriteKit. I have literally argued with developers at apple about the broken nature of it, and they claim that is how it is intended to work, and we must deal with it. The latest atrocity I know about, is that isPaused is defaulted to true for new scenes, then a message is sent out to unpause it. On top of that, when you unpause a parent node, it also unpauses all children nodes.
So in your code here, override the isPaused of your scene to be able to capture your pausing event to block it from going to its children
class BMScene: SKScene {
let gameLayer = SKNode()
private var _paused : Boolean = false
override var isPaused : Boolean
{
get
{
return _paused
}
set
{
_paused = newValue
}
}
func pauseGame(_ state: Boolean? = null){
gameLayer.isPaused = state ?? !gameLayer.isPaused
}
override func didMove(to view: SKView) {
self.pauseGame(true)
}
}
This will stop your node from changing its childrens pause state. After this moment, you should have complete control of your gameLayer pause state, and it should flow how you think it is flowing.
I have also modified your function to allow you to force the pause state, as well as toggle when you want to swap the state.
E.g.
Pause:
pauseGame(true)
'gameLayer.isPaused is now true
Unpause:
pauseGame(true)
'gameLayer.isPaused is now false
Toggle:
pauseGame()
'gameLayer goes from false to true, or true to false
?? Means COALESCE, so if state is null, it will move along the chain to the next value, which is !gameLayer.isPaused
I've solved similar problem by setting speed to zero:
gameLayer.speed = 0
// This will, obviously, unpause
gameLayer.speed = 1
I am coding an app in sprite-kit and swift where when you touch the screen a sprite(the player) throws a projectile at another sprite moving towards it. If the player hits the other sprite then the projectile and the sprite disappear. A problem with the game is that if the player rapidly touches the screen he can easily run up his score in the game. How can I make the code only recognize that the screen is being touched every let's say .3 seconds?
In SpriteKit/GameplayKit games, most of your code is running inside a game loop where you are constantly being passed the current time. That's what this function in an SKScene is:
override public func update(_ currentTime: TimeInterval) {
}
In here it's common to keep track of time and enable/disable things. To keep it simple:
Add the following vars
var firingEnabled = true
var enableFiringAtTime: TimeInterval = 0
var currentTime: TimeInterval = 0
When they fire, add this code
if firingEnabled {
firingEnabled = false
enableFiringAtTime = self.currentTime + 0.3
// your fire code here
}
And in the update override
self.currentTime = currentTime
if currentTime > enableFiringAtTime {
firingEnabled = true
}
I’m working with Xcode 8 and Swift 3, trying to display a “Resume Game” button on my scene after pausing the game but since everything stops, I have not found a way to properly do so. I followed Mike's advice on this page “Tap To Resume” Pause text SpriteKit, but that didn't help.
When my pause button is tapped, it runs the pause function with the following code:
func pauseGame() {
self.isPaused = true
self.physicsWorld.speed = 0
self.speed = 0.0
self.scene?.view?.isPaused = true
}
It may seem a little overkill but that works. If I remove
self.scene?.view?.isPaused = true
from my pause function, I'm able to display the tap to Resume Button but I can still interact with some of the SpriteKit nodes in the scene. I'm working on a space shooter game so the user can still move the spceship and tap to fire although the bullets don't travel until I resume the scene.
I thought about adding a boolean "true" to the pause function and adding an IF statement to the firing and the moving above but that seems to me like complicating things a bit.
Any suggestions on how I can display the Resume button when pausing the scene?
You can't interact with any nodes on your scene because you've paused it entirely, which pauses all its children, which is everything on the scene. To avoid this, pause only certain SKNodes (layers).
Add certain nodes to different SKNodes so instead of pausing the entire scene, you can only pause the the layer (SKNode) that you wish to pause (gameLayer). It would look something like this:
Initialize the nodes
let gameLayer = SKNode()
let pauseLayer = SKNode()
Now when you want to add a child to the scene, instead add it to the layer that you want it to be a part of:
Add child nodes to the main layers
gameLayer.addChild(gameSceneNode)
pauseLayer.addChild(resumeButton)
Don't forget to add the layers to the scene too
Add the layers to the scene
addChild(gameLayer)
addChild(pauseLayer)
To pause a layer write this:
gameLayer.isPaused = true
Note that in this example, all the nodes on the gameLayer will be paused, however everything on the pauseLayer will not.
Your complete example might look something like this:
func pauseGame() {
gameLayer.isPaused = true
pauseLayer.isHidden = false
gameLayer.physicsWorld.speed = 0
gameLayer.speed = 0.0
}
func unpauseGame() {
gameLayer.isPaused = false
pauseLayer.isHidden = true
// Whatever else you need to undo
}
For my next game I'm sure I'll give Nik's suggestion a try. As for my issue above, it worked by using the code shown below. The additional code is to dismiss the pause button as well. Now that this works I can maybe add a fadeIn and fadeOut actions to both buttons.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches {
let pointOfTouch = touch.location(in: self)
let nodeUserTapped = atPoint(pointOfTouch)
if nodeUserTapped.name == "PauseButton" {
if (self.isPaused == false) {
pauseGame()
}
}
if nodeUserTapped.name == "ResumeButton" {
if (self.isPaused == true) {
resumeGame()
}
}
}
}
// MARK: - pauseGame
func pauseGame() {
self.isPaused = true
currentGameState = gameState.pauseGame
self.physicsWorld.speed = 0
self.speed = 0.0
if (backgroundMusicIsOn == true) {
backingAudio.stop()
}
if resumeButton.isHidden == true {
resumeButton.isHidden = false
}
if pauseButton.isHidden == false {
pauseButton.isHidden = true
}
}
// MARK: - resumeGame
func resumeGame() {
self.isPaused = false
currentGameState = gameState.inGame
self.physicsWorld.speed = 1
self.speed = 1.0
if (backgroundMusicIsOn == true) {
backingAudio.play()
}
if resumeButton.isHidden == false {
resumeButton.isHidden = true
}
if pauseButton.isHidden == true {
pauseButton.isHidden = false
}
}
I have some nodes that I would like to run their action in the start menu. How would I do this? I have these clouds that move from left to right and I want to un pause them. I tried a lot of different things but cant seem to get it to work. Thanks here is the code but its not working for me. Im in spritekit swift.
override func didMoveToView(view: SKView) {
view.scene?.paused = true
}
override func update(currentTime: CFTimeInterval) {
//movement of clouds
cloud.paused = false
cloud2.paused = false
cloud3.paused = false
}
}
You can use the .speed property of your SKAction to pause and resume the action:
//Stops your action
yourAction.speed = 0
//Resumes your action
yourAction.speed = 1
Also don't put code in the update method which doesn't need to be called every frame. Put it inside a didMoveToView method or something similar.