因CCBlock而抛出异常?

时间:2015-02-17 20:29:40

标签: cocos2d-iphone

我使用了RAYWENDERLICH的一些代码,这是一个很棒的网站,但这不是重点。我从网站上得到的代码给了我一个艰难的时间就是这个(我添加了一个应该在屏幕上移动的怪物/幽灵):

- (void) addMonster {

CCSprite * monster = [CCSprite spriteWithImageNamed:@"Ghost.png"];

// Determine where to spawn the monster along the Y axis
CGSize viewSize = [CCDirector sharedDirector].viewSize;
int minY = monster.contentSize.height / 2;
int maxY = viewSize.height - monster.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = ccp(viewSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster z: -4];

// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Create the actions
CCActionMoveTo * actionMove = [CCActionMoveTo actionWithDuration:actualDuration
                                                        position:ccp(-monster.contentSize.width/2, actualY)];
CCActionCallBlock * actionMoveDone = [CCActionCallBlock actionWithBlock:^(CCNode *node) {
    [node removeFromParentAndCleanup:YES];
}];
[monster runAction:[CCActionSequence actions:actionMove, actionMoveDone, nil]];

}

上面的代码添加在init方法之上,而这是在init方法中添加的:

//How often a new ghost gets produced
      [self schedule:@selector(gameLogic:) interval:1.0];

在init方法之后,我有如下的回调方法:

//The call back function
-(void)gameLogic:(CCTime)dt {
[self addMonster];
}

然而,我在运行应用程序几秒钟后向我抛出异常导致终止。调试器中的消息如下所示:

-[__NSGlobalBlock__ removeFromParentAndCleanup:]: unrecognized         selector sent to instance 0x10ef5ee20
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSGlobalBlock__ removeFromParentAndCleanup:]: unrecognized selector sent to instance 0x10ef5ee20'

因此,就我的理解而言,阻止方法存在问题,但我无法确切地弄清楚究竟是什么。

我有什么线索可以解决这个问题和/或代码有什么问题以及为什么?

1 个答案:

答案 0 :(得分:0)

不确定addMonster方法中'节点'是指什么,但如果你想在移动后消灭怪物,你可能意味着:

[monster removeFromParentAndCleanup:YES]; // monster, not node