I'm working on a project and tried to reset the position with an SKAction
, but the SKSpriteNode
didn't move! Can anyone help?
import SpriteKit
import Foundation
enum Difficulty: TimeInterval {
case easy = 8.5
case normal = 6.0
case hard = 4.0
case extraHard = 2.0
}
class Note: SKSpriteNode {
init() {
super.init(texture: SKTexture(imageNamed: "dropNotes"), color: .clear, size: CGSize(width: 50, height: 50))
self.position = CGPoint(x: 375, y: 999)
}
func fall(difficulty: Difficulty) {
let fallAction = SKAction.move(
to: CGPoint(
x: 375,
y: 180
),
duration: difficulty.rawValue
)
let resetAction = SKAction.move(to: CGPoint(x: 375, y: 700), duration: 0.01)
run(fallAction)
run(resetAction)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Does anyone know why this happpened? Please answer if you do!
Look at this part of your code
run(fallAction)
run(resetAction)
You are running these 2 actions at the same time. Instead I guess you want to run the fallAction
and then, AFTER it finishes, the resetAction
right?
In this case you need to create a sequence and run it.
let sequence = SKAction.sequence([fallAction, resetAction])
run(sequence)
Here's the full fall method updated
func fall(difficulty: Difficulty) {
let fallAction = SKAction.move(
to: CGPoint(
x: 375,
y: 180
),
duration: difficulty.rawValue
)
let resetAction = SKAction.move(to: CGPoint(x: 375, y: 700), duration: 0.01)
let sequence = SKAction.sequence([fallAction, resetAction])
run(sequence)
}