0

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

-(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课堂上我实现了协议并将自己设置为代表。

+(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];
    }
}

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

PS 为了节省空间,我省略了两个 .h 文件。但我向你保证它们是正确的,@protocol使用 inHudLayer和相同的协议名称放在HelloWorldLayer.

4

1 回答 1

1

我认为 :

- (void)projectileButtonTapped:(id)sender
{
    NSLog(@"projectileButtonTapped HudLayer");
    if ([self.delegate respondsToSelector:@selector(projectileButtonTapped:)]) {
       [self.delegate performSelector:@selector(projectileButtonTapped:) withObject:sender];
    }
}
于 2013-03-21T15:50:37.727 回答