Cocos2d iphone 由于CCBlock而引发异常?
我正在使用RAYWENDERLICH的一些代码,这是一个很棒的网站,但这不是重点。我从网站上得到的代码让我很难过,这是我在添加一个怪物/幽灵,它应该在屏幕上移动:Cocos2d iphone 由于CCBlock而引发异常?,cocos2d-iphone,Cocos2d Iphone,我正在使用RAYWENDERLICH的一些代码,这是一个很棒的网站,但这不是重点。我从网站上得到的代码让我很难过,这是我在添加一个怪物/幽灵,它应该在屏幕上移动: - (void) addMonster { CCSprite * monster = [CCSprite spriteWithImageNamed:@"Ghost.png"]; // Determine where to spawn the monster along the Y axis CGSize viewSize = [C
- (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'
就我的理解而言,block方法有一些问题,但我无法准确地找出问题所在
有什么线索可以帮助我解决这个问题和/或代码出了什么问题,为什么 不确定addMonster方法中的“节点”指的是什么,但如果您想在怪物移动后消失,您可能是指:
[monster removeFromParentAndCleanup:YES]; // monster, not node