How to 'resume' game on SKScene1 after transitioning to SKScene2? - ios

Perhaps I'm not using the correct keywords to search but I haven't had any luck in finding out how to do this. I am developing a game for the AppStore using Xcode's sprite kit. I have the main game play on SKScenePlay.m and when we click a pause button (an SKSpriteNode) on SKScenePlay, I transition to SKScenePause with the following code:
SKScene *PauseScene = [[SKScenePause alloc] initWithSize:self.size];
[self.view presentScene:PauseScene];
I have a resume button on the SKScenePause that does the following to transition back to SKScenePlay:
SKScene *PlayScene = [[SKScenePlay alloc] initWithSize:self.size];
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
[self.view presentScene: PlayScene transition:reveal];
but when I go back to SKScenePlay, the game is reset (score, player position, et cetera). How would I, after pausing the game to go from SKScenePlay to SKScenePause, transition from SKScenePause to back to SKScenePlay while saving the state of everything in SKScenePlay?
P.S: To actually pause the game, all of the SKSpriteNode's (which are the objects in my game that move and fall around) are children of an SKNode *background, and I set background.paused=true and this pauses all the SKActions in SKScenePlay.

Using your current code, you can't. At least not in any short and simple way. Your code trashes the PlayScene and transitions to PauseScene.
Your choices are to:
1) Include pause code in your PlayScene to handle whatever it is you need done without transitioning to a new scene.
2) Save all your relevant data in your PlayScene before transitioning to PauseScene and loading the saved data when transitioning back to PlayScene.
There's really no reason to have a new scene for a pause menu, so I would recommend you work to incorporate your pause code into your PlayScene.

1)You need to add children view controller in the container view controller in a stack. So You just put the view of new view controller above first one.
2)when you press "play" or "resume" button you just remove top view controller from the stack and game resumes.
I have made absolutely the same but on swift.
hope it helps.

Related

Pause scene before transition and perform transition

I'm struggling with pause moment in my game.
I want to make button which perform pause logic and transition me to another scene with save action to sceneManager that holds gameScene.
Everything works fine but I can't make pause all actions my scene has. I read quite a lot of themes but still no luck.
The only think that really pauses any movement on my scene is
self.scene.view?.isPaused = true
But it pauses my transition as well.
I have spawning enemies, power-ups, shots and background stuff. So if I translate to other scene with this code:
transition.pausesOutgoingScene = true
transition.pausesIncomingScene = true
I see that objects and actions are not paused as well!
How to get pause logic work?
I added all nodes I want to pause to the same SKNode instance and after that I can just write:
someNodeWithSpritesToPause.isPaused = true
physicsWorld.speed = 0
This code solves my problem.
Thank you!

How to correctly pause the movements of sprites in a SpriteKit game?

I have a simple SpriteKit game, and in the appDelegate file in theapplicationWillResignActive(application: UIApplication)function, I put the code:GameScene().physicsWorld.speed = 0.0. However, when I close and reopen the app, a SKSpriteNode with a physicsBody attached carries on falling. I have also tried adding both these lines of code:
GameScene().speed = 0,
GameScene().paused = true. None of this works though. Any help would be appreciated.
You need to pause the SKView
self.scene?.view?.paused = true
Add an observer in your view controller, holding the view, for the willResignActive notification and then pause the SKView.
Add the observer for willEnterForeground to start it.

Swift: Presenting a Scene over another Scene

I'm having trouble finding this answer. If I have a settings scene that I want to slide down over another scene, how do I do this without removing the original scene. I want to be able to transition the settings scene overtop of the game and when they click "Back" have the settings scene scroll back up and disappear.
Not sure why everyone is talking about view controllers here, but the easiest way to achieve what you want to do does not involve multiple scenes.
In SpriteKit, you can only have 1 scene at a time going.
So what you do, is you have an SKNode that will be used for your settings scene, and an SKNode for your main content.
Place all of your settings nodes into this setting SKNode as you would for the scene.
Place all of your main content into the main SKNode.
Add the main node to your scene for normal game functionality.
Give a high z position to the settings node so that it is on top.
Then when you need to present the settings node, you set its position to the screen height + 1/2 the SKNodes height (unless you change the anchor point).
Pause the main node if needed.
Use an SKAction on the settings node to have it move down to the spot on the screen you need it to go. (SKAction.moveToX(x:y:) should work for you).
Then when you need to leave, just call the reverse of the SKAction and unpause the main node.

Presenting previous scene makes it partillay unresponsive

A SpriteKit game presents MainMenuScene then LevelSelectionScene and finally the GamePlayScene. Once the game is over, in GamePlayScene, the user is transferred back to MainMenuScene like so:
MainMenuScene *newScene = [MainMenuScene sceneWithSize:self.view.bounds.size];
[self.view presentScene:newScene transition:[SKTransition fadeWithDuration:0.5]];
It takes me to the scene but the buttons, that worked previously, don't work anymore. I press them, they seem to do their little animation as if they're being pressed but they don't present me appropriate scenes they're supposed to.
I have a very specific bundle of code activated every time a scene is about to be left and I don't actually know if it influences this whole conundrum:
-(void)willMoveFromView:(SKView *)view
{
/* Remove anything stuck in memory. */
[self removeAllActions];
[self removeAllChildren];
[self removeFromParent];
[self.scene removeAllActions];
[self.scene removeAllChildren];
[self.scene removeFromParent];
//[self.scene.view removeFromSuperview]; <--TOTALLY DESTROYS ALLS SCENES IN THE GAME. Don't use it.
}
Has any had previously working scenes presented as partially working duds?
From what you said looks like the button is triggering the animation but not the action, still not much information to be sure how to exactly fix it, probably the creation of the actions of the button is working. Double check if the part of the code that creates the SKActions is actually running and active.
So I poked around and found out the culprit is in the class I used to create buttons. It's called AGSpriteButton and I highly recommend it (You can find it on github). It has built-in SELECTORS, ACTIONS and BLOCKS functionality offered by default that is very easy to tap into. I'm not sure as to why, but SELECTORS stopped functioning correctly. I just switched to ACTIONS calling my methods & everything works perfectly.

CCSpriteBatchNode not being retained?

I have an app that uses sprite sheets and is a Cocos2D/UIKit app. In the init method of my scene I do this:
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"TheSpriteSheet.pvr.ccz"];
[self addChild:spriteSheet z:1];
Now when the game is over, I do this:
[[[CCDirector sharedDirector] actionManager] removeAllActions];
[self unscheduleAllSelectors];
This causes the CCSprites that are currently still as a child to remain there so once I finish my UIKit view transition to my other UIViewController I try to call a method back in my CCLayer class (the class the scene is in) to remove all the children that were in my CCSpriteBatchNode, the app crashes with EXC_BAD_ACCESS.
Now I HAVE to call the method at this point because I want to achieve the effect of the enemies still being on screen while I am doing my custom view transition so they don't magically disappear before I finish switching views. Also I do not do replaceScene anywhere or release my batch node explicitly so I don't know why this is happening.
Anyway how would I fix this issue?
Thanks!

Resources