我會非常感謝您提供的任何建議,因爲我越來越沮喪於我遇到的問題 - 我也很感謝我遇到的問題是由於因爲我缺乏知識/理解。COCOS2d CCLabelTTF不會隨球員比分更新 - 使用CCLayer職業
在試圖進一步深造和舒展自己,我選擇創建一個PlayerStats類來處理球員的得分 - 在時間,健康等
我已在GameLevelLayer和PlayerStats類實現如下:
GameLevelLayer.m如下:
#import "GameLevelLayer.h"
#import "Player.h"
#import "PlayerStats.h"
@interface GameLevelLayer() {
CCTMXTiledMap *map;
Player *player;
PlayerStats *playerStats;
}
@end
@implementation GameLevelLayer
@synthesize grabber = _grabber;
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
GameLevelLayer *layer = [GameLevelLayer node];
PlayerStats *hudLayer = [PlayerStats node];
[scene addChild: layer];
[scene addChild: hudLayer];
return scene;
}
-(id) init {
if((self=[super init])) {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
playerStats = [[PlayerStats alloc]init];
...........
}
PlayerStats.m如下:
-(id) init
{
if ((self = [super init])) {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
score = [CCLabelTTF labelWithString:@"Score : 0" dimensions:CGSizeMake(100,20) hAlignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:(18.0)];
int margin = 5;
score.position = ccp(screenSize.width - (score.contentSize.width/2) - margin, score.contentSize.height/2 + margin);
[self addChild:score z:99];
}
return self;
}
-(void)numberOfItemsCollected:(int)collected {
NSString *str = [score string];
CCLOG(@"What does the label say %@", str);
// This is actually displaying the correct string of what the score should be ..
[score setString:[NSString stringWithFormat:@"Score : %d", collected]];
}
當(從GameLevelLayer.m)我發起
[playerStats numberOfItemsCollected:5];
的CCLOG表明,標籤上應顯示分數:5,但標籤本身不更新。
任何意見將不勝感激,因爲我非常清楚,我誤解了這個問題。 我認爲這個問題是關於我正在更新的圖層,而不是我相信它是一個....
在此先感謝。
您的代碼看起來不錯,和我做一些事情幾乎相同,你在做什麼。標籤實際顯示的是什麼?沒有文字?或者只是「分數:0」?嘗試進入cocos2d代碼,看看'setString裏面發生了什麼:' – MechEthan 2012-08-07 17:53:52
標籤顯示Score:0 ..而Log顯示「標籤說的是什麼Score:5」..例如.. – TheTurnip 2012-08-07 19:32:25
我很茫然。我想仔細檢查一下,你是不是意外地發送了「收集」的錯誤值。您應該在調用'[score setString:...'後添加一個CCLog以打印該值,並將收集的值直接添加到CCLog中。 – MechEthan 2012-08-07 19:41:34