0

我在一个场景中设置了两层:

场景头文件:

@interface GameScene1 : CCScene {

GameLayer *gameLayer;

HUDLayer *hudLayer;

}

场景主文件:

-(id)init {

self = [super init];

if (self != nil) {

gameLayer = [GameLayer node];

[self addChild:gameLayer];

hudLayer = [HUDLayer node];

[self addChild:hudLayer];

}

return self;

}

HUD 层标头:

@interface HUDLayer : CCNode {

CCSprite *background;

CGSize screenSize;



}
-(void)updateMonstersSlayed:(NSString*)value;

HUD层主要:

@implementation HudLayer
-(id)init
{
    self = [super init];

    if (self)
    {
        CGSize viewSize = [[CCDirector sharedDirector] viewSize];


        monstersSlayed = [CCLabelTTF labelWithString:@"Monsters Killed: 0" fontName:@"Arial" fontSize:15];
        monstersSlayed.position = ccp(viewSize.width * 0.85, viewSize.height * 0.1 );
        [self addChild:monstersSlayed];


    }
    return self;
}
-(void)updateMonstersSlayed:(NSString*)value
{
    monstersSlayed.string = value;
}

游戏层主

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair collisionPlayer:(CCNode *)user collisionMonster:(CCNode *)monster
{
    if (holdingWeapon)
    {

        HudLayer *myHud = [[HudLayer alloc] init];
        [myHud updateMonstersSlayed:@"Monsters Killed: 1"];

    }
}

只需尝试将其设置为我可以设置游戏层中的文本以显示在 Hud 层中的标签中。

我将如何在 Cocos2d 3 中实现这一点?

4

3 回答 3

0

你可以使用方法。在 HUD 图层类中创建一个方法。

-(void) giveMeMyText:(NSString*)someText
{
   do something with my someText
}

不要忘记让 HUD.h 中的方法可见 -(void) giveMeMyText; 然后在 GameScene1 #import "HUDlayer.h" 中导入 HUD 图层类并使用它。

HUDLayer* myHud = [[HUDLayer alloc] init];
[myHud giveMeMyText:@"say hi!"];
于 2014-06-13T08:46:42.150 回答
0

您可以使用委托,因此您的场景将实现 GameLayerProtocol 和 GameLayer 的委托。这样一来,GameLayer 所做的任何更改都会通知场景并在 HudLayer 上采取适当的行动。

例如:

// GameLayer class

@protocol GameLayerProtocol <NSObject>

- (void)someThingHappenedInGameLayer;

@end

@interface GameLayer : CCNode

@property (nonatomic, weak) id<GameLayerProtocol> delegate;

@end

@implementation GameLayer

- (void)someActionInGameLayer
{
    [self.delegate someThingHappenedInGameLayer];
}

@end

// 场景类

@interface IntroScene : CCScene <GameLayerProtocol>

@end

@implementation IntroScene

// Implement protocol methods
- (void)someThingHappenedInGameLayer
{
   //Do something with your HUDLayer here
}

@end
于 2014-06-13T16:03:15.617 回答
0

有很多方法可以做到这一点。但为了简单起见,最简单的方法是通过通知。例如在 hud 中添加:

@implementation HudLayer

- (void)onEnter
{
    [super onEnter];

    NSNotificationCenter* notiCenter = [NSNotificationCenter defaultCenter];

    [notiCenter addObserver:self
                   selector:@selector(onUpdateMonsterText:)
                       name:@"HudLayerUpdateMonsterTextNotification"
                     object:nil];
}


- (void)onExit
{
    [super onExit];

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)onUpdateMonsterText:(NSNotification *)notification
{
    NSDictionary* userInfo = notification.userInfo;

    if (userInfo)
    {
        NSString* text = userInfo[@"text"];

        if (text)
        {
            [self updateMonstersSlayed:text];
        }
        else
        {
            CCLOG(@"Monster hud text param is invalid!.");
        }
    }
    else
    {
        CCLOG(@"Monster hud user info invalid!");
    }
}

@end

然后,在您想要更新文本的应用程序中的任何地方,您都可以发布通知。使用您的物理碰撞开始示例:

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair
    *emphasized text*collisionPlayer:(CCNode *)user collisionMonster:(CCNode *)monster
{
    if (holdingWeapon)
    {
        NSDictionary* userInfo = @{@"text" : @"Monsters Killed: 1"};
        NSString* notiName = @"HudLayerUpdateMonsterTextNotification";

        [[NSNotificationCenter defaultCenter] postNotificationName:notiName
            object:self userInfo:userInfo];

    }
}

希望这可以帮助。

于 2014-06-13T16:59:50.687 回答