所以,我决定在我的游戏中创建一个碰撞检测功能,并将其放入敌人类中。然后我把它放到“enemy.cpp”并使用“this”指针。 代码:
if(((this->sprite.getPosition().y + this->sprite.getTextureRect().height) >= blocklist[i].sprite.getPosition().y) &&
(this->sprite.getPosition().y <= blocklist[i].sprite.getPosition().y))
这给了我 SIGSEGV 段错误。我找不到这一行可能的问题,这里是 enemyclass 以供引用。
class enemyclass
{
public:
sf::Sprite sprite;
sf::Sprite bubble;
//PROPERTIES
float xspeed, yspeed;
int x, y;
float health;
bool colR, colL, colU, colD;
int skin;
void detectCollisions(int blocksize);
};
以及我制作的 vector 用作敌人列表的 block 类:
class blockclass
{
public:
sf::Sprite sprite;
int x, y;
};
非常感谢您的回答,因为我无法找出问题所在。
最佳答案
正如其他人所说,如果某些复杂的指令导致错误,请将其拆分成更小的部分,看看错误出现在哪一部分:
if( this->x == 0) // changes nothing
x = 0; // but validates 'this'
int thisy = sprite.getPosition().y;
int thish = sprite.getTextureRect().height;
sf::Sprite &that = blocklist[i].sprite;
if( that.x == 0) // validate 'that'
that.x = 0;
int thaty = that.getPosition().y;
if(thisy + thish >= thaty && thisy <= thaty) {
// ......
}
关于c++ - "this"指针有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688078/