Scene transition troubleshoot in spritekit in swift - ios

I'm Trying to make a Start Scene Where you press a "Start" Button, and it goes to the main game scene. Here's the code:
if startBTN.containsPoint(location) {
let gameSceneTemp = GameScene(fileNamed: "GameScene")
self.scene?.view?.presentScene(gameSceneTemp, transition: SKTransition.fadeWithDuration(0.01))
}
But for some reason, whenever the scene loads up, it displays a squished-like scene in the GameScene. Can anyone help solve this problem by displaying a normal size (portarit) scene? Here's the Screen shots between the expected and actual scene:
The Expected Scene
The Actual Scene (The Problem)

It turns out, I didn't set the scene mode. I looked this problem up and I finally found the solution. Here's the code:
if startBTN.containsPoint(location) {
var gameScene = GameScene(size: self.size)
var transition = SKTransition.fadeWithDuration(0.8)
gameScene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view?.presentScene(gameScene, transition: transition)
}
I had put the gamescene file and transition, but didn't add the scalemode to it.

Related

adding uitextfield or (textentry box with keyboard) in sprite kit

I am recently working on sprite kit to create a simple game where user input name as identity and moves forward to play game and has option to edit name or replace name.
Which is best option to use uitextfield: in skscene or viewcontroller? Or any other solutions?
I had tried with uitextfield adding in skscene but got memory issue (since i called scene from viewwilllayoutSubview
override func viewWillLayoutSubviews() {
let skView = self.view as! SKView
skView.ignoresSiblingOrder = true
let scene =
NameEntryScene(size:CGSize(width: 1024, height: 718))
scene.scaleMode = .aspectFill
skView.presentScene(scene)
}
which I followed stackoverflow others answer to add uitextfield in gamesence at didMove(to view: SKView).
Also created uitextfield in viewcontroller and pass its instance all over the gamesence with action of isHidden true and false to remove memory issue, which worked but I can't work like this if i have many uitextfield's at various postions.
Any suggestion and help is very much appreciated. Thanks in advance.

Sprites disappears sometimes [Sprite Kit]

Main menu looks like this.
When i press start code below changes the scene.
let newScene = LevelScene(size: self.scene!.size)
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Up, duration: 1)
newScene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view!.presentScene(newScene, transition: nil)
And after I return to the main menu it looks like this
or this
So, as you can see, sprites just disappears. I can't come up with a reason for this to happen.
Are you doing,
self.addChild(...)
self.addChild(...)
in your didMoveToView method?
Another possible problem might be that you are setting the zPosition of the node to less than your background, and / or other nodes.
I had the same problem with sprites disappearing and after changing the zPositions of all my sprites and background I still wasn't able to solve it.
I ended up just redeclaring the scene inside my restartButton:
var scene = SKScene(fileNamed: "GameScene")
scene?.scaleMode = .aspectFill
view!.presentScene(scene)
And that worked for me.

How to create starting screen in SpriteKit with swift

In my game right now I've got a GameScene.swift and a GameOverScene.swift. When the game loads, GameScene.swift shows up and the game plays great. However, now that I'm done with the gameplay, how can I add a start screen that displays when the app opens?
To create the scene that runs when your app first opens, you will have to make some adjustments to the game's view controller. First change
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 change the opening scene from GameScene to sceneName, or whatever you called your scene. Also, you must declare you scene like this: class sceneName:SKScene {...}. You can create a new .swift file (cmd + n) to score this class in for easy file management or you can put it in the GameScene file. Happy coding.

SKShapeNode Circle not appearing circular

I am working on an app where there should be a red circle on the screen. At first when I tested with only one scene, the circle appeared circular just fine. However, I have added in another scene and now the circle appears like this:
The circle appears to be much longer in the x direction.
Here is the code I use to make the circle (All of this in an skscene):
let ball = Ball(circleOfRadius: 500)//ball is a subclass of SKShapeNode
ball.setScale((self.frame.height / 6) / 1000)
ball.position = CGPointMake(self.frame.width / 4, self.frame.height / 2)
ball.fillColor = UIColor.redColor()
ball.strokeColor = UIColor.clearColor()
self.addChild(ball)
Here is the code I use to transition to the scene with the ball (A different skscene):
func transitionToGame(){
let reveal = SKTransition.crossFadeWithDuration(1.0)
let gameOverScene = GameScene(size: self.size)
self.view?.presentScene(gameOverScene, transition: reveal)
}
Any ideas on why the ball would no longer appear as a circle? I a worried this is also going to throw off other nodes I attempt to add, and possible means the coordinate system is incorrect. I am happy to hear any ideas. Thank you!
"All of this in an skview" are you sure you aren't doing that in a SKScene?
If the ball is indeed getting added directly to the scene I would look at the scene.scaleMode in your View Controller. Most of the time people set it to scene.scaleMode = .AspectFill but when you create a new scene you aren't setting the scaleMode. This might be as easy as making sure both scenes have the same scaleMode.
I am guessing this will fix your issue.
func transitionToGame(){
let reveal = SKTransition.crossFadeWithDuration(1.0)
let gameOverScene = GameScene(size: self.size)
gameOverScene.scaleMode = scene.scaleMode
self.view?.presentScene(gameOverScene, transition: reveal)
}
Hopefully that helps.

How to stop Sprite Kit from reinitializing the scene with iAd banner

I made a game using sprite kit in landscape mode. The only allowed orientations are landscape left and landscape right. When I select the banner ad in my game, rather than freezing the scene and returning to the original point in the game, the entire scene reinitializes itself (music player restarts, content shows as start screen, etc).
The same thing happens when I flip the phone around so that the orientation switches. How can I prevent this?
As for when you flip your phone you need this...
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
SKView * skView = (SKView *)self.view;
if ( !skView.scene ) {...
SKScene * scene = [MenuScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
}
I'm still looking into the iAd issue, I'll update if I figure anything out.
Thanks for the response. That was the problem. I had been initializing the scene in the viewWillLayoutSubviews method in my root view controller. I moved the scene initialization to the viewDidLoad method and it fixed the issue.

Resources