View distorted after connecting two scenes - swift

I have the following problem:
In my game, I have two scenes (GameScene.swift and GameOverScene.swift). I think they are pretty self-explanatory, in GameScene.swift happens everything connected to the game and if the player collides with the enemy, he will be sent to GameOverScene.swift. This is the code:
var transition:SKTransition = SKTransition.flipHorizontalWithDuration(0.5)
var gameOverScene:SKScene = GameOverScene(size: self.size, dead: true)
self.view?.presentScene(gameOverScene, transition: transition)
As soon as the player touches the screen, he should be redirected to the GameScene:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
var transition:SKTransition = SKTransition.flipHorizontalWithDuration(0.5)
var scene:SKScene = GameScene(size: self.size)
self.view?.presentScene(scene, transition: transition)
}
}
Unfortunately, everything in the GameOverScene.swift is completely distorted and the background color changes (?!). When going back to GameScene.swift, it's distorted as well. This is how GameOverScene.swift is initialized:
class GameOverScene: SKScene {
init(size:CGSize, dead:Bool) {
super.init(size: size)
Any idea why this doesn't work properly? Thanks in advance!

Related

SKEmitterNode disappears when added to a new SKScene

I have a SKEmitterNode that's running on a SKScene and I want to move it to the next SKScene without interrupting the particles.
Normally I'd do it like the following:
let scene: SKScene = GameScene(size: self.size)
let transition = SKTransition.crossFade(withDuration: 0.5)
self.view!.presentScene(scene, transition: transition)
And then on the next SKScene:
override func didMove(to view: SKView) {
particleEmitter.removeFromParent()
addChild(particleEmitter)
}
This works perfectly fine however in this situation I don't want to use a transition when moving to the next SKScene. I've tried it without a transition like:
let scene: SKScene = GameScene(size: self.size)
self.view!.presentScene(scene)
And the SKEmitterNode disappears as soon as the new SKScene is presented even though I've removed it from the last SKScene and added it as child to the new one.
My question is why is the SKEmitterNode dissapearing and how can I get it to work without using a transition between the SKScene's. Any help would be much appreciated, thanks.
Note: Using a SKTransition with a duration of 0 also works but this causes a noticeable 'flash' during the transition.
There shouldn't be any flashes and such. If I understand you correctly, what you need to do is to create an emitter in a global scope. Before presenting the next scene, you should remove the emitter from its parent. When you are in the next scene, you add emitter to it. I just tried and it works for me without any lag, flashes or something.
Here is the code...There are two scenes...A GameScene:
import SpriteKit
let emitter = SKEmitterNode(fileNamed: "Fireflies")
class GameScene: SKScene {
override func didMove(to view: SKView) {
backgroundColor = .black
if let emitterNode = emitter {
addChild(emitterNode)
}
print("Game Scene")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let scene = WelcomeScene(fileNamed: "WelcomeScene") {
scene.scaleMode = .aspectFill
emitter?.removeFromParent()
self.view?.presentScene(scene)
}
}
}
and a WelcomeScene:
import SpriteKit
class WelcomeScene:SKScene{
override func didMove(to view: SKView) {
backgroundColor = .black
print("Welcome Scene")
if let emitterNode = emitter {
addChild(emitterNode)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let scene = GameScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
emitter?.removeFromParent()
self.view?.presentScene(scene)
}
}
}
For the emitter I've used standard fireflies template.

Scene doesn't fully show after changing from another scene

In my MenuScene I have a play Button and if I touch it, it changes to the GameScene using this code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if atPoint(location).name == "playButton" {
let playScene = GameScene(size: self.size)
let playSceneTransition = SKTransition.fade(withDuration: 0.5)
playScene.scaleMode = SKSceneScaleMode.aspectFill
self.view?.presentScene(playScene, transition: playSceneTransition)
}
}
But the GameScene looks like it's in the bottom left corner and only shows the top right corner of the Scene. If I change the GameViewCotroller to show the GameScene instead of the MenuScene when I start the game it's looks perfectly fine. why is that? is there something wrong with the code to go to the GameScene?
The problem here is you are loading your MenuScene via an SKS file. This by default puts your anchor point at 0.5,0.5.
You are then transitioning to game scene using a size initializer, not an SKS file, with GameScene(size: self.size), which is going to have an anchor point of 0,0 or 0.5,0.5 depending on what version of iOS your users are loading up.
I would recommend avoiding using GameScene(size: self.size) and use GameScene(fileNamed:"GameScene") to keep yourself consistent.

Sprite won't move with finger/ SpriteKit / Swift 3

*I searched most places and could not find a "basic solution" to my question
Hi there, I am trying to create a simple SKSpriteNode that will move along with my finger anywhere on the screen (x & y coords). So far what I have done is the code below, and I can't seem to figure out much else because my Sprite won't even move when the game is ran. I am also getting an error at:
simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode
This line of code causes my program to not run since I've added it. Please and thanks for your help.
In this case Sprite "simpleS" is the Sprite I am trying to move when touches begin.
CODE STARTS HERE:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var simpleS = SKSpriteNode()
override func didMove(to view: SKView) {
simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode
simpleS.physicsBody?.affectedByGravity = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
simpleS.run(SKAction.moveTo(x: location.x, duration: 0.0))
simpleS.run(SKAction.moveTo(y: location.y, duration: 0.0))
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
simpleS.run(SKAction.moveTo(x: location.x, duration: 0.0))
simpleS.run(SKAction.moveTo(y: location.y, duration: 0.0))
}
}
}
this code
simpleS = self.childNode(withName: "simpleS") as! SKSpriteNode
connects the sprites in your GameScene.sks with GameScene.swift
so in the first step you have to make sure that you set the right name (exactly "simpleS") in your sks file.
if you did it right, so please send the error for us.
i have built and ran your code and everything works in a right way.
another point is that if you want your sprites only moves by your finger movement you have to delete the codes in touchesBegan function, because touchesBegan moves your sprite wherever you touch.

