Objective-c:使用CCCallBlock导致Helper类错误

时间:2014-08-06 13:26:09

标签: ios objective-c cocos2d-iphone

我有一个Helper类来设置动画,但是当我调用CCCallBlock时代码仍然失败。

这是助手类:

+(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;


    }

这就是在父代中调用代码的方式:

-(void)asteroidCreate{

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

[self countDownToCreateNextAsteroid];
}

错误

Printing description of block:
<__NSStackBlock__: 0xbfffab90>

1 个答案:

答案 0 :(得分:1)

我怀疑这是对的:

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

[spriteObj removeFromParentAndCleanup:YES];