Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: getPosition() is returning 0,0 when the sprite visable elsewhere  (Read 2151 times)

0 Members and 1 Guest are viewing this topic.

Aerine

  • Newbie
  • *
  • Posts: 10
    • View Profile
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]);

    }
}
« Last Edit: April 22, 2017, 09:08:57 am by Aerine »

Martin Sand

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: getPosition() is returning 0,0 when the sprite visable elsewhere
« Reply #1 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?

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: getPosition() is returning 0,0 when the sprite visable elsewhere
« Reply #2 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.
« Last Edit: April 22, 2017, 08:27:39 pm by Ruckamongus »

Aerine

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: getPosition() is returning 0,0 when the sprite visable elsewhere
« Reply #3 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!!!