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:内ののみHudLayerが呼び出されることです (つまり、デリゲートのメソッドが呼び出されることはありません)。

PS スペースを節約するために、両方の .h ファイルを省略しました。@protocolしかし、 で使用されHudLayer、同じプロトコル名が <> 内に置かれているため、それらが正しいことを保証します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 に答える