我将非常感谢您提供的任何建议,因为我对自己遇到的问题越来越感到沮丧 - 我也很感激我遇到的问题是由于我缺乏知识/理解。
为了进一步扩展我的知识和扩展自己,我选择创建一个 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 显示标签应显示 Score : 5 但标签本身不会更新。
任何建议将不胜感激,因为我非常清楚我误解了这个问题。我认为问题在于我正在更新的图层不是我认为的那个......
提前致谢。