如何让Node永远向一个方向移动?

时间:2015-08-07 13:13:33

标签: ios sprite-kit swift2

我试图像SpriteKit(Xcode 7 beta)中的Doodle Jump那样制作一个垂直无尽的跑步者,但是无法弄清楚如何不断向上移动节点。

override func didMoveToView(view: SKView) {

    makeBackground()

    // Bubble

    let bubbleTexture = SKTexture(imageNamed: "bubble.png")

    bubble = SKSpriteNode(texture: bubbleTexture)

    bubble.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

    bubble.physicsBody = SKPhysicsBody(circleOfRadius: bubbleTexture.size().height/2)
    bubble.physicsBody!.dynamic = false
    bubble.physicsBody!.allowsRotation = false

    let float = SKAction.moveToY(+yScale, duration: 0.2)

    bubble.SKAction.repeatActionForever(runAction(float))

    self.addChild(bubble)
} 

谢谢!

1 个答案:

答案 0 :(得分:1)

恰好在你回答了最新的帖子之后看到你的帖子;我想我也会回答它。

您需要的是以下内容,它告诉SKAction重复操作作为参数:

let float = SKAction.repeatActionForever(SKAction.moveToY(+yScale, duration: 0.2))

bubble.runAction(float)

作为旁注:

在Swift中,可以为你的vars命名,例如,float。但是它被广泛赞同,因为有一个第一类变量,即float。但在Swift中,浮动是大写的;因此,这里没有冲突。然而,为了避免我们的前额每隔一段时间被高级程序员抢购,我们可以将您的行为命名为,例如," 浮动",我觉得它很合适由于" -ing " (连续动词形式 gerund )表达了一种行动感。

相关问题