Simple Sprite Kit Scene Setup Going Wrong

Simple Sprite Kit Scene setup going wrong

In the interface builder, change the class of the window's view to SKView. Type-casting a UIView into SKView won't do anything unless the UIView was previously obtained by casting a SKView.

What is wrong with the coordinate system in this SpriteKit setup?

This is the way it works. All OpenGL views (the 2D ones at least) have their origin at the lower left corner.

The position of your sprites is correct too. Both are located at 0,0 by default. The texture of the sprites is drawn relative to the node position based on the anchorPoint factor. It's default value of 0.5, 0.5 places the texture centered on the node's position.

You can change the anchorPoint of the scene to 0.5,0.5 which will move the sprites to the center of the scene. You can also change the sprite's anchorPoint though that isn't recommended since it affects things like rotation, scale, collision detection and child node position.

SpriteKit SKScene not resizing correctly to fit iPhone 12

Fix (credit @aheze)

As mentioned here, this can be fixed by setting the launch screen file in your project settings under App Icons and Launch Images.

Sprite Kit - Make scene extend up, not down, or set initial node position to bottom left corner

Create a background composition

You can create a background composition using the next function:

func backgroundNode() -> SKSpriteNode {
// Create a container node for your background
let backgroundNode = SKSpriteNode()
backgroundNode.anchorPoint = CGPointZero
backgroundNode.name = "background"

// Start the creation of each part of the background and add it to the container node
let background1 = SKSpriteNode(imageNamed: "background1")
background1.anchorPoint = CGPointZero
background1.position = CGPoint(x: 0, y: 0)
backgroundNode.addChild(background1)

let background2 = SKSpriteNode(imageNamed: "background2")
background2.anchorPoint = CGPointZero
background2.position = CGPoint(x: 0, y: background1.size.height)
backgroundNode.addChild(background2)

let background3 = SKSpriteNode(imageNamed: "background3")
background3.anchorPoint = CGPointZero
background3.position = CGPoint(x: 0, y: background1.size.height * 2)
backgroundNode.addChild(background3)

// The use of (background1.size.height * 3) depends if your background images have the same
// height, otherwise just sum al the heights instead of multiply them
backgroundNode.size = CGSize(width: background1.size.width, height: background1.size.height * 3)
return backgroundNode
}

Now you can use the function to create and place your background node:

let background = backgroundNode()
background.anchorPoint = CGPointZero
background.position = CGPointZero
background.name = "background"
addChild(background)

Scrolling or moving the background

Move the background is a quite complex task and depends too much of what your a trying to accomplish in your game.

Here is a piece of code from the Ray Wenderlich's book iOS Games by Tutorials Third Edition to move the background horizontally with the movement of the nodes and the finger, but you can try to modify it to meet your goals.

let backgroundMovePointsPerSec: CGFloat = 200.0

func moveBackground() {
enumerateChildNodesWithName("background") { node, _ in
let background = node as SKSpriteNode
let backgroundVelocity = CGPoint(x: -self.backgroundMovePointsPerSec, y: 0)
// dt(NSTimeInterval) is a property defined to keep track of the last time Sprite Kit called
// update() and the delta time since the last update. update() is the Game Loop.
let amountToMove = backgroundVelocity * CGFloat(self.dt) background.position += amountToMove
}
}

This code is using the awesome SKUtils Swift+SpriteKit extensions from Ray Wenderlich too.

Spritekit Scene content is zoomed in

You really have 2 questions here. First is what's wrong with my current setup and secondly can I use the sks file and still pass data along.

to answer the second question, yes

if let gameScene = GameScene(fileNamed: "GameScene") {

self.gameScene = gameScene
self.gameScene.stage = 1
self.gameScene.setupBasedOnStage()
self.gameScene.scaleMode = .aspectFill
self.view?.presentScene(self.gameScene, transition: SKTransition.reveal(with: .down, duration: 1.0))
}

You are able to set any custom property before revealing the page (in this case it's stage), and if you needed to you can call a setup function to load info/graphics based on the stage.

I use sks files for my scenes and setup them up like this all the time.

Crash calling a ViewController implementing a SpriteKit scene

I have solved with the following code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
ViewControllerB *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
[self presentViewController:viewController animated:YES completion:nil];


Related Topics



Leave a reply



Submit