我无法弄清楚这一点,我正在学习。我有一系列卡片,我想移动到一个固定的位置。这样可以正常工作,除非它们一下子都移动了。我想要一种方法来检查第一个是否在第二个完成之前完成了移动。到目前为止,我的代码是:
-(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);
}
所以,理想情况下,我想知道doMarkedMovement什么时候完成了它的工作,然后再次运行循环。
感谢您提供的任何帮助。
答案 0 :(得分:1)
让doMarkedMovement
在方法完成时调用方法 - 让我们在列表中的下一个元素上调用procNextMovement
- 和procNextMovement
调用doMarkedMovement
。
或者,如果这样可行则更简单 - 更改afterDelay
参数。因此,如果每个doMarkedMovement
需要0.2秒才能完成,那么你的afterDelay
将是:0.5,0.7,0.9,1.1等......这不会像回调方法那样精确
这样的事情:
- (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];
}
然后在selectedUnit
的函数中:
- (void) doMarkedMovement:(id td) {
//regular code here
//callback once movement is done, where `caller` is whatever the object above was
[caller procNextTurn];
}