Unable to make a button that will transition you to main menu with Spritekit Swift - swift

I'm new to Swift and Spritekit and having trouble with implementing a button or imagebutton that will take you to the main menu from the GameScene. All I want to do is to make a button that will display a dialog that will tell you if you want to return to the main menu.
For example is there a way to make a dialog that will transition you to other scenes with the code below?
let skview = self.view as! GameSKView!
skview.score = score
let scene = MainMenuScene(size: self.size)
let tr = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1)
scene.scaleMode = SKSceneScaleMode.AspectFill
skview.presentScene(scene,transition: tr)

try this
func changeScene() {
let newScene = MainMenuScene(size: self.size)
let theTransition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1)
newScene.scaleMode = .AspectFill
scene?.view?.presentScene(newScene, transition: theTransition)
}

Currently developing a button that works like a uibutton using spritekit. Here is what I have, it may be slightly overkill for what you want but here it is.https://github.com/marcJV/SKAToolKit/tree/swift?files=1
It will be pulled over to SKAToolKit when as we are porting it to swift. There is also an example project showing off how to use it. Also there is a lack of a proper readme at this point, but the example should have everything you need. This is using swift 2.0

Related

Mac Catalyst - Make window NSToolbar transparent

Problem
Hi All,
I need help to make my window NSToolbar transparent, however, I still need to be able to use a toolbar within the window scene titlebar. Currently, you can set a titlebar to be transparent by either hiding your toolbar or not having one at all, but even though I will not be adding any actual tools within the toolbar, there needs to be one so that I can reposition the window traffic light buttons using the UITitlebarToolbarStyle property - I know that from Electron 8, developers are able to reposition the traffic light buttons (is this also possible in Swift?)
Any help is much appreciated 😊
Current Code
- this is situated within the AppDelegate
guard let windowScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
windowScene.sizeRestrictions?.minimumSize = CGSize(width: 1366 * 0.9, height: 1024 * 0.9)
if let titlebar = windowScene.titlebar {
let customToolbar = NSToolbar()
titlebar.titleVisibility = .hidden
titlebar.toolbar = customToolbar
titlebar.toolbarStyle = .unified
titlebar.autoHidesToolbarInFullScreen = true
}
#endif
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.makeKeyAndVisible()

How to add an overlay start scene in swift?

can someone code for me please how to add an overlay start scene in swift?
I want to add a startMenu like in Flappy Bird, and then when you click play it just moves/fades away and the game begins.
Can someone help me achieve this effect?
Looking to achieve this.
I will be more than thankful!
Try to post some code of what you have tried so far when asking a question on stack overflow, people tend to not write code for you.
You can basically do this 2 ways.
1)
Either create a StartScene and transition to GameScene with your preferred SKTransition when the play button is pressed e.g SKTransition.crossFade
2)
If you want to do it in the same scene you can just create a SKNode/SKSpriteNode class to use as a menu.
class Menu: SKSpriteNode {
lazy var playLabel: SKLabelNode = {
let label = SKLabelNode(fontNamed: "HelveticaNeue")
label.text = "Play"
label.fontSize = 22
label.fontColor = .yellow
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
return label
}()
/// Init with size
init(size: CGSize) {
super.init(texture: nil, color: .red, size: size)
addChild(playLabel)
}
}
}
Than in your GameScene Scene you can add the menu like so with your preferred size, in this example the size of the scene.
class GameScene: SKScene {
lazy var menu: Menu = Menu(size: self.size)
override func didMove(to view: SKView) {
addChild(menu)
}
}
You can add other nodes for UI, including sprites for buttons etc, to that menu class.
Than in GameScene in touches Began you can look for nodes that are touched, e.g the play button and than do your thing e.g to animate out the menu
let action1 = SKAction.fadeAlpha(to: 0, duration: 0.5)
let action2 = SKAction.removeFromParent()
let sequence = SKAction.sequence([action1, action2])
menu.run(sequence)
If you stuck there and want more coding samples you should google how to do this, is basic SpriteKit stuff that you need to know and there is plenty tutorials available. This includes samples of how to make button subclasses using SKSpriteNodes.
Hope this helps

