skippy_winks
skippy_winks

Reputation: 778

How to run a method within a CCAction?

I am currently on the brink of throwing my computer at the wall because I cannot figure this out. I have done about 200 Google searches, and every link is clicked up to like page 6. I cannot find an answer. So here's the dirt:

I want my Enemies class to contain a shoot method. Simple enough right? Well, I have the action to move the enemies in the HelloWorldLayer method. I want to find a way to have (some type of) CCAction call that method from the Enemies.m class. Please help! And @Lukman your Object Oriented Programming answer didn't work. Thanks!

EDIT:

Here's what's in HelloWorldLayer.m that is necessary to the answer:

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

And here's what is in 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

Upvotes: 1

Views: 3088

Answers (2)

xuanweng
xuanweng

Reputation: 1939

hmm... hope this works:

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

Upvotes: 3

Mikhail Vasilev
Mikhail Vasilev

Reputation: 1167

as far as i understood CCCallFunc is what you need

Upvotes: 4

Related Questions