c++ - "this"指针有什么问题?

标签 c++ pointers

所以,我决定在我的游戏中创建一个碰撞检测功能,并将其放入敌人类中。然后我把它放到“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/

相关文章:

c - 通过指针传递单个字符与通过指针传递字符串

c++ - 为什么成员函数调用 "ambiguous"?

C++ 数组超出范围访问计算指针有效?

c++ - 为什么我不能创建带有可选 UnaryPredicate 参数的模板函数?

c++ - 如何使用 cmake 组织大型构建

c - 指针作为调用 scanf 的函数的参数

c - 为什么 memchr() 将 void 指针作为输入?

c - Memsetting 指向数组的指针

java - 在 Mac OSX 上为 OpenCV 编译 Android JNI

C++ 未定义的函数引用