0
我一直在使用sfml 2.0庫的oop版本的蛇,我有一個單獨的類來處理碰撞。下面的代碼:SFML 2.0碰撞類
包括「collision.hpp」
bool sfc::Sprite::collision(sfc::Sprite sprite2) {
this->setBounds();
sprite2.setBounds();
if (top > sprite2.bottom || bottom < sprite2.top || left > sprite2.right || right < sprite2.left) {
return false;
}
return true;
}
void sfc::Sprite::colMove(sf::Vector2f &movement, sfc::Sprite sprite2) {
if (!this->collision(sprite2)) {
this->move(movement);
}
}
void sfc::Sprite::colMove(float x, float y, sfc::Sprite sprite2) {
if (!this->collision(sprite2)) {
this->move(x, y);
}
}
void sfc::Sprite::setBounds() {
top = this->getPosition().y;
bottom = this->getPosition().y + this->getTexture()->getSize().y;
left = this->getPosition().x;
right = this->getPosition().x + this->getTexture()->getSize().y;
}
唯一的問題是,一旦碰撞事件發生時,精靈被卡住窗口的餘生。我怎麼能得到它,以便在碰撞時它不會粘在那裏。謝謝! 〜邁克爾
編輯:據我所知,精靈是不允許移動發生碰撞後,但我不知道任何其他方式停止移動後的精靈的衝突。
可惜,這是行不通的。我明白我的精靈在碰撞後不允許移動,但你上面發佈的代碼只會在碰撞發生之前改變兩個精靈之間的距離。 – Michaelslec
哦,我明白了。我不記得太多關於smfl的信息,但是我希望它對你有所幫助 – th3sn4k3
謝謝你的回答!我很感激 :) – Michaelslec