【发布时间】:2017-08-06 15:52:44
【问题描述】:
我正在使用 SKTileMaps 和 CameraNode 以及 GameplayKit 和 SpriteKit
我想要工作的代码不正确在“视图”中找到位置
func handleTapFrom(_ sender: UITapGestureRecognizer)
{
let location = sender.location(in: self.view)
if (map.contains(location))
{
let tRow = map.tileRowIndex(fromPosition: location)
let tColumn = map.tileColumnIndex(fromPosition: location)
let movePosition = map.centerOfTile(atColumn: tColumn, row: tRow)
let moveAction = SKAction.move(to: movePosition, duration:1.0)
cam.run(moveAction)
}
}
正常工作的代码是在self(而不是self.view)中查找位置,这是正确的
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
let firstTouch = touches.first?.location(in: self)
if (map.contains(firstTouch!))
{
let tRow = map.tileRowIndex(fromPosition: firstTouch!)
let tColumn = map.tileColumnIndex(fromPosition: firstTouch!)
let movePosition = map.centerOfTile(atColumn: tColumn, row: tRow)
let moveAction = SKAction.move(to: movePosition, duration:1.0)
cam.run(moveAction)
}
}
列和行根据位置是正确的,我遇到的问题是两个函数之间的位置(xfloat,yfloat)不同,即使触摸在同一位置
这两段代码都在 GameScene 类中,但我不明白为什么它们的位置不同?以及如何修复点击手势,使其找到的位置与 touchesBegan 中的位置相同。
对不起,如果这令人困惑或难以理解。我欢迎有机会澄清。
【问题讨论】:
标签: swift sprite-kit uitapgesturerecognizer gamekit touchesbegan