【问题标题】:Make a sprite move in all directions让精灵向各个方向移动
【发布时间】:2018-12-03 14:58:45
【问题描述】:
@objc func handleSwipe(gesture: UIGestureRecognizer) {
    if let gesture = gesture as? UISwipeGestureRecognizer {
        switch gesture.direction {
        case .up:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 110))
            print("Swiped up")
        case .down:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: -110))
            print("Swiped down")
        case .right:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 110, dy: 0))
            print("Swiped right")
        case .left:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: -110, dy: 0))
            print("Swiped left")
        default:
            print("No such gesture")
        }
    }

}

我试图让我的精灵节点在所有方向上移动,包括对角线和 90 度到 45 度之间的每个角度。这就是我所拥有的,现在不知道该怎么做。有什么帮助吗?

【问题讨论】:

    标签: ios swift sprite-kit uigesturerecognizer uiswipegesturerecognizer


    【解决方案1】:

    我认为UISwipeGestureRecognizer 不够灵活,无法满足您的需求。您可能需要使用 UIPanGestureRecognizer 获取从触摸事件开始到结束的向量

    This tutorial on UIGestureRecognizer 应该有你要找的东西,特别是减速部分。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2020-02-13
    • 2018-06-11
    • 2012-07-24
    相关资源
    最近更新 更多