Problem About SpriteKit

0

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.

    
asked by Ramon Alvarez Mora 11.09.2017 в 16:33
source

1 answer

1

First, the UIKit coordinate system is different from SpriteKit. While the UIKit starts at the top left, the SpriteKit 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 the position is given by the coordinate x and y that is also 0.0 but it is in the center of the node (property position). I recommend you to review this concept when creating your nodes in SpriteKit, I am almost sure that your problem is in a bad understanding of the SpriteKit coordinate system.

    
answered by 12.09.2017 / 15:33
source