Rae Tucker
Rae Tucker

Reputation: 523

Q: How do I move objects down the y axis as my player moves up?

Creating a game like Doodle Jump where my player is constantly being moved up by bouncing off of obstacles. Ive tried every trick in the book but nothing seems to be working/doing exactly what I want. Can anyone give me some tips?

Upvotes: 1

Views: 212

Answers (1)

jmcmahon443
jmcmahon443

Reputation: 302

iOS 9 introduced the Camera Node. Use SKCameraNode, which is a subclass of SKNode, and can be translated and rotated in same way.

So, instead of moving all of your background elements in the opposite direction of your hero/player, you can simply attach your scene's camera node to your hero/player and the rest is taken care of.

PS. You can also do cool stuff like scaling the camera size.

EDIT.

Happy to include an example.

First, make a camera constant in your scene.

import SpriteKit

class myFirstScene: SKScene {
  let myCamera: SKCameraNode = SKCameraNode()
  ...
}

Then in your didMoveToView() function, assign the scene's built-in camera variable to the camera constant we made earlier.

override func didMoveToView( view: SKView ) {
  camera = myCamera
  ...
}

Now, there are a few different ways to "attach" your camera to your hero/player. The first is to attach your camera node to your hero.

hero.addChild( myCamera )

I don't even know if it works that easily because my game uses something different, a simpler version is below.

update(){
  camera!.zRotation = hero.zRotation
  camera!.position = hero.position
}

Upvotes: 1

Related Questions