SFML community forums

Help => General => Topic started by: Aerine on April 22, 2017, 06:16:43 am

Title: getPosition() is returning 0,0 when the sprite visable elsewhere
Post by: Aerine on April 22, 2017, 06:16:43 am
I have a 2 classes. I have a tower class and I have a monster class. The default constructor automatically sets a position for the tower. I store the classes in a vector and run a function that returns the value of the towers x position. I can clearly see on my screen that the tower is not in the 0,0 coordinate, but it says it is anyway.

void checkcolission(TowerA tower)
{
    if (tower.getLeft() > monsterSprite.getPosition().x) /*&& tower.getTop() < monsterSprite.getPosition().y && tower.getBottom() > monsterSprite.getPosition().y*/
    {
        //std::cout << "#($)(#%*#%U#*(%*#%*#%" << std::endl;
    }
    std::cout << tower.getPositionX() << std::endl;

}

public://in my tower class
TowerA()
{
    spritetower1.setPosition(100, 50);
    towerRange.setPosition(getPositionX(), getPositionY());
}



//This is in my game class with push_backed vectors of the class
for (int i = 0; i < mymonsters.size(); i++)
{
    for (int k = 0; k < myicetower.size(); k++)
    {
        mymonsters[i].checkcolission(myicetower[k]);

    }
}
Title: Re: getPosition() is returning 0,0 when the sprite visable elsewhere
Post by: Martin Sand on April 22, 2017, 12:01:25 pm
I can clearly see on my screen that the tower is not in the 0,0 coordinate, but it says it is anyway.

Can you please be more specific, what component "says" the tower is in 0, 0 coordinate? Can you please post more code?
Title: Re: getPosition() is returning 0,0 when the sprite visable elsewhere
Post by: Ruckamongus on April 22, 2017, 05:23:12 pm
You're passing the towers to the check function by value so the class is getting copied. Is the copy constructor perhaps zeroing out the position? Try passing it by const reference instead. You may be having the same issue when you push_back into the vector.
Title: Re: getPosition() is returning 0,0 when the sprite visable elsewhere
Post by: Aerine on April 22, 2017, 07:33:15 pm
Oh man you are a genius that's exactly what was going on!!!! Thank you so much. You saved me hours more of a heacache!!!