Swift Find Scene By Name

How do i make a Swift sprite kit game jump to another scene by searching for the scene with a string. so:
let scenename : String = "Map1"
let scene : SKSCene = SkScene.getscene(scenename)
Do we have something like that?
if not please help me with alternatives!
Thanks for your help and time!
Make a delegate call to your SKView from current scene, which is presenting the scene then present another scene like this:
let newScene = SKScene.init(fileNamed: "MyScene")
newScene!.scaleMode = SKSceneScaleMode.Fil
let reaveal = SKTransition.crossFadeWithDuration(0.01)
let aSKView = self.view as! SKView
aSKView.presentScene(newScene!, transition: reaveal)
Do check for memory leaks ;)

remove UITextView from SpriteKit app with animation

I have a UITextView object in my Swift/SpriteKit program but while the transition from SpriteKit scene to SpriteKit scene works well, I can't get the UITextView to animate in a similar way.
For the scene transition, I use:
let transition = SKTransition.flipVerticalWithDuration(1)
let newScene = GameScene(size: self.frame.size)
view?.presentScene(newScene, transition: transition)
And to remove the UITextView, I use
myUITextView.removeFromSuperview()
Any ideas how I can get the textView to flip on the vertical axis as well, and at the same time as the scene change?
Update:
I realised this might be too difficult to do, so I changed it to this...
let transition = SKTransition.revealWithDirection(.Right, duration: 1.0)
let newScene = GameScene(size: self.frame.size)
newScene.scaleMode = .AspectFill
let options = UIViewKeyframeAnimationOptions.CalculationModeLinear
UIView.animateKeyframesWithDuration(1.0, delay: 0.0, options: options, animations: {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/2, animations: {
self.myUITextView.transform = CGAffineTransformMakeTranslation(300, 0)
})
}, completion: {finished in
self.myUITextView.removeFromSuperview()
})
view?.presentScene(newScene, transition: transition)
The exact timing between the scene transition and the UITextView animation needs some tuning but this basically works, except for a new problem.
The new scene I'm transitioning to also has a UITextView. This appears on top of the one I'm animating away (like pulling a card out from behind the deck). I understand why this is happening, the presentScene function is calling the new scene, and with it, the declaration of the new UITextView. Because SpriteKit and UIKit don't work well together, this is being placed on top of the older UITextView I'm trying to remove. Any ideas how I can achieve this animation with the old scene's UITextView being kept on top?
Update2: I've done a little experiment with the following commands:
let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft
var views : (frontView: UIView, backView: UIView)
views = (frontView: self.myUITextView, backView: self.UITextView)
UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil)
When I did this, I expected the myUITextView to flip from the left, but to my surprise the entire SKView flipped in the way I was looking for as well. The only problem is the toView is pointing back onto the same scene (so it flips back onto oldScene) and I want to go to newScene. Any ideas how to point towards the new scene or open it behind the old scene?

Swift multiple scenes

I have made a swift SpriteKit game but now I want to add a menu scene(and eventually a lose scene). So what I did was
I created a new scene called Menu scene.
I edited GameViewController and changed it to load menu scene(it worked).
In MenuScene.swift I put some code in so when a sprite was clicked it would run this code:
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: size)
self.view?.presentScene(scene, transition:reveal)
But when I click the button it flips round to the next scene and immediately crashes.
So my question is: Am I doing this the right way and if I am why does it crash?
It turns out I wasn't deleting a particle emitter so I put in a bit of code to delete it just before the transition and it worked.
If you do, it should work:
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameScene(size: self.frame.size)
let skView = view as SKView!
skView.presentScene(scene, transition:reveal)
I hope this works for you.
Since you're in sprite kit , it's wrong to use :
self.view?.presentScene(scene, transition:reveal)
You should do the following :
let scene = GameScene(size: view.bounds.size)
let skView = view as SKView
//skView.showsFPS = true
//skView.showsNodeCount = true
//skView.ignoresSiblingOrder = true
//scene.scaleMode = .ResizeFill
skView.presentScene(scene, transition:reveal)