Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Cocos2D使用线程更新UI_Ios_Multithreading_Cocos2d Iphone - Fatal编程技术网

Ios Cocos2D使用线程更新UI

Ios Cocos2D使用线程更新UI,ios,multithreading,cocos2d-iphone,Ios,Multithreading,Cocos2d Iphone,我有一个带有“加载条”的“加载屏幕”。这个加载屏幕从我的服务器获取图像并将它们加载到ccsprite,所以我将在其他屏幕中使用它们。当加载屏幕下载图像并创建ccsprite时,我希望加载栏更新它的UI。LoadingBar是一个CCNode子类 我知道我应该使用线程来更新UI,但问题是(据我所知)Cocos2D节点不是线程安全的,因此当我使用下面的代码时,它确实会更新UI,但随后精灵不会加载: dispatch_async(dispatch_get_global_queue(DISPATCH_Q

我有一个带有“加载条”的“加载屏幕”。这个加载屏幕从我的服务器获取图像并将它们加载到ccsprite,所以我将在其他屏幕中使用它们。当加载屏幕下载图像并创建ccsprite时,我希望加载栏更新它的UI。LoadingBar是一个CCNode子类

我知道我应该使用线程来更新UI,但问题是(据我所知)Cocos2D节点不是线程安全的,因此当我使用下面的代码时,它确实会更新UI,但随后精灵不会加载:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions), ^{
    // go do something asynchronous...
    dispatch_async(dispatch_get_main_queue(), ^{
        // update UI
    });
});
我尝试过很多事情,但我解决不了这个问题。请帮忙:)

以下是使我的加载栏更新的代码:

-(void)makeStep{
int actualPersentage = 100 * (numSteps - numStepsToGo + 1) / numSteps;
int objectAtIndex = MAX(0, numSteps - numStepsToGo);
persentageLabel.string = [NSString stringWithFormat:@"%i%% %@", actualPersentage, [steps objectAtIndex:objectAtIndex]];
[frontground setTextureRect:CGRectMake(0, 0, frontground.textureRect.size.width + (200/numSteps), 40)];
frontground.position = ccp(initialPosition + (frontground.contentSize.width/2) - (background.contentSize.width/2), frontground.position.y);
numStepsToGo --;

if(numStepsToGo == 0){
    [frontground setTextureRect:CGRectMake(0, 0, 200, 40)];
    persentageLabel.string = [NSString stringWithFormat:@"All loaded correctly!"];
}
}
基本上,我有一个背景灰色矩形作为背景,一个绿色矩形(frontground)在每次调用makeStep时“填充”背景

下面是我解析JSON的代码示例,我的加载栏应该在这里更新:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, kNilOptions), ^{
...
CCSprite *ssButtonStart = [self getSpriteFromUrl:[startScreenData objectForKey:@"SSimg_button_start"]];
CCSprite *ssButtonExit = [self getSpriteFromUrl:[startScreenData objectForKey:@"SSimg_button_exit"]];
CCSprite *ssBackground = [self getSpriteFromUrl:[startScreenData objectForKey:@"SSimg_background"]];
self.gameProperties.slBackground = ssBackground;
self.gameProperties.slButtonExit = ssButtonExit;
self.gameProperties.slButtonStart = ssButtonStart;
NSLog(@"Start Screen Loading done!");
dispatch_async(dispatch_get_main_queue(), ^{
    [self updateStep];
});
...
dispatch_async(dispatch_get_main_queue(), ^{
    [self updateStep];
});
//Etc.
获取图像并转换为CCSprite的代码:

-(CCSprite*)getSpriteFromUrl:(NSString*)stringUrl{
__block NSData *imageData;
__block CCSprite *sprite;
    imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:stringUrl]];
        //NSLog(@"string imageData: %@", imageData);
        UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
    dispatch_async(dispatch_get_main_queue(), ^{
        CCTexture2D *texture = [[CCTexture2D alloc] initWithCGImage:imView.image.CGImage resolutionType:kCCResolutionUnknown];
        if(texture != nil){
            sprite = [[CCSprite alloc] init];
            sprite = [[CCSprite alloc] initWithTexture:texture];
        }else{
            [self showError:@"Some textures didn't load correctly. Please try again!"];
        }
        // update UI
    });
return sprite;
}
我使用的是iOS 7和Cocos2D 2.0


您应该更新主线程上的条。而是将图像下载移动到后台线程。NSData或NSURLRequest甚至有异步方法来执行此操作。然后,在下载完图像后,作为最后一步,在主线程上创建ccsprite。无论如何,必须在主线程上创建精灵

此答案来自LearnCos2D(请参见相关代码),另外:


解决了!在getSpriteFromUrl中获取每个精灵后,我都睡着了。代码是[NSThread sleepForTimeInterval:0.2];。希望它能帮助其他人


从我这里,解决了我的问题。谢谢

您应该更新主线程上的条。而是将图像下载移动到后台线程。NSData或NSURLRequest甚至有异步方法来执行此操作。然后,在下载完图像后,作为最后一步,在主线程上创建ccsprite。精灵必须在主线程上创建。谢谢你的回答。我试过你的建议,但没有成功。我用更改的代码和获取图像的方法更新问题。你能告诉我哪里错了吗?解决了!在
getSpriteFromUrl
中获取每个精灵后,我都睡着了。代码是
[NSThread sleepForTimeInterval:0.2]。希望它能帮助其他人!