swift - SpriteKit 起源和手势角度

标签 swift sprite-kit uigesturerecognizer touches

我是 SpriteKit 的新手,对于初学者的问题深表歉意。

我有两个对象,用户可以从中滑动到“目标”方向。我需要确定起始点(即触摸来自两个对象中的哪一个)和滑动角度。

我可以自己使用 touchesBegan 和 touchesEnded 来计算它们,但想知道是否有任何其他方法可以用 Sprite 来完成。

谢谢。

最佳答案

总是有另一种做某事的方式——这就是编程!但是,正如 Rube Goldberg 在下面演示的那样,另一种方法可能不是最好/最简单的方法:

enter image description here

使用 touchesEnded,您可以做出类似于“ throw ”的手势,其功能可能会有所限制。

例如,如果您希望对象在 x 的某个 f 处连续获得速度/进行小的路径更改(不必抬起手指),您可以使用 touchesMoved。用你喜欢的任何计算/函数填充它。

示例:通过触摸拖动节点:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //-Main touch processoereor
    for touch in touches {
        
        let TPOINT  = touch.locationInNode(self)
        my_super_cool_node!.position = TPOINT
        
        // Put math stuff here, etc.
        
    }
}

这使您可以更好地控制 Sprite 从 A 点到 B 点发生的情况。

这是一个很好的教程:

http://www.globalnerdy.com/2016/02/02/writing-ios-games-with-swift-and-sprite-kit-part-2-responding-to-touches/

还有 Apple 的东西: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/#//apple_ref/occ/instm/UIResponder/touchesMoved:withEvent :

不幸的是,SpriteKit 中最好的东西需要一些巧妙的实现/数学,并且不能全部用场景编辑器和节点来完成。

你说的是一个相当常规的(数学)函数,距离公式,所以用它制作一个实际的 func 是非常有利的,这样你就可以自定义它并且不必为您想做的每件事手动编写大量代码。 SK 已经让事情变得 super 简单。在使用 C++ Direct3d/OpenGL(以及大量高级数学)的日子里,即使是用旋转宇宙飞船做基本的 hello world 也需要一千行(或更多)代码。

SK 编辑器很棒,只需在后端进行一些编程,您实际上可以仅使用编辑器制作完整的游戏。

关于swift - SpriteKit 起源和手势角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192834/

相关文章:

ios - 为什么 waitForDuration : method only works once in the update method in sprite kit?

iOS。使 UIPinchGestureRecognizer 优于 UITapGestureRecognizer?

ios - UITextView startInteractionWithLinkAtPoint 崩溃仅限 iOS 11

ios - SpriteKit 丢失纹理

ios - 旋转 ImageView 动画

ios - 尝试在 Swift 中将 UILabel 居中

swift - 为什么我的场景在 swift sprite kit 中不会改变?

ios - SKTextureAtlas外部app.bundle

ios - 向 GMSMapView 添加手势识别器以获得 .begin/.ended/.change 手势

swift - fatal error : init(coder:) has not been implemented error