2013-04-22 1 views
0

HUD 레이어를 표시 할 수 있지만 업데이트 할 수 없습니다. 나는 Ray의 튜토리얼에서 작동 시키지만, 어떤 이유로 그것을 내 어플리케이션에서 작동시킬 수는 없다. 새로운 Cocos2d 프로젝트를 만들었으므로 문제를 격리하고 시도 할 수 있으며 동일한 문제가 발생했을 수 있습니다. 아마도 여러분이 도와 줄 수 있습니다. (나는 오류를 받고없고 들여 쓰기를 해결하기 위해 노력하고있어 I에 유래 위해 할 수 최고의 .. 등)게임 레이어에서 HUD 레이어 메서드를 호출하는 데 문제가 있음 Cocos2d

문제점 : scoreLabel를 업데이트 할 수 없습니다

코드 :

GameHUD.h

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface GameHUD : CCLayer { 

    CCLabelTTF *scoreLabel; 

} 

- (void) updateScore:(int)score; 

@end 

GameHUD.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 HUD를 아직 생성되지 않습니다 때, 즉 [HelloWorldLayer node]를 호출 할 때

#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]; 
} 
+0

IDE 자체에 대한 질문에만 [tag : xcode] 태그를 사용하십시오. 감사! – Undo

+0

좋아, 알아 냈어. init 메소드에서 hud를 업데이트 할 수 없습니다. 나중에 수행해야합니다. 업데이트 메서드를 호출하면 ccTouchBegan에서 제대로 작동합니다. hud 객체가 아직 생성되지 않았다고 생각합니까? 그렇다면 잘못된 액세스 오류가 표시되지 않는 이유가 궁금합니다. 이상한. – mike

+0

릴리스 된 객체에 액세스 할 때만이 오류가 발생합니다. 오브젝트를 생성하기 전에 objective-c의 모든 오브젝트는 C++에서와 같이 'NULL'이 아닌 'nil' 값을 갖습니다. 가장 큰 차이점은 오류를 발생시키지 않고 모든 메시지를'nil'에 보낼 수 있다는 것입니다. – Morion

답변

1

init가 호출

#import "cocos2d.h" 
#import "GameHUD.h" 

@interface HelloWorldLayer : CCLayer 
{ 
    GameHUD *_hud; 
} 

@property (nonatomic,retain) GameHUD *hud; 

+(CCScene *) scene; 

@end 

HelloWorldLayer.m, _hudnil입니다. nil 개체로 메시지를 보내는 것은 무효 작업이며 'NULL'개체에서 함수를 호출하는 경우 그대로 충돌하지 않습니다.

+0

아, 그건 의미가 있습니다. 명확히 해 주셔서 감사합니다! – mike

관련 문제