Reputation: 139
var action = SKAction.sequence([
SKAction.waitForDuration(1),
SKAction.runBlock({
//Some code
})
])
I want the above action to keep repeating while some condition is true. How can I do this?
I know I can run the action once with runAction(action)
or repeat forever using runAction(SKAction.repeatActionForever(action))
. How do I only repeat it while some condition is true though?
If this isn't possible using actions, is there an alternative to how I can repeat these steps while some condition is true (obviously, on a separate thread, I don't want this to freeze my app):
1) Wait for a second
2) Execute a block of code
3) Check if the condition is true. If it is stop repeating. Else, repeat.
I'm hesitant to use sleep()
because that sounds like a bad solution, and something Apple wouldn't allow for apps in their store.
Upvotes: 2
Views: 1425
Reputation: 139
Alternative Solution using Swift:
runAction(
SKAction.repeatActionForever (
SKAction.sequence([
SKAction.waitForDuration(1),
SKAction.runBlock({
//Code you want to execute
if conditionIsTrue {
self.removeActionForKey("New Thread")
}
})
])
),
withKey: "New Thread"
)
Upvotes: 6
Reputation: 1134
You can use SKAction's
You would want to create a block. Inside just check for the variable and then apply your logic accordingly, once the condition is no longer true you can remove it from the object.
If you want to modify the variable add __block in front of its declaration.
SKAction *logic = [SKAction runBlock:^{
if (myvar){
NSLog(@"hello world!");}
else
{
//remove actions
}} queue : dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
SKAction *delay = [SKAction waitForDuration: 0.5];
SKAction *mySequence = [SKAction sequence:@[logic,delay,nil]];
[myNode runAction:mySequence];
easy as apple pie my boy
Upvotes: 2