SKAction.moveToX重复更改值

时间:2016-06-29 02:54:41

标签: ios swift sprite-kit skspritenode skaction

我希望能够每隔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 个答案:

答案 0 :(得分:2)

至少在上面的代码中,球应该移动20个像素,但是10秒内的20个像素可能看起来像停顿一样。无论如何我认为你使用moveToX而不是moveBy:过分复杂的事情,所以(有点重新调整)你可能会更喜欢这样的事情:< / p>

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