Not moving from GameViewController Menu to GameScene Swift - 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.

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)
}
}
}
}

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.

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!

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

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

How to call a function in viewController from GameScene

I am wanting to call a share function during game play. I have some code I can use in my GameViewController
func showTweetSheet() {
let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetSheet.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
//Add code to deal with it being cancelled
break
case SLComposeViewControllerResult.Done:
//Add code here to deal with it being completed
//Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
break
}
}
tweetSheet.setInitialText("Test Twitter") //The default text in the tweet
tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on
self.presentViewController(tweetSheet, animated: false, completion: {
//Optional completion statement
})
}
I have also set the ViewController in my GameScene Class...
var viewController: GameViewController!
... and set the scene.viewController to self in my ViewController
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? 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)
scene.viewController? = self
}
}
However, When I call the function like so...
viewController.showTweetSheet()
... from my GameScene it gives me a "found nil when unwrapping an optional value" error.
I think I may need to set the scene.viewController to self later on, but I don't know how to do that in the viewController.
Any help would be much appreciated.
First of all, you don't need the question mark after scene.viewController.
Second, scene.viewController = self should come before skView.presentScene(scene). This will probably fix your problem.
Lastly, it's considered bad design (or at least sloppy) to make an SKScene have a property that is a UIViewController. The scene class is now tied to using a UIViewController, and if you want to extend your code to something that doesn't use UIViewController to control views (e.g. you want to make a Mac version of your game), it won't work right away because it's hard-coded to work with UIViewController.
The "pure" way to do this would be a technique called "delegation" by iOS programmers. You create a protocol that will be your delegate, and make your view controller implement that protocol. Then the SKScene uses the protocol, not UIViewController.
All that said, you might want to leave out this complexity.