我需要使用这种方法:1)查找并删除匹配项 2)填充空单元格 3)在未找到匹配项时执行 1 和 2 4)仅在未找到匹配项时 - 查找重新洗牌方法
我是怎么做到的:
[self launchAsyncCheckAndFill:^{
[self isReshuffleNeeded];
}];
-(void)launchAsyncCheckAndFill:(void(^)(void))completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performSelectorOnMainThread:@selector(check) withObject:nil waitUntilDone:true];
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler();
});
});
}
- (void) check {
[self performSelector:@selector(fillEmptyCells) withObject:nil afterDelay:0.4];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
if ([self checkMapForMatchesWithFindMatchesMode:NO isCopyVector:NO]) {
[self check];
}
});
}
首先我要删除匹配项,然后我会使用这个方法:首先我会使用 launchAsyncCheckAndFill 方法,在回调时必须启动 reshuffle 方法。launchAsyncCheckAndFill 必须仅在 check 方法完成时发送回调。检查方法在循环中完成,此方法必须始终填充空单元,然后检查匹配和未找到匹配,然后必须在 launchAsyncCheckAndFill 上工作回调。
但是现在的行为,在循环中总是删除匹配,但是重新洗牌的方法在第一次删除和填充后起作用。顺便说一句,时间 0.4 , 0.5 用于动画,这次数字移动并改变位置。不幸的是,在 cocos 中我只能使用
[elem.sprite runAction:[CCMoveTo actionWithDuration:0.4f position:elemPos1]];
仅在延迟后放置位置。
更新:
[self launchAsyncCheckAndFills];
[self isReshuffleNeeded];
-(void)launchAsyncCheckAndFills {
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(group, queue, ^{
[self check];
});
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(group);
}
- (void) check {
[self performSelector:@selector(fillEmptyCells) withObject:nil afterDelay:0.4];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
if ([self checkMapForMatchesWithFindMatchesMode:NO isCopyVector:NO]) {
[self check];
}
});
}