SKSpriteNode 的循环位置
Posted
技术标签:
【中文标题】SKSpriteNode 的循环位置【英文标题】:looping position for SKSpriteNode 【发布时间】:2018-04-17 18:55:51 【问题描述】:我认为我对 while 循环有相当深入的了解。我希望 left_spinner 从右侧的屏幕外移到左侧的屏幕外。
然后移回右侧的屏幕外并返回左侧的屏幕外。代码方面我没有发现问题。
import SpriteKit
import GameplayKit
class GameScene: SKScene
var left_spinner = SKSpriteNode()
override func didMove(to view: SKView)
left_spinner = self.childNode(withName: "left_spinner") as! SKSpriteNode
left_spinner.position = CGPoint(x: 675, y: 0)
left_spinner.run(SKAction.move(to: CGPoint(x: -675, y: 0), duration: 3.0))
goalLoop()
func goalLoop()
left_spinner.run(SKAction.move(to: CGPoint(x: -675, y: 0), duration: 3.0))
while (left_spinner.position == CGPoint(x: -675, y: 0))
left_spinner.run(SKAction.move(to: CGPoint(x: 675, y: 0), duration: 0.0))
left_spinner.run(SKAction.move(to: CGPoint(x: -675, y: 0), duration: 3.0))
override func update(_ currentTime: TimeInterval)
// Called before each frame is rendered
【问题讨论】:
【参考方案1】:您不需要任何同步循环,SKAction API 为您提供了对操作进行排序的方法。只需将您的整个代码替换为:
override func didMove(to view: SKView)
left_spinner = self.childNode(withName: "left_spinner") as! SKSpriteNode
left_spinner.position = CGPoint(x: 675, y: 0)
// Prepare base actions
let moveLeftAction = SKAction.move(to: CGPoint(x: -675, y: 0), duration: 3.0)
let moveRightAction = SKAction.move(to: CGPoint(x: 675, y: 0), duration: 3.0)
// Prepare sequencing
let loopCount = 10
let leftRightAction = SKAction.sequence([moveLeftAction, moveRightAction])
let pingPongAction = SKAction.repeat(leftRightAction, count: loopCount)
// Run final action
left_spinner.run(pingPongAction)
【讨论】:
以上是关于SKSpriteNode 的循环位置的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 SKAction 改变 SKSpriteNode 的位置
如何使任何模拟器/设备的 SKSpriteNode 位置相同?