Search code examples
iosobjective-ccocos2d-iphone

Objective-c: Using CCCallBlock causing error in Helper Class


I have a Helper Class that sets up animation but the code keeps failing when i call CCCallBlock.

Here is the Helper Class:

+(CCSpriteBatchNode*)setupAnimation:(NSString*)spriteNameWithoutPNG :(int)numberOfFrames :(CCSprite*)spriteObj :(int)startRandomX :(int)endRandomX :(int)endDuration{

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        NSString *plist = [NSString stringWithFormat:@"%@.plist",spriteNameWithoutPNG];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist];

        NSString *spriteFileName = [NSString stringWithFormat:@"%@.png",spriteNameWithoutPNG];
        CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:spriteFileName];

        NSMutableArray *animFrames = [NSMutableArray array];
        for(int i=1; i <= numberOfFrames; i++){
        NSString *frameName = [NSString stringWithFormat:@"%@%d.png",spriteNameWithoutPNG, i];
        [animFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName]];
        }

        CCAnimation *moveAnim = [CCAnimation animationWithFrames:animFrames delay:0.1f];

        NSString *spriteFrameName = [NSString stringWithFormat:@"%@1.png",spriteNameWithoutPNG];
        spriteObj = [CCSprite spriteWithSpriteFrameName:spriteFrameName];

        int x = arc4random() % startRandomX;
        spriteObj.position = ccp(x, 480);
        CCAction *actionFrames = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:moveAnim restoreOriginalFrame:NO]];

        int q = arc4random() % endRandomX;
        int r = arc4random() % endDuration;
        CCAction *moveAction = [CCMoveTo actionWithDuration:r position:ccp(q, -50)];

        id spriteObjRemove = [CCCallBlock actionWithBlock:^
                 {
                     [spriteObjRemove removeFromParentAndCleanup:YES];
                     //[asteroidArray removeObject:asteroid];
                 }];

        id spriteSeq = [CCSequence actions:moveAction, spriteObjRemove, nil];

        [spriteObj runAction:spriteSeq];
        [spriteObj runAction:actionFrames];
        [spriteSheet addChild:spriteObj];
        return spriteSheet;


    }

And this is how the code gets called in the parent:

-(void)asteroidCreate{

Asteroid *asteroid = [[Asteroid alloc] init:@"asteroid1.png"];
CCSpriteBatchNode *spritesheet = [Helper setupAnimation:@"asteroid" :8 :asteroid :320 :320 :10];
[self addChild:spriteSheet];

[self countDownToCreateNextAsteroid];
}

ERROR

Printing description of block:
<__NSStackBlock__: 0xbfffab90>

Solution

  • i doubt this is correct :

    id spriteObjRemove = [CCCallBlock actionWithBlock:^
    {
        [spriteObjRemove removeFromParentAndCleanup:YES];  <=== using action instead of sprite
      //[asteroidArray removeObject:asteroid];
    }];
    

    try

    [spriteObj removeFromParentAndCleanup:YES];