标准化触摸位置以偏移精灵(锚点)位置

时间:2017-12-13 08:38:57

标签: ios swift3 sprite-kit offset normalize

我正在尝试编写一个“规范化偏移函数”,以便当用户触摸/移动精灵时,它不会捕捉到触摸中心(默认行为)。偏移功能可能应该在“触摸开始,触摸移动”期间解决锚点,然后在触摸结束时恢复到中心。enter link description here

class GameScene: SKScene {

private var redSquare : SKSpriteNode?
private var originalAnchorPoint: CGPoint = CGPoint(x: 0.5, y: 0.5)
override func didMove(to view: SKView) {

    // Get label node from scene and store it for use later
    self.redSquare = self.childNode(withName: "redSquare") as? SKSpriteNode

}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  for touch in (touches ) {

    let location = touch.location(in: self)
    redSquare?.position = location
    }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches ) {

        let location = touch.location(in: self)

        if (self.redSquare?.contains(location))!{

            redSquare?.position = location

        }
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    // set red sprite anchor position to originalAnchorPoint

}

0 个答案:

没有答案