您好,我在 cocos2d 游戏中遇到碰撞检测问题。我正在开发一款需要测试子弹是否击中角色的游戏。我正在使用 CGRectIntersectsRect 方法来查看是否发生碰撞。在模拟器中,您可以看到子弹穿过角色,但没有任何反应。如果子弹击中角色,我希望角色消失。在我的代码中,我有一个 CCLOG 语句,如果子弹击中角色,则输出“COLLISION”。此外,我还有另外两个 CCLOG 语句,它们输出项目符号和字符的 contentSize.width。子弹的 contentSize.width 应该是 20.0 但有时它会输出宽度为 0。这是碰撞检测的代码。
-(void)testForBulletCollision:(ccTime)delta{
CCLOG(@"bullet.contentsize.width = %f",bullet.contentSize.width);
CCLOG(@"character.contentsize.width = %f",character.contentSize.width);
if (CGRectIntersectsRect([bullet boundingBox], [character boundingBox]))
{
CCLOG(@"BULLET COLLISION");
character.visible = NO;
bullet.visible = NO;
}
}
这是创建角色的代码。
character = [CCSprite spriteWithFile:@"mcharacter.png"];
character.position = ccp(screenWidth/3.4, screenHeight/2 - 100);
[self addChild:character z:-3];
这是创建子弹和子弹动画的代码。
-(void)shootTheBullets:(ccTime)delta{
bullet = [CCSprite spriteWithFile:@"thebullet.png"];
bullet.color = ccRED;
bullet.position = redEnemy.position;
[self addChild:bullet z:-1];
bulletRect = CGRectMake(bullet.position.x - (bullet.contentSize.width/2),
bullet.position.y - (bullet.contentSize.height/2),
bullet.contentSize.width,
bullet.contentSize.height);
CCLOG(@"bullet.contentSize.width = %f", bullet.contentSize.width);
bulletMoveLeft = [CCMoveTo actionWithDuration:4.0 position:ccp(-screenWidth, screenHeight/2)];
[bullet runAction:bulletMoveLeft];
[self schedule: @selector(stopBullets:)interval:18.0f/1.0f];
}
-(void)stopBullets:(ccTime)delta{
[self unschedule:@selector(shootTheBullets:)];
}
这是输出。
2013-08-07 16:31:43.637 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.638 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013- 08-07 16:31:43.638 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.638 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08- 07 16:31:43.639 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.639 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16 :31:43.685 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.686 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31 :43.686 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.687 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.687 Zach App[1320:a0b] 项目符号。contentsize.width = 0.000000 2013-08-07 16:31:43.688 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.688 Zach App[1320:a0b] bullet.contentsize。宽度 = 0.000000 2013-08-07 16:31:43.689 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.689 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.690 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.691 Zach App[1320:a0b] bullet.contentSize.width = 20.000000 2013 -08-07 16:31:43.836 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.837 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08 -07 16:31:43.838 Zach App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.838 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43。839 扎克应用 [1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.840 扎克应用 [1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.841 扎克App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.842 Zach App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.843 Zach App[ 1320:a0b] bullet.contentsize.width = 20.000000 2013-08-07 16:31:43.843 Zach App[1320:a0b] character.contentsize.width = 64.000000000000 2013-08-07 16:31:43.843 扎克应用 [1320:a0b] character.contentsize.width = 64.000000000000 2013-08-07 16:31:43.843 扎克应用 [1320:a0b] character.contentsize.width = 64.000000