我是 Cocos2d 游戏开发的初学者,我已经完成了 raywenderlich 的教程,http ://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2 -x-tutorial 问题是我想检测怪物与玩家对象之间的碰撞,如下面的代码
-(void)gameLogic:(ccTime)dt {
[self addMonster];
for (CCSprite *monster in _monsters) {
CGRect targetRect = CGRectMake(
monster.position.x - (monster.contentSize.width/2),
monster.position.y - (monster.contentSize.height/2),
monster.contentSize.width,
monster.contentSize.height);
if (CGRectIntersectsRect(targetRect, player.boundingBox)) {
NSLog(@"Intersect Right!");
}
}
}
-(id) init
{
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background-music-aac.caf"];
_monsters = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
if ((self = [super initWithColor:ccc4(255, 255, 255, 255)])) {
CGSize winSize = [CCDirector sharedDirector].winSize;
player = [CCSprite spriteWithFile:@"player.png"];
[player setColor:ccRED];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
}
[self schedule:@selector(gameLogic:) interval:1.0];
[self schedule:@selector(update:)];
[self setTouchEnabled:YES];
return self;
}
- (void) addMonster {
CCSprite * monster = [CCSprite spriteWithFile:@"monster.png"];
// Determine where to spawn the monster along the Y axis
CGSize winSize = [CCDirector sharedDirector].winSize;
int minY = monster.contentSize.height / 2;
int maxY = winSize.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(winSize.width + monster.contentSize.width/2, actualY);
[self addChild:monster];
// 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
CCMoveTo * actionMove = [CCMoveTo actionWithDuration:actualDuration
position:ccp(-monster.contentSize.width/2, actualY)];
CCCallBlockN * actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) {
// CCScene *gameOver = [GameOverLayer sceneWithWon:NO];
// [[CCDirector sharedDirector] replaceScene:gameOver];
[node removeFromParentAndCleanup:YES];
}];
[monster runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
monster.tag = 1;
[_monsters addObject:monster];
}
但我无法获得这 2 个对象之间的准确命中,甚至有时这 2 个对象直接相互碰撞,但似乎日志没有运行。