触摸位置的坐标为零

时间:2015-01-31 10:57:12

标签: swift sprite-kit coordinates

我正在努力根据触摸获得坐标。

为了简单起见,我创建了一个使用swift和sprite kit游戏的新项目 - 默认的旋转太空船游戏。

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)
    }
}

我添加到项目中的所有内容都是NSLog行来输出触摸的坐标。但我输入的所有内容都是0 xy

有人有任何想法吗?

1 个答案:

答案 0 :(得分:1)

如果要使用location和{x打印坐标,请将y对象返回变量FloatNSLog的任何内容转换为%f {1}}格式化程序。

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

如果您没有使用NSLog,那么您也可以执行Okapi在评论中建议的内容并使用println。这两种机制都有效。

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