我可以让 HUD 层出现,但我似乎无法更新它。我在 Ray 的教程中得到了它,但由于某种原因,我无法在我自己的应用程序中得到它。我做了一个新的 Cocos2d 项目,这样我就可以尝试隔离问题,我也遇到了同样的问题……也许你们可以帮忙。(我没有收到任何错误,并尝试尽我所能为 StackOverflow 修复缩进 ..)
问题:我无法更新 scoreLabel
代码:
游戏HUD.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface GameHUD : CCLayer {
CCLabelTTF *scoreLabel;
}
- (void) updateScore:(int)score;
@end
游戏HUD.m
#import "GameHUD.h"
@implementation GameHUD
- (id) init {
if (self = [super init]) {
scoreLabel = [CCLabelTTF labelWithString:@"00000" dimensions:CGSizeMake(240,100) hAlignment:kCCTextAlignmentRight fontName:@"Arial" fontSize:32.0f];
scoreLabel.anchorPoint = ccp(0,0);
scoreLabel.position = ccp(200,0);
scoreLabel.color = ccc3(255, 200, 100);
[self addChild:scoreLabel];
}
return self;
}
- (void)updateScore:(int)score {
scoreLabel.string = [NSString stringWithFormat:@"%i",score];
}
@end
HelloWorldLayer.h
#import "cocos2d.h"
#import "GameHUD.h"
@interface HelloWorldLayer : CCLayer
{
GameHUD *_hud;
}
@property (nonatomic,retain) GameHUD *hud;
+(CCScene *) scene;
@end
HelloWorldLayer.m
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
#pragma mark - HelloWorldLayer
@implementation HelloWorldLayer
@synthesize hud = _hud;
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
GameHUD *hud = [GameHUD node];
[scene addChild:hud];
layer.hud = hud;
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Layer A" fontName:@"Marker Felt" fontSize:64];
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
// Try to update the scoreLabel in the HUD (NOT WORKING)
[_hud updateScore:74021];
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
[super dealloc];
}