【问题标题】:How do you make a Node move forever in one direction?如何让节点永远朝一个方向移动?
【发布时间】:2015-08-07 13:13:32
【问题描述】:

我正在尝试在 SpriteKit(Xcode 7 测试版)中制作像 Doodle Jump 这样的垂直无尽跑步者,但不知道如何不断向上移动 Node

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)
}

【问题讨论】:

  • 你能展示你的尝试吗?
  • 子类SKSpriteNode 并在覆盖的update 方法中修改精灵位置。

标签: ios swift sprite-kit swift2


【解决方案1】:

在回答了您的最新帖子后,碰巧在这里看到了您的帖子;我想我也会回答。

您只需要以下内容,它告诉SKAction 重复一个动作作为参数:

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

bubble.runAction(float)

附注:

在 Swift 中,可以将变量命名为 float。但它被广泛反对,因为有一个一等变量,即float。但在 Swift 中,float 是大写的;因此,这里没有冲突。但是,为了避免我们的额头不时被高级程序员折断,我们可以将您的操作命名为,例如,“浮动”,我觉得它很合适,因为“-ing”(连续动词形式动名词),传达行动感。

【讨论】:

  • 如果有人在看 let float = SKAction.repeatForever(SKAction.moveTo(y: +yScale, duration: 0.2)) player.run(float) 是根据 ios 13+ 更新的代码
猜你喜欢
  • 2017-09-04
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
相关资源
最近更新 更多