I am attempting to set the duration of SKAction.moveBy to reduce by 1.0 every time it repeats. For some reason when SKAction.repeatForever is ran, variables such as duration (or wallSpeedMultiplier) do not get changed on the repeat. Does anyone know a work around to this?
I tried putting it in a sequence with an action to change the duration, to move the object, then wait for the movement to be done to repeat.
var wallSpeedMultiplier = 10.0
...
let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: 15.0, midPoint: road!.position)
road!.run(SKAction.resize(toWidth: 100, duration: 2.0))
let multiplyIt = SKAction.run { [self] in
wallSpeedMultiplier = wallSpeedMultiplier - 1.0
}
let moveIt = SKAction.moveBy(x: size.width, y: 0, duration: wallSpeedMultiplier)
let waitIt = SKAction.wait(forDuration: wallSpeedMultiplier)
let sequenceIt = SKAction.sequence([moveIt,waitIt, multiplyIt])
road!.run(SKAction.repeatForever(oscillate))
road!.run(SKAction.repeatForever(sequenceIt))
Tried solution from https://www.stackoverflow.com/a/34633329/3402095
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if !gameStarted {
gameStarted = true
road!.run(SKAction.resize(toWidth: 100, duration: 2.0))
recursive()
}
}
func recursive() {
let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: wallSpeedMultiplier, midPoint: road!.position)
let multiplyIt = SKAction.run { [self] in
wallSpeedMultiplier = wallSpeedMultiplier - 5.0
}
let recursive = SKAction.sequence([oscillate, SKAction.wait(forDuration: wallSpeedMultiplier),multiplyIt])
road!.run(recursive, withKey: "aKey")
}
FIXED:
func recursive() {
let oscillate = SKAction.oscillation(amplitude: 200, timePeriod: wallSpeedMultiplier, midPoint: road!.position)
let multiplyIt = SKAction.run { [self] in
wallSpeedMultiplier = wallSpeedMultiplier - 5.0
print(wallSpeedMultiplier)
print(oscillate)
}
let recursive = SKAction.sequence([oscillate, multiplyIt, SKAction.run({[unowned self] in NSLog("Block executed"); self.recursive()})])
road!.run(recursive, withKey: "aKey")
}