Cocos2d iphone COCOS2d CCLabelTTF未使用玩家分数更新-使用CCLayer类

Cocos2d iphone COCOS2d CCLabelTTF未使用玩家分数更新-使用CCLayer类,cocos2d-iphone,Cocos2d Iphone,我将非常感谢您提供的任何建议,因为我对我所面临的问题越来越感到沮丧——我也理解我所面临的问题是由于我缺乏知识/理解 为了进一步扩展我的知识和拓展我自己,我选择创建一个PlayerStats类来处理球员的得分——以及时间、健康等 我的GameLevelLayer和PlayerStats类实现如下: #import "GameLevelLayer.h" #import "Player.h" #import "PlayerStats.h" @interface GameLevelLayer() {

我将非常感谢您提供的任何建议,因为我对我所面临的问题越来越感到沮丧——我也理解我所面临的问题是由于我缺乏知识/理解

为了进一步扩展我的知识和拓展我自己,我选择创建一个PlayerStats类来处理球员的得分——以及时间、健康等

我的GameLevelLayer和PlayerStats类实现如下:

#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];
    ...........
   }
-(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如下所示:

#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];
    ...........
   }
-(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]];


}
PlayerStats.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];
    ...........
   }
-(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,但标签本身不更新

任何建议都将不胜感激,因为我非常清楚我误解了这个问题。 我认为问题在于我正在更新的图层,而不是我认为的那个图层


提前感谢。

我已经在PlayerStats类标题中将CCLabelTTF*分数声明为实例变量。一个晚上远离代码有多大的不同。

最后我设法解决了这个问题,将字符串设置为空字符串,然后将其重置为您的字符串

[label setString:@""];
[label setString:yourString]; 

您的代码看起来很好,我所做的与您所做的几乎相同。标签实际显示什么?没有短信?还是仅仅“分数:0”?尝试单步进入cocos2d代码,看看在
setString:
标签中显示的分数是0。。例如,当日志显示“标签上的分数是多少:5”时,我不知所措。我想仔细检查一下,您是否无意中为收集的
发送了错误的值。调用
[score setString:…
后,您应该添加一个CCLog来打印值,并将打印
收集的
的值直接添加到CCLog中。这确实是我的愚蠢。我已经声明CCLabelTTF*score是PlayerStats类的一个实例变量。谢谢您的帮助。