Change SKScene using presentScene()

In my SpriteKit Game i'm using:
self.scene!.removeFromParent()
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true
var scene: PlayScene!
scene = PlayScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene, transition: SKTransition.fadeWithColor(SKColor(red: 25.0/255.0, green: 55.0/255.0, blue: 12.0/255.0, alpha: 1), duration: 1.0))
to move from one scene to another. But how can I go back to the original scene? Using the same principle of code always led to a major crash..
I made an example where global structure is used to track the info about previousScene. It can be done with a custom property as well, or by using userData which every node has. The logic is the same. Also, I've removed debugging code (debug label code etc.) because it is not important for everything to work.
Example might be better if I added a few buttons where each links to the certain scene, but I left just one button to keep everything short as possible.
What you need to know about this example (you will change this rules according to your game, but the logic is the same - set the previousScene before an actual transition):
there are three scenes, WelcomeScene (default one), MenuScene and a GameScene.
tapping on the black button takes you to the GameScene. There is an exception to this rule when current scene is a GameScene. In that case, transition will take you to the previousScene.
tapping anywhere around the black button will take you to the previous scene. There is an exception to this rule when WelcomeScene is loaded for the first time (previousScene is not set) and a transition will take you to the MenuScene in that case.
-in your GameViewController you should set up a WelcomeScene to be a default one. Otherwise, you should change a code a bit to handle situations what happening when previousScene is not set (like I did in touchesBegan of WelcomeScene).
So those are rules I've made, just in order to make all those transitions a bit more meaningful...
Here is the code (BaseScene.swift):
import SpriteKit
enum SceneType: Int {
case WelcomeScene = 0
case MenuScene //1
case GameScene //2
}
struct GlobalData
{
static var previousScene:SceneType?
//Other global data...
}
class BaseScene:SKScene {
let button = SKSpriteNode(color: SKColor.blackColor(), size: CGSize(width: 50, height: 50))
override func didMoveToView(view: SKView) {
setupButton()
}
private func setupButton(){
if (button.parent == nil){
//Just setup button properties like position, zPosition and name
button.name = "goToGameScene"
button.zPosition = 1
button.position = CGPoint(x: CGRectGetMidX(frame), y: 100)
addChild(button)
}
}
func goToScene(newScene: SceneType){
var sceneToLoad:SKScene?
switch newScene {
case SceneType.GameScene:
sceneToLoad = GameScene(fileNamed: "GameScene")
case SceneType.MenuScene:
sceneToLoad = MenuScene(fileNamed: "MenuScene")
case SceneType.WelcomeScene:
sceneToLoad = WelcomeScene(fileNamed:"WelcomeScene")
}
if let scene = sceneToLoad {
scene.size = size
scene.scaleMode = scaleMode
let transition = SKTransition.fadeWithDuration(3)
self.view?.presentScene(scene, transition: transition)
}
}
}
Every scene (WelcomeScene, MenuScene, GameScene) inherits from a BaseScene class (which is subclass of a SKScene). I guess, there is no need to explain that, but feel free to ask if something confuses you. The important method here (which is used by every subclass) is goToScene(scene:SceneType) and its parameter (of type SceneType) which tells us what type of scene a method should load.
SceneType is just an enum which holds integers...So actually we are not working with objects here, thus there is no fear of strong reference cycles.
Next, there are other scenes (WelcomeScene.swift):
import SpriteKit
class WelcomeScene:BaseScene {
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.backgroundColor = SKColor.darkGrayColor()
}
deinit {print ("WelcomeScene deinited")}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
if let location = touch?.locationInNode(self){
//Give a priority to a button - if button is tapped go to GameScene
let node = nodeAtPoint(location)
if node.name == "goToGameScene"{
GlobalData.previousScene = SceneType.MenuScene
goToScene(SceneType.GameScene)
}else{
//Otherwise, do a transition to the previous scene
//Get the previous scene
if let previousScene = GlobalData.previousScene {
GlobalData.previousScene = SceneType.WelcomeScene
goToScene(previousScene)
}else{
// There is no previousScene set yet? Go to MenuScene then...
GlobalData.previousScene = SceneType.WelcomeScene
goToScene(SceneType.MenuScene)
}
}
}
}
}
To keep short as possible, everything is commented. Next code (MenuScene.swift):
import SpriteKit
class MenuScene: BaseScene {
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
backgroundColor = SKColor.purpleColor()
}
deinit {
print ("MenuScene deinited") //If this method isn't called, you might have problems with strong reference cycles.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
if let location = touch?.locationInNode(self){
//Give a priority to a button - if button is tapped go to GameScene
let node = nodeAtPoint(location)
if node.name == "goToGameScene"{
GlobalData.previousScene = SceneType.MenuScene
goToScene(SceneType.GameScene)
}else{
//Otherwise, do a transition to the previous scene
//Get the previous scene
if let previousScene = GlobalData.previousScene {
GlobalData.previousScene = SceneType.MenuScene
goToScene(previousScene)
}
}
}
}
}
And for the end (GameScene.swift):
import SpriteKit
class GameScene: BaseScene{
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
self.backgroundColor = SKColor.orangeColor()
}
deinit {print ("GameScene deinited")}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Here, we ignore black button because we don't want to transition to the same scene
if let previousScene = GlobalData.previousScene {
GlobalData.previousScene = SceneType.GameScene
goToScene(previousScene)
}
}
}
Preview:
Just read again the rules from the beginning and you will be fine (eg. in GameScene black button doesn't work, or on first launch previousScene is not set , so you will be transitioned to the MenuScene by default).
That would be it. Hope this helps a bit. You can copy and paste the code to test it and improve it to your needs. Still, not sure that you really need this. It looks that you just need to correctly transition between scenes.
HINT: What is important here is that every scene BaseScene, WelcomeScene... has it own .sks file. You create those from File->New->File->Resource and name it appropriately (like BaseClass.sks, WelcomeScene.sks...) Also, it is your job to maintain the state of GlobalData.previousScene variable (eg. set it before the transition is made).
You would need to create a property in your new scene that stores the previous one, something like previousScene. Then you can set it like this: scene.previousScene = self.scene. In you new scene, you can now go back to the previous scene with skView.presentScene(previousScene)
And I'd advise against naming the new scene you are going to present scene because your current scene is also named scene, so if you accidentally forget the self in self.scene then that may cause a lot of confusion. I'd name it something like newScene or sceneToPresent.
Also, your first line, self.scene!.removeFromParent(), isn't necessary. You don't need to remove the current scene before presenting a new one.

How to transition scenes in Swift

In Objective-C, using Sprite-Kit, I would successfully use something like the following code in Objective-C to bring up a new scene
if ([touchedNode.name isEqual: #"Game Button"]) {
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
GameScene *newGameScene = [[GameScene alloc] initWithSize: self.size];
// Optionally, insert code to configure the new scene.
newGameScene.scaleMode = SKSceneScaleModeAspectFill;
[self.scene.view presentScene: newGameScene transition: reveal];
}
In trying to port my simple game to Swift, so far I have this working...
for touch: AnyObject in touches {
let touchedNode = nodeAtPoint(touch.locationInNode(self))
println("Node touched: " + touchedNode.name);
let touchedNodeName:String = touchedNode.name
switch touchedNodeName {
case "Game Button":
println("Put code here to launch the game scene")
default:
println("No routine established for this")
}
But I do not know what code to write to actually transition to another scene.
Question(s):
Can someone please provide an example of using SKTransition with Swift?
Would you normally create another "file" to put the other scene code in for the other scene, assuming you would have under Objective-C, or is there something about using Swift that means I should approach it differently?
Thank you
To start with your second question, it's kind of up to you. If you wish to, you can continue to follow the Objective-C convention of having one class per file, although this isn't a requirement, and wasn't in Objective-C either. That being said, if you have a couple of classes that are tightly related, but aren't made up of much code, it wouldn't be unreasonable to have them grouped in a single file. Just do what feels right, and don't make a huge blob of code in a single file.
Then for your first questions... Yes, you had a good deal of it already. Basically, from where you got up to, you need to create the instance of GameScene through its size: initializer. From there, you just set the properties and call present.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
guard let location = touches.first?.locationInNode(self),
let scene = scene,
nodeAtPoint(location) == "Game Button" else {
return
}
let transition = SKTransition.reveal(
with: .down,
duration: 1.0
)
let nextScene = GameScene(size: scene.size)
nextScene.scaleMode = .aspectFill
scene.view?.presentScene(nextScene, transition: transition)
}
if you have to work on touch-begain or node action , Then use it:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let touch = touches.anyObject() as UITouch
if CGRectContainsPoint(btncloseJump.frame, touch.locationInNode(self)) {
self.scene.removeFromParent()
btncloseJump.removeFromParent()
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
var scene: HomeScene!
scene = HomeScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene, transition: SKTransition.fadeWithColor(SKColor(red: 25.0/255.0, green: 55.0/255.0, blue: 12.0/255.0, alpha: 1), duration: 1.0))
}
}