【问题标题】:Using Swipe Recognizer in SpriteKit在 SpriteKit 中使用滑动识别器
【发布时间】:2017-10-24 16:19:55
【问题描述】:

我正在尝试在 SpriteKit 中的游戏中实现左右滑动手势,其中玩家收集从屏幕顶部生成的坠落物体。我面临的问题是试图在手指在屏幕上时保持玩家的连续移动,直到触摸结束并且玩家停留在最后一次触摸结束的地方。除了滑动手势之外,可能还有更好的方法来实现这一点,因此我问你们!任何帮助都会很棒,谢谢大家!

【问题讨论】:

    标签: swift sprite-kit move swipe-gesture


    【解决方案1】:

    这不是您想要使用滑动(我认为您正在使用平移)手势的东西。你要做的就是覆盖场景中的touchesBegantouchesMovedtouchesEnded调用,根据这3个方法规划你的运动。

    您可能希望将SKAction.move(to:duration:) 与这些方法一起使用,并计算出保持恒定速度的数学运算。

    例如

    func movePlayer(to position:CGPoint)
    {
      let distance = player.position.distance(to:position)
      let move = SKAction.move(to:position, duration: distance / 100) // I want my player to move 100 points per second
      //using a key will cancel the last move action
      player.runAction(move,withKey:"playerMoving")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
    {
      let touch = touches.first
      let position = touch.location(in node:self)
      movePlayer(to:position)
    }
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) 
    {
      let touch = touches.first
      let position = touch.location(in node:self)
      movePlayer(to:position)
    }
    
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
      let touch = touches.first
      let position = touch.location(in node:self)
      movePlayer(to:position)
    }
    

    【讨论】:

    • 太棒了,这工作,非常感谢老兄! @KnightOfDragon
    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多