ios - HudLayer 和 GameLayer 之间的委托(delegate)无法工作

标签 ios cocos2d-iphone

我创建了一个 HudLayer 类,它定义了按下按钮时发生的协议(protocol)。

-(id) init
{
    if( (self=[super initWithColor:ccc4(255, 255, 0, 128)])) {
        self.position=ccp(0,0);
        self.contentSize=CGSizeMake(480, 40);

        scoreLabel=[CCLabelTTF labelWithString:@"0" fontName:@"Marker Felt" fontSize:24];
        scoreLabel.position=ccp(62,20);
        scoreLabel.anchorPoint=ccp(0,0.5);
        [self addChild:scoreLabel];

        CCLabelTTF *textLabel=[CCLabelTTF labelWithString:@"Score:" fontName:@"Marker Felt" fontSize:24];
        textLabel.position=ccp(5,20);
        textLabel.anchorPoint=ccp(0,0.5);
        [self addChild:textLabel];

        CCMenuItemImage * item = [CCMenuItemImage itemWithNormalImage:@"fire.png" selectedImage:@"fire.png" target:self selector:@selector(projectileButtonTapped:)];

        CCMenu *menu = [CCMenu menuWithItems:item, nil];
        menu.position = ccp(150, 20);
        [self addChild:menu];
    }
    return self;
}

- (void)projectileButtonTapped:(id)sender
{
    NSLog(@"projectileButtonTapped HudLayer");
    [self.delegate respondsToSelector:@selector(projectileButtonTapped:)];
}

然后在我的 HelloWorldLayer 类中,我实现该协议(protocol)并将自己设置为委托(delegate)。

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    //add another layer to the scene
//    HudLayer *anotherLayer = [HudLayer node];
//    anotherLayer.delegate = self;
//  [scene addChild: anotherLayer];
//    layer.hud=anotherLayer;

    // return the scene
    return scene;
}

-(id) init
{
    if( (self=[super init]) ) {

        _tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"GridWars@medium.tmx"];
        _background = [_tileMap layerNamed:@"Background"];
        _foreground = [_tileMap layerNamed:@"Foreground"];

        _meta = [_tileMap layerNamed:@"Meta"];
        _meta.visible = NO;


        CCTMXObjectGroup *objectGroup = [_tileMap objectGroupNamed:@"Objects"];
        NSAssert(objectGroup != nil, @"tile map has no objects object layer");

        NSDictionary *spawnPoint = [objectGroup objectNamed:@"SpawnPoint"];
        int x = [spawnPoint[@"x"] integerValue];
        int y = [spawnPoint[@"y"] integerValue];

        _wizardHero = [[WizardHero alloc]init];
        _redEnemy = [[RedEnemy alloc]init];
        _wizardHero.position = ccp(x,y);

        [self addChild:_wizardHero];
        [self setViewPointCenter:_wizardHero.position];

        [self addChild:_tileMap z:-1];

        self.touchEnabled = YES;

        self.enemies = [[NSMutableArray alloc] init];
        self.projectiles = [[NSMutableArray alloc] init];
        [self schedule:@selector(testCollisions:)];

        for (spawnPoint in [objectGroup objects]) {
            if ([[spawnPoint valueForKey:@"Enemy"] intValue] == 1){
                x = [[spawnPoint valueForKey:@"x"] intValue];
                y = [[spawnPoint valueForKey:@"y"] intValue];
                [self addEnemyAtX:x y:y];
            }
        }

        _hud = [HudLayer node];
        _hud.delegate = self;
        [self addChild:_hud];
    }
    return self;
}

- (void)projectileButtonTapped:(id)sender
{
    // Find where the touch is
    //    CGPoint touchLocation = [touch locationInView: [touch view]];
    //    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    //    touchLocation = [self convertToNodeSpace:touchLocation];

    if (self.wizardHero.selectedTargets.count > 0) {


        // Create a projectile and put it at the player's location
        CCSprite *projectile = [CCSprite spriteWithFile:@"Projectile.png"];
        projectile.position = _wizardHero.position;
        [self addChild:projectile];

        // Determine where we wish to shoot the projectile to
        int realX;

        // Are we shooting to the left or right?
        CGPoint diff = ccpSub(self.redEnemy.position, self.wizardHero.position);
        if (diff.x > 0)
        {
            realX = (_tileMap.mapSize.width * _tileMap.tileSize.width) +
            (projectile.contentSize.width/2);
        } else {
            realX = -(_tileMap.mapSize.width * _tileMap.tileSize.width) -
            (projectile.contentSize.width/2);
        }
        float ratio = (float) diff.y / (float) diff.x;
        int realY = ((realX - projectile.position.x) * ratio) + projectile.position.y;
        CGPoint realDest = ccp(realX, realY);

        // Determine the length of how far we're shooting
        int offRealX = realX - projectile.position.x;
        int offRealY = realY - projectile.position.y;
        float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
        float velocity = 480/1; // 480pixels/1sec
        float realMoveDuration = length/velocity;

        // Move projectile to actual endpoint
        id actionMoveDone = [CCCallFuncN actionWithTarget:self
                                                 selector:@selector(projectileMoveFinished:)];
        [projectile runAction:
         [CCSequence actionOne:
          [CCMoveTo actionWithDuration: realMoveDuration
                              position: realDest]
                           two: actionMoveDone]];

        [self.projectiles addObject:projectile];
    }
}

问题是只有 HudLayer 中的 projectileButtonTapped: 被调用(即,委托(delegate)的方法永远不会被调用)。

附言 为了节省空间,我省略了两个 .h 文件。但我向您保证它们是正确的,HudLayer 中使用了 @protocol,并且 HelloWorldLayer 中的 <> 中使用了相同的协议(protocol)名称。

最佳答案

我认为:

- (void)projectileButtonTapped:(id)sender
{
    NSLog(@"projectileButtonTapped HudLayer");
    if ([self.delegate respondsToSelector:@selector(projectileButtonTapped:)]) {
       [self.delegate performSelector:@selector(projectileButtonTapped:) withObject:sender];
    }
}

关于ios - HudLayer 和 GameLayer 之间的委托(delegate)无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15551349/

相关文章:

ios - 如何在swift 3中每天在特定时间触发本地通知

ios - iOS 13 和 Cordova 应用程序不滚动的问题

ios - 当 Cocos2D 2.0 支持 NPOT 纹理时,为什么要使用纹理图集?

iphone - 如何旋转和调整 Sprite 像库中的照片大小

ios - 在 MIKMIDI 中使用 soundfonts

ios - tableView reloadData 失去文本字段焦点

iphone - 从 iphone 的核心数据中删除对象时应用程序崩溃

objective-c - cocos2d 在不同目标上按顺序运行序列

cocos2d-iphone - Cocos2d - 动画结束后更改动画

ios - Cocos2d。为什么异步加载纹理会保留它?