a question about spritekit. Every time I run a game, the object appears to me as if the coordinate point of the scene was in the middle of the scene, that is, 0.5. And I've seen the documentation, and it says that the coordinate axis of a scene is 0.0, I don't know what to do. Help, I'm a beginner. It's basically the background of the flappy bird and its character (because its components are easy to find, and I didn't want to design just for practice) where if you click or drag, the bird goes where you clicked.
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 coordinate point of the scene is in the middle, the bird does not go beyond the middle), and the other classes are intact.
First of all, UIKit's coordinate system is different from SpriteKit's. While UIKit's starts at the top left, SpriteKit's starts at the bottom left.
That said, I think your problem is that while in UIKit the position of the views is related by the x and y coordinate which is 0,0 in the upper left corner of the view (origin property of the view frame), in SpriteKit's position is given by the x and y coordinate which is also 0,0 but is at the center of the node (position property). I recommend you review this concept when creating your nodes in SpriteKit, I'm almost sure that your problem is a misunderstanding of the SpriteKit coordinate system.