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 ;)
Related
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
Where is it declared which scene will show up first on load up of the app? The first file I made which is the game obviously shows up first, but if I'm wanting to switch it so that my main menu pops up first, how and where do I do that?
Thanks
In the view controller, the first scene is set. By default, it's automatically set to GameScene but you can change this by altering the code in the GameScene. You should change this line in unarchive from file:
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene to
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! sceneName
and this line in didMoveToView
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
to
if let scene = GameScene.unarchiveFromFile("GameScene") as? sceneName {
This will cause the game to load sceneName, or whatever you scene is called as the initial scene.
I'm trying to do this for a while. I have a main scene for a game named PlayScene. I have a pause button there. When player is tapping that button I want to load another scene, named PauseScene. For visualy creating that scene I use Sprite kit level editor. So I have two files PauseScene.swiftand PauseScene.sks. But .sks content ( just a background for now) isn't unarchiving. I don't really know where could I make a mistake.
So this is my transition via pause button, located in PlayScene
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.pause {
var scene = PauseScene(size: self.size)
scene.paused = true
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}
And this is what I have in PauseScene.swift:
class PauseScene: SKScene {
override func didMoveToView(view: SKView) {
self.initPauseScene()
}
func initPauseScene() {
}
}
I tried this and it's not working very well. However that tutorial is closest I could get with my problem. It's not crushing and isn't showing any errors, but when I tap pause button it's transitioning to that scene without my background, just standard grey scene. This answer is not working for me. First suggestion show errors and when I tried second and tap the pause – game is crushing.
I don't know, that level editor is made for easier work but for know it just doubles it for me. I even thought that problem is in my background image maybe. But its standard .png. Why my content isn't showing up?
So I tried to copy GameViewController default code for loading GameScene and ended up with crashing. It's not the first time I have that error while trying to unarchive this. What does it mean? I got this when i tried to replace var scene = PauseScene(size: self.size) with a if let scene = PauseScene.unarchiveFromFile("PauseScene") as? PauseScene
It may be transition to a grey scene due to spelling errors or the file not even being in the mainBundle. I have tested this and it works. You need to make sure that you are writing the exact name of the .sks file when loading or else you get a grey screen.
Example
If the file is called GameScreen.sks and the class is called GameScene then if you put the class name when loading the .sks file you will get a grey screen.
let doors = SKTransition.doorwayWithDuration(1.0)
let archeryScene = GameScene(fileNamed: "GameScreen")
self.view?.presentScene(archeryScene, transition: doors)
So if we look at the second line of code, We see that it is loading the .sks file with the name GameScreen opposed to the class name GameScene. We also abort using the file extension because the compiler know by default it is a .sks file we are searching for in the mainBundle.
If you do keep getting a grey screen, Then it may be an error in spelling or ing you GameViewController, In the SKNode Extension, Be sure to change
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
to this..
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
We just edited this line of code,
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
to this
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene
This will allow us to load multiple .sks files without any errors or crashes.
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)
I want to replace scene in my project.
I have made project using spritekit and swift in xcode version 6
Now when I try to write replace scene code using this
for touch:AnyObject in touches
{
let location=touch.locationInNode(self)
if self.nodeAtPoint(location)==self.playbutton
{
println("Go to the game!")
var scene=PlayScene(size:self.size)
let skView = self.view as SKView //ERROR
}
}
It gives me ERROR:
Downcast from 'SKView?' to 'SKView' only unwraps optionals; did you mean to use '!'?
You have an "optional" problem over here.
Use this instead:
let skView = self.view? as SKView!
You don't need the downcast part, that's what Xcode is saying, not as .....
Remove as.... and just unwrap with let skView = self.view! or an if/let statement if you're not sure about whether its nil:
if let skView = self.view{
skView.presentScene(...)
}
Watch out for strong reference cycles when you replace the scene. Remember the view and scene are together, so..., you'll figure out.