如何在CCAction中运行方法?

时间:2011-05-14 02:27:06

标签: iphone xcode cocos2d-iphone

我目前正处于将电脑扔到墙上的边缘,因为我无法弄清楚这一点。我已经完成了大约200次Google搜索,每个链接都被点击到第6页。我找不到答案。所以这是污垢:

我希望我的敌人课程包含射击方法。够简单吧?好吧,我有动作来移动HelloWorldLayer方法中的敌人。我想找到一种方法让(某种类型的)CCAction从Enemies.m类中调用该方法。请帮忙!而@Lukman你的面向对象编程的答案没有用。谢谢!

编辑:

以下是答案所必需的HelloWorldLayer.m中的内容:

action = [CCSequence actions:
          [CCMoveBy actionWithDuration:1 position:ccpMult(ccpNormalize(ccpSub(moveToPoint, buffDude.position)), 75)],
          [CCMoveBy actionWithDuration:3 position:ccp(buffDude.position.x,buffDude.position.y)],
          nil];

CCCallFuncO *a = [CCCallFuncO actionWithTarget:buffDude selector:(@selector(shoot:)) object:buffDude];

CCSequence *seq = [CCSequence actions:action,a, nil];

CCRepeatForever *repeat = [CCRepeatForever actionWithAction:seq];

[buffDude runAction:repeat];

这就是Enemies.m中的内容:

@implementation BigAndStrongEnemy

+(id)enemy {

BigAndStrongEnemy *enemy = nil;
if((enemy = [[[super alloc] initWithFile:@"bigAndStrongEnemy.gif"] autorelease])) {
    enemy.hp = 200;
    enemy.pointsWorth = 1000;
}
return enemy;

}

-(void)spriteMoveFinished:(id)sender {
    CCSprite *b = (CCSprite *)sender;
    [self removeChild:b cleanup:YES];
}

-(void)shoot {
    CCSprite *b = [CCSprite spriteWithFile:@"bullet.gif"];
    b.position = ccp(self.position.x,self.position.y);
    [self addChild:b];
    [bullets addObject:b];

    CGSize winSize = [[CCDirector sharedDirector] winSize];

    CGPoint point = CGPointMake((winSize.width - (winSize.width - self.position.x)),0);

    [b runAction:[CCSequence actions:
                  [CCMoveBy actionWithDuration:0.5 position:point],
                  [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                  nil]];
}

-(void)shoot:(id)sender {
    BigAndStrongEnemy *e = (BigAndStrongEnemy *)sender;
    [e shoot];
}

@end

2 个答案:

答案 0 :(得分:4)

据我所知,CCCallFunc就是您所需要的

答案 1 :(得分:3)

嗯...希望这有效:

id *func = [CCCallFunc actionWithTarget:buffDude selector:@selector(shoot)];