iPhone 용 box2d 인 cocos2d를 사용하여 iPhone 게임을 개발하고 있습니다. 나는 여기에 하나의 문제가있다. iOS 게임에서 모든 연결 b2Body 가져 오기
위의 스크린 샷에서 볼 수 있듯이이 볼은 b2Body입니다. 한 가지를 제외하고 모든 것이 올바르게 진행됩니다. 내가 볼을 탭하면 화면에서 제거하고 잘 작동합니다.
그러나 동일한 색상의 연결된 볼을 모두 제거해야합니다. 예를 들어 바닥에 빨간 공을 두 드릴 때이 탭 공과 교차하는 빨간색 공도 모두 제거해야합니다.
이 방법은 탭 위치
- (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가되면, 매우 단순 두드린 볼 주위에있는 다른 볼을 찾습니다. 볼 B가 하나 인 경우 타격 볼이 없어 질 때까지 볼 B로 다시 이동합니다.
그러나, 재귀 함수는 논스톱 :(당신이 어떤 아이디어 나 Box2D의
이 기능을 할 수있는 알고리즘이 있다면 내가 당신에게 당신을위한 대단히 감사합니다 감사드립니다
을 실행하는 것 지원.
탕
내가 솔루션 나 자신을 발견
에서 발견되지 않은 공을 요구 /html/classb2_body.html#a16bdbfb266c82a0ef51be351a8928bc5 – LearnCocos2D
예,이 GetContactList()가 도움이됩니다. 그러나 접촉 된 볼의 볼을 가져 오기 위해 "재귀 확인"을하고 싶습니다. 그러나 "스택 오버플로"오류가 발생합니다. –