2012-02-01 13 views
0

私はそれがさえのNSLogかかわらず、HUDに追加されたら、HUDには以下のコードの助けを借りてCocos2d iPhoneのHUDでラベルを更新していますか?

// playGame Class 

+(CCScene *) scene 
{ 
// 'scene' is an autorelease object. 
CCScene *scene = [CCScene node]; 

// 'layer' is an autorelease object. 
playGameLayer *layer = [playGameLayer node]; 

// add layer as a child to scene 
[scene addChild: layer]; 

// create HUD 
    id statsHuds = [HUDlayer statsHUDWithBackgroundSprite:HUDBackground withRect:CGRectMake(160,30, 130,60)]; 

[statsHuds addLabeltoStatsHUDwithName:@"Score" andValue:@"50"]; 
//[statsHuds setStatusString:@"yewq"]; 
//[statsHuds updateScorewithValue:199]; 
// add HUD 
    [scene addChild: statsHuds]; 

// return the scene 


return scene; 
} 

- (id)init 
{ 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) 
    { 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO]; 

    [self performSelector:@selector(updateLabel) withObject:nil afterDelay:5]; 

} 
return self; 
} 

-(void)updateScore 
{ 

HUDlayer *obj = [[HUDlayer alloc]init]; 
[obj setScoreString:@"100"]; 
[obj release]; 
} 


// HUDLayer Class 

+(id)statsHUDWithBackgroundSprite:(NSString *)spriteName withRect:(CGRect)rect 
{ 
    HUDlayer *hud = [[HUDlayer alloc] init]; 

UIImage *image = [UIImage imageNamed:spriteName]; 

CCSprite *statsSprite = [CCSprite spriteWithCGImage:image.CGImage key:nil]; 
[statsSprite setPosition:ccp(rect.origin.x,rect.origin.y)]; 
[hud addChild:statsSprite]; 



return [hud autorelease]; 
} 

-(void)addLabeltoStatsHUDwithName:(NSString *)labelName andValue:(NSString *)labelValue 
{ 

[_statusLabel setString:@"no"];// [CCLabelBMFont labelWithString:@"no" fntFile:@"Arial.fnt"]; 
[_statusLabel setPosition:ccp(160,240)]; 
[self addChild:_statusLabel]; 

} 

// label added above is not updating 
- (void)setScoreString:(NSString *)string 
{ 
    _statusLabel.string = string; 

    NSLog(@"stats string after %@",_statusLabel.string); 
} 

_statuslbelが更新されていないことでスコア表示するラベルを追加し、私のゲームの層の上にHUDレイヤーを作成しています新しい値を返します 私はおそらく何か間違っていますか?

+0

_statusLabelはどこに作成されていますか? updateScoreメソッドは何をすべきでしょうか?すべての呼び出しで新しいレイヤーが作成されているようです。 – FBryant87

+0

がHUDLayerクラスのinitにあります。updateScoreはスコアを更新し、_statusLabelを更新する必要があります – hemant

+0

updateScoreメソッドは頻繁に呼び出されるはずですか?ここに含まれていない限り、updateLabelメソッドも見つかりません。 – FBryant87

答えて

1

updateScoreメソッドでは、このメソッドが呼び出されるたびに新しいHUDLayerオブジェクトを作成しています。

代わりに、シーンに追加したHUDLayerを参照する必要があります。あなたのupdateScore方法、そのタグによるアクセスHudLayerことで次に

[scene addChild: statsHuds z:0 tag:HUDTag]; 

とあなたのスコアを更新します:私はあなたのHUDLayerにタグを与えることを示唆している

-(void)updateScore 
{ 
    HUDLayer * obj = (HudLayer *)[self.parent getChildByTag:HUDTag]; 
    [obj setScoreString:@"100"]; 
} 

・ホープ、このことができます。

+0

thnx ..それは働いた – hemant

関連する問題