SKAction.moveToX 重复值变化
Posted
技术标签:
【中文标题】SKAction.moveToX 重复值变化【英文标题】:SKAction.moveToX repeat with value change 【发布时间】:2016-06-29 02:54:40 【问题描述】:我希望能够在每移动 20 个像素时暂停移动球我尝试了这个但什么也没做,球停留在它开始的点它不会移动到屏幕的末端,我知道我没有放 waitForDuration ,因为我想检查它是否会移动
func spwan()
let ball:SKSpriteNode = SKScene(fileNamed: "Ball")?.childNodeWithName("ball") as! SKSpriteNode
ball.removeFromParent()
self.addChild(ball)
ball.name = "spriteToTrack"
ball.zPosition = 0
ball.position = CGPointMake(1950, 1000)
var num:CGFloat = ball.position.x
let a1 = SKAction.moveToX(num - 20, duration: 10)
// i want to get to -50 let a1 = SKAction.moveToX(-50 , duration: 10)
let minus = SKAction.runBlock
num -= 20
let sq1 = SKAction.sequence([a1,minus])
ball.runAction(SKAction.repeatAction(sq1, count: 10)
【问题讨论】:
很好,我看到您刚刚收到了很好的答复。祝你好运 【参考方案1】:在上面的代码中,球至少应该移动 20 像素,但 10 秒内移动 20 像素可能看起来像是静止的。无论如何,我认为您使用moveToX
而不是moveBy:
使事情变得过于复杂了,所以(稍微调整一下)你可能会更好地使用这样的东西:
func spawn()
let x: CGFloat = 1950
let xDelta: CGFloat = -20
let xDestination: CGFloat = -50
let repeats = Int((x - xDestination)/fabs(xDelta))
let move = SKAction.moveBy(CGVectorMake(xDelta, 0), duration: 2) // made it a lot quicker to show that stuff is happening
move.timingMode = .EaseInEaseOut
let ball:SKSpriteNode = SKScene(fileNamed: "Ball")?.childNodeWithName("ball") as! SKSpriteNode
ball.removeFromParent()
ball.name = "spriteToTrack"
ball.position = CGPointMake(x, 1000)
addChild(ball)
ball.runAction(SKAction.repeatAction(move, count: repeats))
【讨论】:
以上是关于SKAction.moveToX 重复值变化的主要内容,如果未能解决你的问题,请参考以下文章