有谁知道为什么bmlabel
没有更新?日志“分数”什么都没有显示?如果没有GCD,它可以正常工作,但它会阻止用户界面(例如,我想显示100到500之间的数字,例如显示101,102 ... 499,500非常快,而不是直接从“100”到“ 500" )。所以我想用另一个线程来计算它,即使我不确定这是最好的方法。这里是我的代码:为什么我的GCD方法没有响应?
//in .h, i imported :
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//in .m
@implementation ScoreLayer
-(void)updateScore:(int)num{
CCLOG(@"hello");
int newScore = score+num;
//dispatch_queue_t queue = dispatch_queue_create("Test", 0);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
for (int i=score; i< newScore; i++){
score++;
CCLOG(@"score:%i", score);
NSString *str = [NSString stringWithFormat:@"%d", score];
dispatch_async(main, ^{
[bmlabel setString:str];
});
}
});
score = newScore;
}
-(id) init
{
if((self=[super init])) {
bmlabel = [CCLabelBMFont labelWithString:@"14763" fntFile:@"TestFont.fnt"];
bmlabel.anchorPoint = ccp(0, 0.5f);
bmlabel.position = ccp(0,250.0f);
[self addChild:bmlabel z:200];
score = 14763;
}
return self;
}
非常感谢正在异步执行
我不使用cocos2d,但会自动设置标签使其刷新,或者您是否需要发送另一条消息以使屏幕上的版本自行绘制? – gaige
会做'CCLOG(@“score:%i”,score);'打印是否正确? – Johnnywho
谢谢,我感谢你的帮助! @gaige对不起,我不确定要理解你的问题,我希望文本被更新为++,直到我到达newScore编号(setString在“main”队列中)。 – Paul