在普通的obj-c或cocos2d中,有没有一种方法可以在if-else块中进行延迟?喜欢
if ([self isValidTileCoord:cTileCoord] && ![self isWallAtTileCoord:cTileCoord])
{
[self addChild:circle0];
//wait two seconds
//perform another task
}只是在两个任务之间等待的一个简单的延迟,或者拖延一个动作。有什么简单的方法可以做到这一点吗?
发布于 2013-05-19 07:38:53
您可以使用performSelector: withObject: afterDelay:方法延迟任务:
if ([self isValidTileCoord:cTileCoord] && ![self isWallAtTileCoord:cTileCoord])
{
[self addChild:circle0];
//wait two seconds
[self performSelector:@selector(continueTask) withObject:nil afterDelay:2.0];
}在选择器方法中:
-(void)continueTask
{
//perform another task
}发布于 2013-05-19 07:49:59
有很多方法可以做到这一点。我会使用GCD
if ([self isValidTileCoord:cTileCoord] && ![self isWallAtTileCoord:cTileCoord])
{
[self addChild:circle0];
//wait two seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
//perform another task;
});
}发布于 2013-05-19 14:33:20
在cocos2d中,您还可以在节点上运行操作,如
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:2.0],[CCCallFunc actionWithTarget:self selector:@selector(anothertaks)],nil]];执行选择器也会工作,但问题是当应用程序转到后台时,cocos2D的所有调度器都会暂停,但在另一边执行选择器仍然会计算时间,所以有时它会产生任务,动画等同步问题。
执行选择器:
[self performSelector:@selector(continueTask) withObject:nil afterDelay:2.0];https://stackoverflow.com/questions/16630009
复制相似问题