【问题标题】:Did touch function not working in SpriteKit触摸功能在 SpriteKit 中不起作用吗
【发布时间】:2025-12-01 16:50:01
【问题描述】:
import SpriteKit


class GameScene: SKScene {
    var cam: SKCameraNode?
    var player: SKSpriteNode?
    
    
    
    
    override func didMove(to view: SKView) {
       super.didMove(to: view)
       cam = SKCameraNode()
       self.camera = cam
       self.addChild(cam!)
        player = self.childNode(withName: "player") as? SKSpriteNode
    }
    
     func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touch:UITouch = touches.anyObject() as! UITouch
        let touchLocation = touch.location(in: self)

        if touchLocation.x < self.frame.size.width / 2 {
            let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
            player?.run(moveRight)
        } else {
            let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
            player?.run(moveLeft)
            
        }
    }
    
    
    override func update(_ currentTime: TimeInterval) {
        super.update(currentTime)
        if let camera = cam, let pl = player {
          camera.position = pl.position
        }
      }
    
    
}

这是我的整个游戏场景和我使用 sk 场景拖放到屏幕上的精灵。如果这会影响任何事情,我已经在场景中添加了一个相机。感谢您的帮助。

【问题讨论】:

  • 我认为你应该重写 touchesBegan 函数而不是创建你自己的函数。
  • @JohnL 给我这个错误 - 方法不会覆盖其超类中的任何方法

标签: ios swift xcode sprite-kit


【解决方案1】:

您需要覆盖触摸开始功能。它还有助于放入打印声明以表明正在接收触摸。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  for touch in touches {
  print("touch detected")
  let touchLocation = touch.location(in: self)

   if touchLocation.x < self.frame.size.width / 2 {
       let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
       player?.run(moveRight)
   } else {
       let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
       player?.run(moveLeft)
   }
  }
 }

【讨论】:

  • 谢谢它有效,但无论我在哪里点击它似乎都只会向右移动。我认为这是因为我指定了框架大小而不是显示的实际视图。你知道我该如何解决这个问题吗?
  • 我建议您打印 touchLocation.x 并多次触摸屏幕以了解您正在处理的数据。也打印出 frame.size.width。比较这 2 个,因为它们构成了 if 语句的 2 个部分。
  • 您可以在运行 moveRight 或 moveLeft 操作之前查看 player?.removeAllActions(),它将停止当前在节点上运行的所有操作。
  • 所以当我触摸最右边时它会向左移动,然后当我在同一个地方再次点击它时它会再次向右移动哈哈。我现在就按照你的指示去做。
【解决方案2】:
import SpriteKit


class GameScene: SKScene {
    var cam: SKCameraNode?
    var player: SKSpriteNode?
    
    
    
    
    override func didMove(to view: SKView) {
       super.didMove(to: view)
       cam = SKCameraNode()
       self.camera = cam
       self.addChild(cam!)
        player = self.childNode(withName: "player") as? SKSpriteNode
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      for touch in touches {
      print("touch detected")
      let touchLocation = touch.location(in: self)

        if touchLocation.x < (player?.position.x)! {
           let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
        print("right")
           player?.run(moveRight)
       } else {
           let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
        print("Left")
           player?.run(moveLeft)
       }
      }
     }
    
    override func update(_ currentTime: TimeInterval) {
        super.update(currentTime)
        if let camera = cam, let pl = player {
          camera.position = pl.position
        }
      }
    
    
}

我在 JohnL 的帮助下想通了。通过添加 if touchLocation.x

【讨论】: