0
我的問題很簡單,但我無法修復它。Box2d Cocos2d ContactListener檢測碰撞
我有一個雷達是旋轉和我有與操縱桿
好動的球員,我只是想探測雷達和球員之間的碰撞。當都在移動時它可以很好地工作,但當雷達正在移動並且我的播放器不是時它不會運行。
在這裏,你有代碼來檢測它,它是如何工作的:
//moves radar
[self schedule:@selector(loopRadar) interval: 12];
//moves player with joystick
body->SetLinearVelocity(b2Vec2(scaledVelocity.x*dt, scaledVelocity.y*dt));
actor.position = ccp(body->GetPosition().x * PTM_RATIO,
body->GetPosition().y * PTM_RATIO);
// DETECTS COLLISION BETWEEN RADAR AND PLAYER
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
if ((contact.fixtureA == radarBody->GetFixtureList() && contact.fixtureB == body->GetFixtureList()) ||
(contact.fixtureA == body->GetFixtureList() && contact.fixtureB == radarBody->GetFixtureList())) {
//DO SOMETHING LIKE GAME OVER
}
}
//ENDS COLLISION
MycontactListener類:
#import "MyContactListener.h"
MyContactListener::MyContactListener() : _contacts() {
}
MyContactListener::~MyContactListener() {
}
void MyContactListener::BeginContact(b2Contact* contact) {
// We need to copy out the data because the b2Contact passed in
// is reused.
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
void MyContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void MyContactListener::PreSolve(b2Contact* contact,
const b2Manifold* oldManifold) {
}
void MyContactListener::PostSolve(b2Contact* contact,
const b2ContactImpulse* impulse) {
}
那麼,問題是,當雷達經過玩家與玩家ISN」不移動,它不會檢測到碰撞,但是當兩者移動時,它都可以很好地工作。
任何提示? 非常感謝!
你有沒有試過禁用播放器和雷達機構的睡眠? – LearnCocos2D 2013-05-06 21:59:15
工作!非常感謝你!! – mursang 2013-05-07 21:10:52