show menu when paused a game ins spritekit - swift

I created a game like as tower defence. I can successfully "pause" all game. And then i can resume the game with these code:
let startLocation = CGPoint(x: location.x, y: location.y);
let myNodeName = self.nodeAtPoint(location)
if(myNodeName.Name == "Start"){
self.view!.paused = false
}
if(myNodeName.Name == "pause"){
self.view!.paused = true
self.showPausedMenu()
}
In showPausedMenu function. I design a menu with SKLabelNodes and SKSpriteNodes. However when i paused the game all of these "node creation" things also paused. So nothing happen. How i can show my menu while game is paused?
thank you.

Problem is that you're pausing the view itself. Pausing the view stops all of the logic.
If you need to do a pause menu, you need to either add a subview that will appear on top of your view to manage all of the interactions with the user, or you need to not pause the view, but put the view ELEMENTS on pause and create things on top of it.

The best method would be to make use of a SKNode group which consist of all the objects that you want to be paused.
In this way, you retain full control of the elements that you want to pause or unpause

I had solve my problem. While paused the game, all screen freezed however gamescene still detect all "touch" and if it has node name then we can do some operations. At this point, the proble is only showing my menu. So here is my solution.before I paused game, i added my menu on the screen. then pause the game.
In live version is
let startLocation = CGPoint(x: location.x, y: location.y);
let myNodeName = self.nodeAtPoint(location)
if(myNodeName.Name == "Start"){
self.view!.paused = false
self.removePausedMenu()
}
if(myNodeName.Name == "pause"){
self.showPausedMenu()
}
showPausedMenu is:
func showPausedMenu(){
.....
.....
/* Create menu and buttons and labels with NODES. and add to parent as child nodes. */
self.view!.paused = true
}

Related

How to update the pointOfView in ARKit

I'm trying to build my first ARKit app. The purpose of the app is to shoot little blocks in the direction that the camera is facing. Right now, here is the code I have.
sceneView.scene.physicsWorld.gravity = SCNVector3(x: 0, y: 0, z: -9.8)
#IBAction func tapScreen() {
if let camera = self.sceneView.pointOfView {
let sphere = NodeGenerator.generateCubeInFrontOf(node: camera, physics: true)
self.sceneView.scene.rootNode.addChildNode(sphere)
var isSphereAdded = true
print("Added box to scene")
}
}
The gravity works fine, whenever I tap on the screen the block shoots out each and every time I tap. However, they all shoot to the same point, no matter which direction the camera is facing. I'm trying to understand how pointOfView works, would I need to re-render the whole scene? Something else that I can't quite think of? Thanks for any help!
Change this line from
self.sceneView.scene.rootNode.addChildNode(sphere)
to
self.sceneView.pointOfView?.addChildNode(sphere)

Swift SKSpriteNode will disappear but is still clickable

