Reputation:
I can't figure this out, I'm learning as I go. I have an array of cards that I want to move to a set location. This works fine except they all appear to move at once. I want a way to check if the first has finished moving before the second one does. Heres my code so far:
-(void)AIturn {
int NuUnits = [p2Units count];
for (int q = 0; q < NuUnits; q++)
{
[self unselectUnit];
selectedUnit = [p2Units objectAtIndex:q] ;
CGPoint moveto = CGPointMake(184,556);
TileData * td = [self getTileData:[self tileCoordForPosition:moveto]];
//[selectedUnit doMarkedMovement:td ];
[selectedUnit performSelector:@selector(doMarkedMovement:) withObject:td afterDelay:0.5];
NSLog(@"%@ moved", selectedUnit);
}
So, What Ideally I want is for it to know when doMarkedMovement has finished doing its stuff and then run through the loop again.
Thanks for any help you can provide.
Upvotes: 3
Views: 472
Reputation: 229321
Have doMarkedMovement
call a method when it's done - let's call it procNextMovement
- and procNextMovement
calls doMarkedMovement
on the next element in the list.
Alternatively, if this works it would be much simpler - change the afterDelay
parameter. So if each doMarkedMovement
takes 0.2 seconds to complete, then your afterDelay
s will be: 0.5, 0.7, 0.9, 1.1, etc... This wouldn't be as precise as doing the callback method though.
Something like this:
- (void) AIturn {
upTo = 0; //has to be an instance variable
[self procNextTurn];
}
- (void) procNextTurn {
if (upTo >= NuUnits) {
//done
return;
}
[self unselectUnit];
selectedUnit = [p2Units objectAtIndex:upTo];
CGPoint moveto = CGPointMake(184, 556);
TileData * td = [self getTileData:[self tileCoordForPosition:moveto]];
upTo += 1
[selectedUnit performSelector:@selector(doMarkedMovement:) withObject:td afterDelay:0.5];
}
Then in the selectedUnit
's function:
- (void) doMarkedMovement:(id td) {
//regular code here
//callback once movement is done, where `caller` is whatever the object above was
[caller procNextTurn];
}
Upvotes: 1