首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpriteKit中的正弦波运动

SpriteKit中的正弦波运动
EN

Stack Overflow用户
提问于 2014-02-22 00:58:43
回答 3查看 2.9K关注 0票数 10

我想让正弦波从屏幕上的第一个点到屏幕上的最后一个点,与屏幕的大小无关。

以下是我的代码,但它不能正常工作:

代码语言:javascript
复制
 SKSpriteNode* RedBird = (SKSpriteNode*)[self childNodeWithName:@"RedBird"];
CGPoint currentPoint=CGPointMake(-60,0);
double width=self.frame.size.width/4;
CGPoint cp1=CGPointMake(width, self.frame.size.height/2);
CGPoint cp2=CGPointMake((width*3), 0);
CGPoint e=CGPointMake(self.frame.size.width+200, 0);


CGMutablePathRef cgpath = CGPathCreateMutable();


CGPathMoveToPoint(cgpath,NULL, currentPoint.x, currentPoint.y);
CGPathAddCurveToPoint(cgpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);



[RedBird runAction:[SKAction group:@[[SKAction repeatActionForever:RedBirdAnimation],[SKAction followPath:cgpath asOffset:YES orientToPath:NO duration:12.0]]]];


CGPathRelease(cgpath);
EN

回答 3

Stack Overflow用户

发布于 2015-05-12 16:26:37

我同意@Gord的观点,因为我认为SKActions是最好的选择。但是,当您可以使用sin函数时,不需要近似正弦曲线。

首先,您需要π,因为它对计算很有用:

代码语言:javascript
复制
// Defined at global scope.
let π = CGFloat(M_PI)

其次,扩展SKAction (在Objective-C中,这将使用categories来完成),以轻松地创建一个SKAction,它振荡有问题的节点:

代码语言:javascript
复制
extension SKAction {
    static func oscillation(amplitude a: CGFloat, timePeriod t: CGFloat, midPoint: CGPoint) -> SKAction {
        let action = SKAction.customActionWithDuration(Double(t)) { node, currentTime in
            let displacement = a * sin(2 * π * currentTime / t)
            node.position.y = midPoint.y + displacement
        }

        return action
    }
}

在上面的代码中:amplitude是振荡的高度;timePeriod是一个完整周期的时间,midPoint是振荡发生的点。displacement的公式来自Simple Harmonic Motion的方程式。

第三,把所有这些放在一起。可以将SKAction.oscillation动作和SKAction.moveByX结合使用,以使精灵沿曲线路径移动。

代码语言:javascript
复制
class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        let node = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 50, height: 50))
        node.position = CGPoint(x: 25, y: size.height / 2)
        self.addChild(node)

        let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: 1, midPoint: node.position)
        node.runAction(SKAction.repeatActionForever(oscillate))
        node.runAction(SKAction.moveByX(size.width, y: 0, duration: 5))
    }
}
票数 14
EN

Stack Overflow用户

发布于 2015-05-12 14:09:50

这是一个旧的帖子,但我想张贴一个答案,使它可以谷歌。我在SKSpriteNode的一个子类中实现了这个函数,它使精灵从一边到另一边以近似的正弦波移动。

这是快速的,但可以很容易地在objective C中完成。此外,你可能想要更智能,并将路径构建放在一个循环中,但我只是保持它没有循环,以使数学易于理解。

(我并不反对游戏循环的答案,但它有点违背了Sprite Kit的理念)

代码语言:javascript
复制
func setSineAction()
{
    let scene = self.parent as! SKScene

    let y42 = scene.size.height/2
    let x1 = scene.size.width
    let x2 = scene.size.width * 0.875
    let x3 = scene.size.width * 0.750
    let x4 = scene.size.width * 0.625
    let x5 = scene.size.width * 0.500
    let x6 = scene.size.width * 0.375
    let x7 = scene.size.width * 0.250
    let x8 = scene.size.width * 0.125
    let x9 = 0.0 as CGFloat

    let path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, x1, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x1/2)+(x2/2), scene.size.height*0.7, x2, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x2/2)+(x3/2), scene.size.height*0.3, x3, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x3/2)+(x4/2), scene.size.height*0.7, x4, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x4/2)+(x5/2), scene.size.height*0.3, x5, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x5/2)+(x6/2), scene.size.height*0.7, x6, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x6/2)+(x7/2), scene.size.height*0.3, x7, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x7/2)+(x8/2), scene.size.height*0.7, x8, y42)
    CGPathAddQuadCurveToPoint(path, nil, (x8/2)+(x9/2), scene.size.height*0.3, x9, y42)

    self.runAction(SKAction.followPath(path, asOffset: false, orientToPath: false, duration: 10))

}
票数 1
EN

Stack Overflow用户

发布于 2014-02-27 19:03:15

我会创建一个基于更新所有游戏元素的游戏循环,而不是依赖于SKActions。SKActions通常是一种执行动画的方法,因为游戏元素的连续移动创建了一个简单的引擎,并让它更新你的红色小鸟的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21940495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档