こんにちは、cocos2d ゲームで衝突検出に問題があります。弾丸がキャラクターに当たるかどうかをテストする必要があるゲームに取り組んでいます。CGRectIntersectsRect メソッドを使用して、衝突が発生するかどうかを確認しています。シミュレーターでは、弾丸がキャラクターの上を通過するのを見ることができますが、何も起こりません。弾が当たったらキャラクターが消えるようにしたいです。私のコードには、弾丸が文字に当たった場合に「COLLISION」を出力する CCLOG ステートメントがあります。また、箇条書きと文字の contentSize.width を出力する CCLOG ステートメントが他に 2 つあります。弾丸の 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 ザック アプリ[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.638 ザック アプリ[1320:a0b] character.contentsize.width = 64.000000 2013- 08-07 16:31:43.638 ザック アプリ[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.638 ザック アプリ[1320:a0b] character.contentsize.width = 64.000000 2013-08- 07 16:31:43.639 ザック App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.639 ザック App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16 :31:43.685 ザック App[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.686 ザック App[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31 :43.686 ザック アプリ[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.687 ザック アプリ[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 ザック アプリ[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.689 ザック アプリ[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.690 ザック アプリ[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.691 ザック アプリ[1320:a0b] bullet.contentSize.width = 20.000000 2013 -08-07 16:31:43.836 ザック アプリ[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.837 ザック アプリ[1320:a0b] character.contentsize.width = 64.000000 2013-08 -07 16:31:43.838 ザック アプリ [1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.838 ザック アプリ[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 ザック アプリ[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.843 ザック アプリ[ 1320:a0b] bullet.contentsize.width = 20.000000 2013-08-07 16:31:43.843 ザック App[1320:a0b] character.contentsize.width = 64.000000000000 2013-08-07 16:31:43.843 ザック App[1320:a0b] character.contentsize.width = 64.000000000000 2013-08-07 16:31:43.843 ザック App[1320:a0b] character.contentsize.width = 64.000000