Reputation: 1
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
}
}
Upvotes: 0
Views: 97
Reputation: 16827
You need to add camera to your scene. I would recommend making camera a child of James so that it always follows James
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
James.addChild(cam)
self.addChild(james)
}
}
Upvotes: 1
Reputation: 6061
I don't see any physics happening in the code you pasted. So likely didSimulatePhysics
isn't getting called. try moving the self.camera!.position = james.position
to the update function.
edit
a lot of trouble lately seems to stem from the fact that the scene is being paused by default. try setting...
self.isPaused = false
inside
override func didMove(to view: SKView)
and putting abreak point on
self.camera!.position = james.position
to ensure that it is getting called
Upvotes: 0