0

我使用SKTileMapsCameraNode以及GameplayKitSpriteKitUITapGesture和的touchesBegan是生产针对不同位置

我要工作的代码是不正确找到在“视图”中的位置

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

}  

和代码是wor王正确是找到自我的位置(而不是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中找到的位置相同。

对不起,如果这是混乱或无法理解。我很欢迎有机会澄清。

回答

0

在您的handleTapFrom函数中,位置应该来自convertPoint

func handleTapFrom(_ sender: UITapGestureRecognizer) { 
    if sender.state != .ended { 
     return 
    } 

    let location = sender.location(in: sender.view!) 
    let targetLocation = self.convertPoint(fromView: location) 

    if map.contains(targetLocation) { 
     // Your code here 
    } 
} 
+0

我在发布后不久就想到了,但谢谢!更重要的是,首先if语句将缓解我在调用多个手势识别器时遇到的很多问题。 –

相关问题