Ruslan Abdelrahman Ruslan Abdelrahman - 1 year ago 70
iOS Question

SKAction.moveToX repeat with value change

i want to be able move the ball with pause every 20 pixels moved i tried this one but didn't do nothing, the ball stays at the point where it started it doesn't move to the end of the screen,i know i didn't put the waitForDuration , because i wanted to check if it will move or not

func spwan()
let ball:SKSpriteNode = SKScene(fileNamed: "Ball")?.childNodeWithName("ball") as! SKSpriteNode
self.addChild(ball) = "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)

Answer Source

The ball should move 20 pixels at least in the above code, but 20 pixels in 10 seconds might seen like a standstill. Anyways I think you've overcomplicated things quite a bit by using moveToX rather than moveBy:, so (with a bit of rejigging) you're probably better of with something like this:

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 = "spriteToTrack"
    ball.position = CGPointMake(x, 1000)

    ball.runAction(SKAction.repeatAction(move, count: repeats))