简体   繁体   中英

making sprite move on swift using spritekit

I am very new to using swift and Xcode in general. I wish to create a very simple game and I am trying to figure out why my ball only goes down once rather than go up and down twice when the screen is touched

override func touchesBegan(_ touches:     Set<UITouch>, with event: UIEvent?) {
    let actionUp = SKAction.moveTo(y: 1330, duration: 0.5)
    let actionDown =    SKAction.moveTo(y: 50, duration: 0.5)
    ball?.run(actionUp)
    ball?.run(actionDown)
    ball?.run(actionUp)
    ball?.run(actionDown)

    }

Just after the ball is told to move up, before it can do anything, it is told to move down, and then up again, and then down. Only the last action moveDown is seen because there are no other actions after it.

You need SKAction.sequence , otherwise the four actions will be run immediately one after another, before the previous one even finishes.

let sequenceOfActions = SKAction.sequence([moveUp, moveDown, moveUp, moveDown])
ball?.run(sequenceOfActions)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM