Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating my HUD class through a SceneManager class
    text
    copied!<p>I'm doing an iPhone app (game) for University. I'm accessing the HUD class so I can update a label when progress is made through the game. Have I got a bad architecture to do this? Or how can I do it?</p> <h2>HUD Class</h2> <pre><code>@implementation HUD @synthesize statBar; @synthesize levelText; @synthesize label; @synthesize level; - (id) init { if((self = [super init])) { CGSize winSize = [[CCDirector sharedDirector] winSize]; statBar = [CCSprite spriteWithFile:@"statbar.png"]; statBar.position = ccp(winSize.width/2, winSize.height-10); levelText = [self createLevelText:10]; // testing purposes levelText.position = ccp(((winSize.width)-(winSize.width)+20), winSize.height-10); levelText.color = ccBLACK; [self addChild:statBar]; [self addChild:levelText]; } return self; // shouldnt reach this } - (CCLabelTTF *) createLevelText:(int)levelNo { level = [NSString stringWithFormat:@"LVL: %d", levelNo]; label = [CCLabelTTF labelWithString:(NSString*) level fontName:@"CharlemagneStd-Bold.otf" fontSize:10]; //[label setString:(NSString *) level]; return label; } @end </code></pre> <h2>ScreenManager Class (Relevant Code)</h2> <pre><code>CCLayer *hudLayer; int hudTag; +(void) goPlay { hudLayer = [HUD node]; CCLayer *playLayer = [PlayLayer node]; hudTag = playLayer.tag; [SceneManager goWithHud: playLayer: hudLayer: hudTag]; } + (CCScene *) goWithHud:(CCLayer *) playLayer:(CCLayer *) hudLayer:(int)hudTag { CCDirector *director = [CCDirector sharedDirector]; CCScene *newScene = [CCScene node]; [newScene addChild: playLayer]; [newScene addChild: hudLayer z:0 tag:hudTag]; if([director runningScene]) [director replaceScene:newScene]; else [director runWithScene:newScene]; return newScene; } +(void)setScoreString:(int *)levelNo:(CCLayer *) hudLayer { NSString *string = [NSString stringWithFormat:@"LVL: %d", levelNo]; hudLayer.levelText = string; } -(void)updateScore { HUD *obj = (HUD *)[self getChildByTag:hudTag]; [obj setScoreString:@"100"]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload