Revick
Revick

Reputation: 25

Co-ordinate of touch location is zero

I am struggling to get the coordinates based on touch.

For simplicity I have created a new Project using swift and a sprite kit game - the default spinning spaceship one.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        let sprite = SKSpriteNode(imageNamed: "Spaceship")

        sprite.xScale = 0.5
        sprite.yScale = 0.5
        sprite.position = location
        **NSLog("position x %f and y %f", location.x, location.y)**
        let action = SKAction.rotateByAngle(CGFloat(M_PI), duration: 1)

        sprite.runAction(SKAction.repeatActionForever(action))

        self.addChild(sprite)
    }
}

All I added to the project was the NSLog line to output the coordinates of the touch. But all I get in the output is 0 for x and y.

Anyone have any idea?

Upvotes: 0

Views: 169

Answers (1)

PixelCloudSt
PixelCloudSt

Reputation: 2805

Convert whatever the location object is returning for variables x and y to a Float if you want to print the coordinates using NSLog with the %f formatter.

NSLog("position x %f and y %f", Float(location.x), Float(location.y))

If you aren't dead set on using NSLog, you can also do what Okapi suggested in his comment and use println. Both mechanisms work.

println("x:\(location.x)  y:\(location.y)")

Upvotes: 1

Related Questions