有誰知道爲什麼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