由于 CCBlock 抛出异常?
Throwing exception due to CCBlock?
我正在使用 RAYWENDERLICH 的一些代码,这是一个很棒的网站,但这不是重点。我从网站上下来的代码给我带来了困难(我添加了一个应该在屏幕上移动的 monster/ghost):
- (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'
据我所知,块方法有问题,但我无法弄清楚到底是什么。
知道如何解决这个问题 and/or 代码有什么问题,为什么?
不确定 'node' 在你的 addMonster
方法中指的是什么,但如果你想在怪物移动后消失,你可能意味着:
[monster removeFromParentAndCleanup:YES]; // monster, not node
我正在使用 RAYWENDERLICH 的一些代码,这是一个很棒的网站,但这不是重点。我从网站上下来的代码给我带来了困难(我添加了一个应该在屏幕上移动的 monster/ghost):
- (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'
据我所知,块方法有问题,但我无法弄清楚到底是什么。
知道如何解决这个问题 and/or 代码有什么问题,为什么?
不确定 'node' 在你的 addMonster
方法中指的是什么,但如果你想在怪物移动后消失,你可能意味着:
[monster removeFromParentAndCleanup:YES]; // monster, not node