Pause scene before transition and perform transition - ios

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!

Related

SCNAnimationPlayer to start playing animation on gesture

I am using Scenekit/ ARKit for the code and Blender for my 3D models. I have a 3D model with some keyframe animations - created in Blender, which has been exported easily into XCode. Nothing complicated - it's just a cat moving a bit up and then down.
I am using animation controls inside my tapGestureRecognizer. As soon as my view loads I have set the animation to be in paused state :-
nonGeometryObjectNode?.childNode(withName: "Armature", recursively: true)?.animationPlayer(forKey: "cat_animated-1")?.paused = true
Code for animation controls inside my tap gesture is as below.
let animationPlayer = hitResults.first?.node.parent?.childNode(withName: "Armature", recursively: true)?.animationPlayer(forKey: "cat_animated-1")
animationPlayer?.animation.autoreverses = true
animationPlayer?.animation.repeatCount = 1
animationPlayer?.animation.duration = 0.8
animationPlayer?.animation.blendInDuration = 0.2
animationPlayer?.animation.blendOutDuration = 0.2
animationPlayer?.paused = true
if (animationPlayer?.paused)! {
animationPlayer?.play()
}
It works fine, the only problem is it works just one time when tap is performed for the very first time. I tried using .paused, .stop() inside my tapGesture code, but animationPlayer doesn't replay as it should every time I am tapping on it. There is no bool var in SCNAnimationPlayer or else which I can use to detect if the animation has played itself out, so that I can use .stop() and then .play() again.
I did see two instance vars animationDidStart and animationDidStop which I thought probably would be useful to manage what I need. But I am at loss of ways to use these. What would be really helpful is what should be used to play and stop/ pause my animation whenever I tap on the object. Any pointer would be helpful.
I know it's an old question but in case anyone stumbles across it in future, the default action is for the animation to be removed once complete. Therefore to be able to play it again you need to set:
animationPlayer?.animation.isRemovedOnCompletion = false

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.

SpriteNode/Shapes change size and disappear after returning to GameScene (Swift SpriteKit)

GIF of the issue in action:
http://i.imgur.com/hiF5lyw.gifv
As you can see, the main Player is a SKSpriteNode, and when you hit a falling block which is a SKShapeNode, the game switches to the GameOver scene. When you click restart in the GameOver scene and move back to the GameScene the falling blocks disappear and the dimensions of the Player sprite is changed along with its y position.
All the code contains pretty detailed comments but if you have any questions about it feel free to ask.
Since the size of my Player sprite is based on the size of the screen, It might have something to do with screen size changes when switching scenes maybe. Also, its strange the falling block enemies dont show up for a few seconds, but then the node count drastically increases and another GameOver scene transition is triggered.
Maybe I have to change something with the GameViewController?
Any help would be greatly appreciated, I've been working at it for hours and I cant seem to figure out why its messing up.
Figured it out.
I created two new variables:
var transition:SKTransition = SKTransition.fadeWithDuration(0.5)
and
var gameOver:SKScene = GameOver(size: self.size)
And then implemented both when calling the scene switch with:
self.view?.presentScene(gameOver, transition: transition)
I think the size: self.size is what fixed it.
If you have troubles with changing size of your nodes when you do transitions, instead of size: self.size, you should use fileNamed: nameOfYourScene. This worked for me.

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

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.

Options to avoid the GameOver function in a game

I'm working on a game using xcode and I want to create a super item (like the star in Super Mario) If you touch this super item, you can pass all the obstacles without lose (avoid the game over). But really I don't have an good idea to do this.
I have my current gameover function in my game, If I touch the obstacle area, I die. Obstacles are UIView Objects. If the player touch any obstacle:
[self gameover];
I can try two things: First, I can make the image invisible, But If I use:
player.hidden = YES;
when the player touch some obstacle, the player dies (game over). Obviously is hidden but the object is still there. I need an option to set player invisible to all the obstacles in the game.
Second: I can avoid the gameover if the player is using an special item.
This is what I'm try to do:
if (CGRectIntersectsRect(player.frame, superitem.frame)) {
[avoid gameover];
I really don't know the correct function to do this. Hope to receive some help. Thanks
I finally found the correct code to do what I need. I want to share it, maybe someone need to do this and this code can help.
UIImageView* tempf = [coins];
if (tempf.tag == obstacles)
{
[coins removeObject:tempf];
[tempf removeFromSuperview];
}
Thanks everyone's for your help.

Resources