有人知道为什么bmlabel
不更新吗?日志“分数”什么也没显示?没有 GCD,它可以正常工作,但它会阻止 ui(例如,我想显示 100 到 500 之间的数字,非常快地显示 101、102...499、500,而不是直接从“100”变为“ 500 英寸)。所以我想用另一个线程来计算它,即使我不确定这是最好的方法。这是我的代码:
//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;
}
非常感谢