I have a problem with SpriteKit and Swift.
I'm adding SKSpriteNodes at different points of the code to the scene - some of them are clickable, some are not. I use the clickable Nodes as a Menu for the player. So, for example - if he clicks the InventoryButtonNode he jumps into the Inventory. In the Inventory he can touch the PlayButton and jumps back into the game. So, first i add the Nodes:
override func didMoveToView(view: SKView) {
PlayButton = SKSpriteNode(imageNamed: "PlayButton")
PlayButton.size = CGSize(width: 100, height: 100)
PlayButton.position = CGPoint ... // not important
PlayButton.zPosition = 200
PlayButton.name = "PlayButton"
self.addChild(PlayButton)
InventoryButton = SKSpriteNode(imageNamed: "InventoryButton")
InventoryButton.size = CGSize(width: 100, height: 100)
InventoryButton.position = CGPoint ... // different position than PlayButton
InventoryButton.zPosition = 200
InventoryButton.name = "PlayButton"
self.addChild(InventoryButton)
In the override func touchesBegan I use these "menu"-Nodes, here for example the InventoryButton-Node.
if InventoryButton.containsPoint(touch.locationInNode(self)) {
print("Show Inventory")
ShowInventory()
}
Now in the ShowInventory() function i want to remove those "menu"-Buttons from view so that i can add other nodes to show the Inventory of the Player.
func ShowInventory(){
PlayButton.removeFromParent()
InventoryButton.removeFromParent()
}
If I build and run this, the Nodes will be removed - or lets better say - they will get invisible.
Because, if i touch now at the position of InventoryButton I still get the print "Show Inventory" - so the function still reacts to my touch even if the node is not visible.
My problem is, that i have like 4 different functions like ShowInventory() .. I have ShowGame() and so on .. and i want in these functions to completely remove the Nodes and the "Touch"-Ability ..
I need a function which can remove the Nodes completely ..
I have even tried:
func ShowInventory(){
self.removeAllChildren()
}
I get a grey Background without any nodes .. but still - if I touch where the Inventory Buttons position was i get the function called and the print "Show Inventory" ... it's frustrating.
It is because your check for which button is pressed does not take into account whether the button is visible.
Even if a node has been removed from its parent, it still has a position and size. Those two properties are used by containsPoint: to determine if a point is in a node
The easiest way to fix it would be to just check if the button has a parent node before checking to see if the button contains the point.
if InventoryButton.parrent != nil && InventoryButton.containsPoint(touch.locationInNode(self)) {
print("Show Inventory")
ShowInventory()
}

Stopping an running SKAction - Sprite Kit

The following code will animate a rotation.
let something:SKSpriteNode = SKSpriteNode()
func start(){
let rotateAction = SKAction.rotateToAngle(CGFloat(M_PI), duration: 10.0)
something.runAction(SKAction.sequence([rotateAction]))
}
Now I want to stop the animation within the animating duration. Is there anything similar to the following? I want to stop the animation when the user touches the screen.
func stop(){
something.SKAction.stop()
}
You just have to use either:
something.paused = false // or true to pause actions on the node
something.removeAllActions() to definitely remove actions associated to the node
name your action when launching something.runAction(action,withKey:"action1") and then something.removeActionForKey("action1") to remove a given action
You may also change the speed if needed.
Firstly, run the action with a key so you can identify it later:
something.runAction(rotateAction, withKey: "rotate action")
Then you can stop it later by calling
something.removeActionForKey("rotate action")
Alternatively, you can remove all actions also
something.removeAllActions()

Keeping the game paused after app become active?

It is my first post on this forum and I apologize in advance if I am doing something not in the right way ! :)
I am making an iphone game with Swift & SpriteKit and I am currently facing a problem. When my app is going to background it calls a function pause (cf. below) but it automatically unpause when the game resumes.
I have seen this very interesting post : Spritekit - Keep the game paused when didBecomeActive (and How to keep SpriteKit scene paused when app becomes active?) but I am stuck.
I don't know how to implement the new SKView class as my View is configured as shown in the below code...
This is how my application works :
class GameViewController: UIViewController {
var scene: GameScene!
override func viewDidLoad() {
super.viewDidLoad()
// Configure the View
let SkView = view as! SKView
SkView.multipleTouchEnabled = true
// Create and configure the scene
scene = GameScene(size: SkView.bounds.size)
scene.scaleMode = .AspectFill
// Present the scene
SkView.presentScene(scene)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("PauseWhenBackGround:"), name:"PauseWhenBackGround", object: nil)
}
func PauseWhenBackGround(notification : NSNotification) {
if scene.Pausing == false{
scene.Pause()
}
}
I've tried the following :
I added a new class which is :
class GameSceneView : SKView {
func CBApplicationDidBecomeActive() {
}
}
Then, I tried to set my view as let SkView = view as! GameSceneView but I got an error saying that I cannot cast the view to MyProjectName.GameSceneView()...
I also tried the following : let SkView! = GameSceneView() as GameSceneView! but I end up with a gray background scene...
Does anyone knows how I can implement the new SKView class to prevent the CBApplicationDidBecomeActive() bug from happening so that the game does not unpause when becoming active ?
Thank you very much in advance ! :)
I think a better way is instead of pausing the whole scene you could create a worldNode in your GameScene and add all the sprites that need to be paused to that worldNode. Its better because if you pause the scene you cannot add pause menu nodes or use touches began etc. It basically gives you more flexibility pausing a node rather than the whole scene.
First create the world node (make global property if needed)
let worldNode = SKNode()
addChild(worldNode)
Than add all the sprites you need paused to the worldNode
worldNode.addChild(sprite1)
worldNode.addChild(sprite2)
Create an enum for your different game states
enum GameState {
case Playing
case Paused
case GameOver
static var current = GameState.Playing
}
Than make a pause func in your game scene
func pause() {
GameState.current = .Paused
//self.physicsWorld.speed = 0 // in update
//worldNode.paused = true // in update
// show pause menu etc
}
And call it like you did above using NSNotification or even better using delegation.
I prefer this method way more than pausing the scene from the gameViewController and also pausing the whole scene.
Create a resume method
func resume() {
GameState.current = .Playing
self.physicsWorld.speed = 1
worldNode.paused = false
// remove pause menu etc
}
and finally add this to your update method
override func update(currentTime: CFTimeInterval) {
if GameState.current == .Paused {
self.physicsWorld.speed = 0
worldNode.paused = true
}
Spritekit sometimes tends to resume the game when the app becomes active again or when an alert such as for in app purchases is dismissed. To avoid this I always put the code to actually pause the game in the update method.
Hope this helps.

Swift, spritekit: How to restart GameScene after game over? Stop lagging?

Alright, so I have a sprite kit game in Swift here and I'm having trouble restarting my GameScene after it's game over.
Right now, when the user loses all their lives, a variable gameIsOver is set to true, which pauses specific nodes within the scene as well as sets off a timer. After this timer ends, I move to my Game Over scene. From the Game Over scene, the user can either go back to home or restart the game.
Here is how I transition to my game over scene:
countdown(circle, steps: 120, duration: 5) {
//Performed when timer ends
self.gameSoundTrack.stop()
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewControllerWithIdentifier("GOViewController")
self.viewController!.presentViewController(vc, animated: true, completion: nil)
//Resetting GameScene
self.removeAllChildren()
self.removeAllActions()
self.scene?.removeFromParent()
self.paused = true
gameIsOver = false
}
I pause my scene here is because, if I don't, when GameScene is reloaded gameIsOver is still set to true and my app crashes. I don't know why this occurs considering I set gameIsOver to false here.
After moving from GameScene to my game over scene and back to GameScene, or from GameScene to my home view controller and back to GameScene a couple times, my fps count has decreased so much that the whole game is lagging to the point where gameplay is impossible.
This leads me to believe that I am not removing/disposing of GameScene properly each time I present my game over scene.
I believe I'm having the same problem as here: In Swift on "game over" move from scene to another UIView and dispose the scene? , but I'm new to this and I can't figure out how they solved their problem.
How I can I completely reset/delete GameScene each time I present my Game Over scene in order to stop the lagging?
Your gameIsOver Bool will not keep a value while switching between scenes unless your make it a struct. So instead of
var gameIsOver = false
it should be
struct game {
static var IsOver : Bool = false
}
so when you change the value as things happen you call
game.IsOver = true
//if your calling it in a different View controller or scene than where you created it just put the name before it like so
GameViewController.game.IsOver = true
As for the transition back to your GameScene create a function
func goToGameScene(){
let gameScene:GameScene = GameScene(size: self.view!.bounds.size) // create your new scene
let transition = SKTransition.fadeWithDuration(1.0) // create type of transition (you can check in documentation for more transtions)
gameScene.scaleMode = SKSceneScaleMode.Fill
self.view!.presentScene(gameScene, transition: transition)
}
then whenever you want to reset to GameScene just call
goToGameScene()