I am trying to make a game on swift SpriteKit (an RPG more specifically). I am having an issue with making the camera follow my character. So far all that happens is the camera appears off screen and you can't even see the character. Thanks!
import SpriteKit
class GameScene: SKScene {
let cam = SKCameraNode()
let james = SKSpriteNode()
override func didMove(to view: SKView) {
self.anchorPoint = .zero
var background = SKSpriteNode(imageNamed: "image-3")
background.size = CGSize(width: 1500, height: 1000);
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
addChild(background)
view.ignoresSiblingOrder = false
self.camera = cam
let james = Player()
james.size = CGSize(width: 40, height: 40)
james.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
james.zPosition = 1
self.addChild(james)
}
override func didSimulatePhysics() {
self.camera!.position = james.position
}
}
I don't see any physics happening in the code you pasted. So likely
didSimulatePhysicsisn't getting called. try moving theself.camera!.position = james.positionto the update function.a lot of trouble lately seems to stem from the fact that the scene is being paused by default. try setting...
inside
and putting abreak point on
to ensure that it is getting called