2012-12-20 46 views
1

我有一個Gamehud我想顯示對象的名稱。主場景中有很多對象/精靈,我試圖做的是在Gamehud上顯示選定的(接觸)對象的名稱。如何在CCSprite或CCNode中更新gamehud,Cocos2d

問題是如果我在CCsprite分配Gamehud類它創建新的實例,並不會更新當前Gamehud。如果我使用類似GameHUD *gamehud= (GameHUD *)[self.parent getChildByTag:99];的東西,則不會發生任何事情,我無法將該對象發送給GameHud類。

那麼在ccsprite or ccnode類中更新遊戲hud的正確方法是什麼?

主場景;

-(id) init 
{ 
    if ((self = [super init])) 
    {    
     gameHud = [GameHUD gamehud]; 
     [self addChild:gameHud z:2 tag:99]; 
    } 
} 

我GameHud

+(id) gamehud 
{ 
    return [[self alloc] init]; 
} 

-(id) init 
{ 
    if ((self = [super init])) 
    { 
     //bunch of labels 
    } 
} 


-(void)showName: :(Object *)obj 
{ 
    NSLog(@"Object name is %@", obj.name); 
    [_labelSpeed setString:obj.name]; 
} 

在對象類:CCSprite

-(void) onTouch 
{ 
    //obj is the object with name property that I want to use 
    GameHUD *gamehud= (GameHUD *)[self.parent getChildByTag:99]; // does not send the obj to gamehud and showName is not called 
    //GameHud *gamehud= [GameHud alloc] init]; // this displays nslog but doesnt update _label 
    [gamehud showName:obj]; 
} 
+0

也許你可以在該CCSprite對象上創建委託方法。當它被選中時它會被點燃。然後在你的主場景中獲取對象名稱並調用'[gameHud showName:objectName]' – mkral

+0

你絕對不想從對象內部設置hud。 – mkral

+0

另一種選擇是檢查哪些子視圖在mainScene中被觸摸,然後在那裏執行。 – mkral

回答

0

您可能需要創建一個單身人士或類似半單身人士的東西。只需在您的遊戲場景呼叫添加新的NSObject類名 「SingletonGameHud」 到你的應用程序

SingletonGameHud.h

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

//create singleton class to use gamehud in movingobject class 
@interface SingletonGameHud : NSObject 
{ 
    GameHUD *gamingHud; 

} 

@property(nonatomic,strong) GameHUD *gamingHud; 

+(SingletonGameHud *)sharedInstance; 


@end 

SingletonGameHud.m

#import "SingletonGameHud.h" 
#import "GameHUD.h" 

@implementation SingletonGameHud 

@synthesize gamingHud=_gamingHud; 


+ (SingletonGameHud *)sharedInstance 
{ 
    static SingletonGameHud *sharedInstance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedInstance = [[SingletonGameHud alloc] init]; 
     // Do any other initialisation stuff here 
    }); 
    return sharedInstance; 
} 

- (id)init { 
    if (self = [super init]) { 
     _gamingHud = [GameHUD hud]; 

    } 
    return self; 
} 

@end 

SingletonGameHud *sharedInstance= [SingletonGameHud sharedInstance]; 
hud = sharedInstance.gamingHud; 
[self addChild:hud z:2 tag:99]; 

您聯繫方式電話

-(void) onTouch 
{ 
    SingletonGameHud *sharedInstance= [SingletonGameHud sharedInstance]; 
    [sharedInstance.gamingHud showName:obj]; 

} 
1

首先,使用singlton否則將創建你叫+(id) gamehud新GameHub每次。我認爲這可能是您的問題:您將一個GameHUD添加到場景並調用GameHUD另一個對象的showName:。另一個問題是你的-(id)init - 你不會返回self!所以你永遠不從現在得到您的GameHUD

static GameHUD *sharedInstance = nil; 
+(id) gamehud { 
    if(!sharedInstance) { 
     sharedInstance = [[GameHUD alloc] init] 
    } 
    return sharedInstance; 
} 

-(id) init { 
    self = [super init]; 
    if (self) { 
     //bunch of labels 

     sharedInstance = self; 
    } 
    return self; //i dont see this in your code! 
} 

你可以從你想要的每一個點訪問您的HUD和你不需要用標籤來處理。小心,它不是創建Singleton的最佳方式(請求谷歌)。不要調用..alloc] init]只使用[GameHUD gamehud];

-(void) onTouch { 
    [[GameHUD gamehud] showName:obj]; 
} 

祝你好運!

+0

'return self;'只是一個複製粘貼錯誤我在我的代碼中。感謝你的回答 –