我有一个设置动画的助手类,但是当我调用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>
最佳答案
我怀疑这是正确的:
id spriteObjRemove = [CCCallBlock actionWithBlock:^
{
[spriteObjRemove removeFromParentAndCleanup:YES]; <=== using action instead of sprite
//[asteroidArray removeObject:asteroid];
}];
尝试
[spriteObj removeFromParentAndCleanup:YES];
关于ios - Objective-C:使用CCCallBlock导致Helper类出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25161650/