Обновление ярлыка на HUD в iPhone Cocos2d? - PullRequest
0 голосов
/ 01 февраля 2012

Я создаю слой HUD поверх моего игрового слоя, добавляя метку для отображения Score в этом 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хотя NSlog возвращает новое значение, что я, возможно, делаю неправильно ??

1 Ответ

1 голос
/ 01 февраля 2012

В вашем методе updateScore вы создаете новый объект HUDLayer при каждом запуске этого метода.

Вместо этого вам нужно ссылаться на HUDLayer, который вы добавили в сцену.Я предлагаю вам дать вашему HUDLayer тег:

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

Затем в вашем методе updateScore получите доступ к этому HudLayer по его тегу и обновите ваш счет:

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

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...