如何在CCSprite或CCNode,Cocos2d中更新gamehud

时间:2012-12-20 19:45:06

标签: objective-c ios xcode cocos2d-iphone ccsprite

我有Gamehud我要显示对象的名称。主场景中有很多对象/精灵,我想要做的是在Gamehud上显示所选(触摸)对象的名称。

问题是如果我在Gamehud类中分配CCsprite它会创建新实例并且不会更新当前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];
}

2 个答案:

答案 0 :(得分: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的最佳方式(请问google)。不要调用..alloc] init]只使用[GameHUD gamehud];

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

祝你好运!

答案 1 :(得分: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];

}
相关问题