Eli
Eli

Reputation: 768

SpriteKit - Nodes not adding to SKCameraNode - Swift

Im trying to pin the game pad controller to the bottom left on my camera node but when i add the node as a child of my camera it doesnt show up?

let gameCamera = SKCameraNode()
var joypadBackground : SKSpriteNode = SKSpriteNode(imageNamed: "a")
override func didMove(to view: SKView) {

    //Set game camera
    self.camera = gameCamera
    joypadBackground.position = convert(CGPoint(x: 0, y: 0), to: gameCamera)
    joypadBackground.size = CGSize(width: 50, height: 50)
    joypadBackground.zPosition = 1000


    gameCamera.addChild(joypadBackground)

}

Upvotes: 1

Views: 422

Answers (2)

Gallonallen
Gallonallen

Reputation: 714

I had a hard time with this same problem the first time I was working with SKCameraNode and creating a heads up display.

Basically you have to remember that there are two parts to the camera. Running its functionality and rendering its children. By setting the scene's camera to gameCamera you've setup the functionality, but your camera isn't in the node tree for rendering. So, if you ever have a camera that needs to render its children don't forget to add it to the scene as a child, then the camera's children will be displayed.

self.camera = gameCamera
self.addChild(gameCamera)

Hope that helps someone avoid a very common error with a very simple solution.

Upvotes: 2

Confused
Confused

Reputation: 6278

You don't need

convert(CGPoint(x: 0, y: 0), to: gameCamera)

You can just set the CGPoint position to (0,0) and it should be at that point relative to the camera's space.

Not sure if this helps, at all, but what I do is (generally) position a child node AFTER I've added it to its parent. This is mainly a mental reminder, to me, that the child's position is within the coordinate space of the parent. So I'd do something like this:

gameCamera.addChild(joypadBackground)
joypadBackground.position = CGPoint(x: 0, y: 0)

If you're using a mid screen origin in your SKScene, this should be in the middle of the screen.

Bottom left will be a negative x and negative y value, size of which is relative to your frame size.

Upvotes: 1

Related Questions