简体   繁体   English

“ this”指针有什么问题?

[英]What's wrong with “this” pointer?

So, i decided to create a collision detection function in my game and i put it into the class of enemies. 因此,我决定在游戏中创建一个碰撞检测功能,并将其归类为敌人。 Then i put it to "enemy.cpp" and i used "this" pointer. 然后我将其放入“ enemy.cpp”,并使用“ this”指针。 The code: 编码:

if(((this->sprite.getPosition().y + this->sprite.getTextureRect().height) >= blocklist[i].sprite.getPosition().y) &&
(this->sprite.getPosition().y  <= blocklist[i].sprite.getPosition().y))

This gives me SIGSEGV segmentation error. 这给了我SIGSEGV分割错误。 I can't find what could be the problem in this line, here is the enemyclass for reference. 我在这行中找不到问题所在,这里是敌人类供参考。

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);
};

and the blockclass that i made a vector of to use as a list of enemies: 和我作为向量的blockclass用作敌人列表:

class blockclass
{
public:
    sf::Sprite sprite;
    int x, y;
};

I would be very thankfull for an answer since i can't find out what's wrong. 我将非常感谢您的回答,因为我无法找出问题所在。

As others have said, if some complicated instruction causes an error, split it into smaller parts and see, in which part the error arises: 正如其他人所说,如果某个复杂的指令导致错误,请将其拆分为较小的部分,然后看看在哪部分出现错误:

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) {
    // ......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM