SKAction.moveToX repeat with value change

女生的网名这么多〃 提交于 2020-01-06 15:17:47

问题


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
    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:


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
    ball.removeFromParent()
    ball.name = "spriteToTrack"

    ball.position = CGPointMake(x, 1000)

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


来源:https://stackoverflow.com/questions/38089587/skaction-movetox-repeat-with-value-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!