使用 Swift 在向下循环中移动 SKSpriteNode [英] Moving a SKSpriteNode in a downward loop, using Swift

查看:29
本文介绍了使用 Swift 在向下循环中移动 SKSpriteNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Swift 和 SpriteKit,我想以螺旋模式移动 SKSpritenode,但没有找到合适的资源来帮助我开始.更准确地说,我想在向下循环中移动精灵节点.我检查了一系列 SKAction,但由于它们不是并行执行的,因此与 move-to 相结合的圆周运动将不起作用.如果有任何提示、教程或片段能让我走上正轨,我会很高兴.

Using Swift and SpriteKit, I'd like to move a SKSpritenode in a spiral pattern, but didn't find the right resources to get me started. To be more precise, I'd like to move a sprite node in a downward loop. I've checked a sequence of SKActions, but as they are not executed parallel, a circular movement combined with move-to will not work. I'd be glad for any hints, tutorials or snippets to set me on the right track.

提前谢谢,马库斯

推荐答案

我整理了一些示例代码,您可以根据自己的目的进行调整.我已经根据 阿基米德螺旋的等式编写了代码:

I've put together some sample code which you can adapt for your purposes. I've based the code off the equation for an Archimedean Spiral:

r = a + bθ

其中a是起始半径;b 是螺旋每转增加的半径,θ 是当前角度.

Where a is the starting radius; b is the radius the spiral will increase by per revolution and θ is the current angle.

螺旋基本上是一个美化圆 (IMO),因此要在螺旋中移动您的节点,您需要能够使用角度、半径和中心点计算圆上的点:

A spiral is basically a glorified circle (IMO), so to move your node in a spiral you need to be able to calculate point on a circle using an angle, radius and center point:

func pointOnCircle(#angle: CGFloat, #radius: CGFloat, #center: CGPoint) -> CGPoint {
    return CGPoint(x: center.x + radius * cos(angle),
                   y: center.y + radius * sin(angle))
}

接下来,扩展 SKAction 以便您可以轻松创建螺旋动作:

Next, extend SKAction so you can easily create a spiral action:

extension SKAction {
    static func spiral(#startRadius: CGFloat, endRadius: CGFloat, angle 
         totalAngle: CGFloat, centerPoint: CGPoint, duration: NSTimeInterval) -> SKAction {

        // The distance the node will travel away from/towards the 
        // center point, per revolution.
        let radiusPerRevolution = (endRadius - startRadius) / totalAngle

        let action = SKAction.customActionWithDuration(duration) { node, time in
            // The current angle the node is at.
            let θ = totalAngle * time / CGFloat(duration)

            // The equation, r = a + bθ
            let radius = startRadius + radiusPerRevolution * θ

            node.position = pointOnCircle(angle: θ, radius: radius, center: centerPoint)
        }

        return action
    }
}

最后是一个使用示例.在 didMoveToView 中:

Finally, an example of use. In didMoveToView:

let node = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 10, height: 10))
node.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(node)

let spiral = SKAction.spiral(startRadius: size.width / 2,
                             endRadius: 0,
                             angle: CGFloat(M_PI) * 2,
                             centerPoint: node.position,
                             duration: 5.0)

node.runAction(spiral)

这篇关于使用 Swift 在向下循环中移动 SKSpriteNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