iPhone用のcocos2d、box2dでiPhoneゲームを開発しています。私はここに1つの問題があります。 iOSゲームで接続するすべてのb2Bodyを取得する
上記のスクリーンショットからわかるように、これらのボールはb2Bodyです。 1つのことを除いて、すべてが正しく進みます。ボールをタップすると、ボールを画面から取り除き、うまく動作します。
しかし、同じ色の接続ボールもすべて取り外す必要があります。例えば、私が底にある赤いボールをタップすると、このタップボールと交差する赤いボールもすべて削除する必要があります。
この方法では、タップ位置
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *touch in touches) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
// go through every single b2Body
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
b2Fixture *bf = b->GetFixtureList();
// check which ball is tapped
if (bf->TestPoint(locationWorld)) {
[self destroyBall:b];
}
}
}
}
「destroyBall」方法を管理している
- (void) destroyBall:(b2Body *)body_ {
CCSprite *bodySprite = (CCSprite *) body_->GetUserData();
CGFloat h2 = bodySprite.contentSize.height/2;
CGFloat w2 = bodySprite.contentSize.width/2;
b2Vec2 p1 = b2Vec2((bodySprite.position.x - w2)/PTM_RATIO, (bodySprite.position.y - h2)/PTM_RATIO);
b2Vec2 p2 = b2Vec2((bodySprite.position.x - w2)/PTM_RATIO, (bodySprite.position.y + h2)/PTM_RATIO);
b2Vec2 p3 = b2Vec2((bodySprite.position.x + w2)/PTM_RATIO, (bodySprite.position.y + h2)/PTM_RATIO);
b2Vec2 p4 = b2Vec2((bodySprite.position.x + w2)/PTM_RATIO, (bodySprite.position.y - h2)/PTM_RATIO);
// go through all b2Body to check which one is intersect with body_
for (b2Body *b = world->GetBodyList(); b; b = b->GetNext()) {
if ((b->GetUserData() != NULL) && (b != body_)) {
CCSprite *newBall = (CCSprite *)b->GetUserData();
b2Fixture *bf = b->GetFixtureList();
if (bf->TestPoint(p1) || bf->TestPoint(p2) || bf->TestPoint(p3) || bf->TestPoint(p4)) {
if (bodySprite.tag == newBall.tag) {
[self destroyBall:b];
}
}
}
}
CCSpriteBatchNode *batch = (CCSpriteBatchNode *) [self getChildByTag:kTagBatchNode];
[batch removeChild:bodySprite cleanup:YES];
world->DestroyBody(body_);
}
以下のように書かれている再帰的な方法、アイデアボールAである場合には、非常に簡単ですタップすると、ボールAを打つ周囲の他のボールを探します。ボールBがボールの場合は、打球がなくなるまでボールBでプロセスが再開します。
しかし、再帰関数はノンストップを実行しているようだ:(
あなたはBOX2D
でこの機能を実行する任意のアイデアや任意のアルゴリズムを持っている場合、私は感謝しないあなたのためにありがとうございましたサポート。
タン
look up b2Body-> GetContactList()here:http://www.learn-cocos2d.com/api-ref/latest/Box2D /html/classb2_body.html#a16bdbfb266c82a0ef51be351a8928bc5 – LearnCocos2D
はい、このGetContactList()が役に立ちます。しかし、私は接触したボールの接触したボールを得るために「再帰的なチェック」をしたい。しかし、それは "スタックオーバーフロー"エラーを発生させる:( –