How to Change view's from a UIVeiwController to a Scene - swift

I Have a UIViewController That I need and I would like it to stay a UIView Controller. but I have a back button that takes you to an Scene that's not working. i just simply need to get to the SKcene any way possible. Thanks
Code:
#IBAction func BackButton(sender: AnyObject) {
var scene = GameScene()
let sKView = self.view! as! SKView
sKView.ignoresSiblingOrder = true
scene.size = sKView.bounds.size
let reveal = SKTransition.fadeWithDuration(0.45)
sKView.presentScene(scene, transition: reveal)
}
}
this function is current giving me this Error below
Could not cast value of type 'UIView' (0x1991fa530) to 'SKView' (0x198f5f560).
My Game Scene is my SkScene and my UIViewController that is attached to the storyboard view is called SoundboardController

Related

SpriteKit screen size problem with swift5

I try to learn SpriteKit with Xcode 12.4 and swift5.
When I made the first project and tried to run the simulator, it showed me a screen like in the below image.
You can see that screen is not full-size. So, I've changed the size of the attribute inspector in GameScene.sks but nothing changed.
GameViewController code is like below, I didn't change anything... How can I make my GameScene fullscreen on a simulator of iPhone 11?
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}
}
}

I am using SpriteKit to make a game, but my view only takes up what appears to be the safe area of the screen. How do I expand this view?

I am making a game using SpriteKit, and I want it to use the whole screen. However, right now, it is only using what looks to be the safe area. How can I make the view take up the entire screen? Below is the GameViewController code I am using:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
let scene = MenuScene(size: UIScreen.main.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .fill
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
}
}
}
If you use SwiftUI, you can present your scene as a view using SpriteView. Then, you can apply the view modifier .ignoresSafeArea() to make it take up the whole screen.
Anyway, I'm a newbie, but that's what I did and it worked for me. There may be a better way, though.
Here's an example of what I'm talking about:
import SwiftUI
import SpriteKit
struct ContentView: View {
var body: some View {
SpriteView(scene: MenuScene(size: UIScreen.main.bounds.size), transition: SKTransition.reveal(with: .down, duration: 1.0))
.ignoresSafeArea()
}
}

Modification of spite node sizes when I change scene

In my app I've 2 scenes.
I use the first scene as menu scene so I create a button for presents the second scene.
But when I present the second scene the sizes of all elements of this scene they become bigger than the sizes that I've set.
The first picture is the second scene which it was presented by first scene.
The second picture is the second scene which it wasn't presented by first scene.
I want to presente the second scene from the first scene, but I want that the objects sizes are like the objects in the second scene which is showed by second picture.
In the second scene the object sizes have been set from me.
I understood and I've done in this way:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "MainScene") {
let gameScene = SKScene(fileNamed: "GameScene")
gameScene?.scaleMode = .aspectFill
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
}
}
But it doesn't work.

Not moving from GameViewController Menu to GameScene Swift

I made my game Menu in GameViewController. Then I try to move to GameScene when the Go button has been pressed. I use the default code for presenting the GameScene file.
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
I have the Menu made in the Storyboard. I think this is why it isn't working. How should I fix this?
Niall
In SpriteKit we use scenes, the GameScene is above in GameViewController.
The GameViewController show you the GameScene.

Segues between gameScene.sks

I'm beginner in Swift and SpriteKit.
I have gameViewController with code:
if let scene = IntroScene(fileNamed:"IntroScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
My app starting with this scene. I have some code with simple animation for this scene in other viewController:
let introAnimationFadeIn = SKAction.fadeInWithDuration(0.05)
let introAnimation = SKAction.animateWithTextures([introAnimation1, introAnimation2, introAnimation3, introAnimation4, introAnimation5], timePerFrame: 1.5)
let sequenceIntroAnimation = SKAction.sequence([
introAnimationFadeIn,
introAnimation])
intro.runAction(sequenceIntroAnimation)
How I can create segue to other GameScene after animation complete?
I want to create main menu in my project. Can I create segue to other GameScene, when SKSpriteNode touched? Or create button would be better?
I know that segue can be only with viewControllers, but I don't know how names segues between game scenes correct. And sorry for my English.
If you want to go from one scene to another, you don't use segues. Segues are transitions between view controllers, not SKScenes.
To present another scene, just get an SKView and call presentScene!
let scene = YourSceneClass(fileNamed:"Name of your scene that is going to present")!
view.presentScene(scene) // view is an SKView
And it seems like you want to do an animation before presenting the scene. You don't need SKActions for this. Why not use SKTransition instead?
You can use this code to transition to another scene with fade:
// again, view is an SKView
view.presentScene(scene, transition: SKTransition.fadeWithDuration(1))
Easy as that!