仅在其他之后运行方法

时间:2013-10-18 10:20:42

标签: objective-c multithreading cocos2d-iphone callback block

我需要制作这个方法: 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方法,回调时必须启动重新洗牌方法。 只有在检查方法完成后,launchAsyncCheckAndFill才必须发送回调。 Check方法是在循环中完成的,这个方法必须总是fillEmptyCells,然后检查匹配,而找不到匹配项,只有那时必须在launchAsyncCheckAndFill上进行回调。

但现在行为,循环总是删除匹配,但重新洗牌后的方法首先删除和填充后工作。 btw时间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];
        }
    });
}

0 个答案:

没有答案