在场景中的两个层之间进行通信Cocos2d 3

时间:2014-06-13 05:35:18

标签: ios objective-c cocos2d-iphone cocos2d-iphone-3

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

场景的头文件:

@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中完成此任务?

3 个答案:

答案 0 :(得分:0)

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

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

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

HUDLayer* myHud = [[HUDLayer alloc] init];
[myHud giveMeMyText:@"say hi!"];

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

答案 2 :(得分:0)

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

@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];

    }
}

希望这有帮助。