Crash when using Cocos2d

Posted by ipodfreak0313 on Game Development See other posts from Game Development or by ipodfreak0313
Published on 2011-05-09T19:23:46Z Indexed on 2012/12/17 11:14 UTC
Read the original article Hit count: 261

Filed under:
|
|

Sorry about the poor question title, it's just that this seems to big for a title. So here's the dirt:

I am making a game (obviously) and I want the enemies to shoot (not necessarily at the player). I want the shoot method to be in the Enemies file, so as not to clutter up my HelloWorldLayer.m file even more. Here's what I'm using right now:

HelloWorldLayer.m

-(void)addEnemy:(BigAndStrongEnemy *)enemy {
enemy = nil;
if((arc4random() % 4) == 3) {
    enemy = [BigAndStrongEnemy enemy];
} else {
    enemy = [SmallAndFastEnemy enemy];
}

if(buffDude.position.y > character.position.y || buffDude.position.y < (character.position.y + 10)) {

}

int rand = arc4random() % 320;

if((arc4random() % 2 == 1)) {
    [enemy setPosition:ccp(0,rand)];
}else{
    [enemy setPosition:ccp(480,rand)];
}
[self animateEnemy:enemy];
[self addChild:enemy];
}

-(void)animateEnemy:(BigAndStrongEnemy *)enemy2 {
    float randX = arc4random() % 480;
    float randY = arc4random() % 320;
    int rand = arc4random() % 320;

CGPoint moveToPoint = CGPointMake(randX, (randY - rand));

[enemies addObject:enemy2];

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

CCCallFuncO *a = [CCCallFuncO actionWithTarget:self selector:(@selector(shoot:)) object:enemy2];
CCSequence *s = [CCSequence actions:action,a, nil];

CCRepeatForever *repeat = [CCRepeatForever actionWithAction:s];

[enemy2 runAction:repeat];
}

And here's the Shoot info from the Enemies class:

Enemies.m:

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

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

-(void)shoot {
    self = [CCSprite spriteWithFile:@"bigAndStrongEnemy.gif"];
    CCSprite *b = [CCSprite spriteWithFile:@"bullet.gif"];
    b.position = ccp(self.position.x,self.position.y);
    b.tag = 2;
    [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]];
}

Every time the 3 seconds goes by, the app crashes, and goes to the breakpoint in the CCCallFuncO file. I haven't touched it, is the thing. I am completely confused. Any help is greatly appreciated.

© Game Development or respective owner

Related posts about iphone

Related posts about cocos2d-iphone