Reputation: 1743
I'm pretty new to Sprite Kit and I'm trying to develop a game where some monsters are moving around forever using this code.
return SKAction.sequence([
SKAction.runBlock({ self.moveMonster(monster) }),
SKAction.waitForDuration(monster.movementSpeed())
])
The thing is that I would like the wait duration to vary using the monster speed, which is a function.
This code doesn't achieve what I'm trying to do because the movementSpeed function is called only once.
Thank you very much for your help!
Upvotes: 2
Views: 1298
Reputation: 24572
You can use the completionHandler
in runAction
to add a different SKAction sequence
each time to change your wait duration
. For example.
func addSKAction(waitDuration : NSTimeInterval)
{
let moveSprite = SKAction.runBlock({ () -> Void in
self.moveMonster(monster)
})
let waitDuration = SKAction.waitForDuration(waitDuration)
let sequence = SKAction.sequence([moveSprite,waitDuration])
spriteNode.runAction(sequence, completion: { () -> Void in
addSKAction(waitDuration) // Change wait duration each time.
})
}
Change the value of the waitDuration
variable each time.
If you just want to randomly change waitDuration
, you can use SKAction.waitForDuration:withRange:
let moveSprite = SKAction.runBlock({ () -> Void in
self.moveMonster(monster)
})
let waitDuration = SKAction.waitForDuration(5, withRange: 4)
let sequence = SKAction.repeatActionForever(SKAction.sequence([moveSprite,waitDuration]))
This will change the waitDuration
from (5 - range/2) = 3 to (5 + range/2) = 7 randomly.
Upvotes: 2
Reputation: 8130
i wouldnt use an action in this case. when a timer needs to vary I think it makes more sense to use your update method.
declare two properties to your scene
var monsterTimer: NSTimerInterval(2)
var monsterInterval: NSTimerInterval(2)
in your update method
self.monsterTimer -= self.delta
if self.monsterTimer <= 0 {
self.moveMonster(monster)
// change the value of monsterInterval if you need to change the delay
self.monsterTimer = self.monsterInterval
}
Upvotes: 1