a question about spriteKit. Every time I play a game, the object appears to me as if the point of coordinates of the scene were in the middle of the scene, ie 0.5. And I've seen the documentation, and it says that the coordinate axis of a scene is 0.0, I do not know what to do. Help, I'm a beginner.
Basically it is the flappy bird background and its character (because its components are easily found, and I did not want to design just to practice) where if you press or drag, the bird goes where you pressed.
class GameScene: SKScene {
let pajaro = SKSpriteNode(imageNamed: "pajaro")
var lastUpdateTime : TimeInterval = 0
var dt : TimeInterval = 0
let pajaroPixelesPerSecond: CGFloat = 300.0
var velocity = CGPoint.zero
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.zPosition = -1
addChild(background)
pajaro.position = CGPoint(x: -210, y: 150)
pajaro.xScale = 1/2
pajaro.yScale = 1/2
addChild(pajaro)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: self)
sceneTouched(touchLocation: location)
}
override func update(_ currentTime: TimeInterval) {
if lastUpdateTime > 0 {
dt = currentTime - lastUpdateTime
}else{
dt = 0
}
lastUpdateTime = currentTime
moveSprite(sprite: pajaro, velocity: velocity)
checkBounds()
}
func moveSprite(sprite: SKSpriteNode, velocity: CGPoint){
let amount = CGPoint(x: velocity.x * CGFloat(dt), y: velocity.y * CGFloat(dt))
print("La cantidad que nos movemos \(amount)")
sprite.position = CGPoint(x: sprite.position.x + amount.x, y: sprite.position.y + amount.y)
}
func movePajaroToLocation(location: CGPoint){
let offset = CGPoint(x: location.x - pajaro.position.x, y: location.y - pajaro.position.y)
let offsetlenght = sqrt(Double (offset.x*offset.x + offset.y*offset.y))
let direction = CGPoint(x: offset.x/CGFloat(offsetlenght), y: offset.y/CGFloat(offsetlenght))
velocity = CGPoint(x: direction.x * pajaroPixelesPerSecond, y: direction.y * pajaroPixelesPerSecond)
}
func sceneTouched(touchLocation: CGPoint){
movePajaroToLocation(location: touchLocation)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: self)
sceneTouched(touchLocation: location)
}
func checkBounds() {
let bottonLeft = CGPoint.zero
if pajaro.position.x <= bottonLeft.x {
pajaro.position.x = bottonLeft.x
velocity.x = -velocity.x
}
}
I hope you can help me (the last function is to prevent the bird from leaving the screen, but since the point of coordinates of the scene is in the middle, it does not pass half the bird), and the other classes are intact.