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

Author Topic: Texture error?  (Read 258 times)

0 Members and 1 Guest are viewing this topic.

LunaUwU

  • Newbie
  • *
  • Posts: 1
    • View Profile
Texture error?
« on: March 15, 2024, 04:37:48 am »
Hello. I have a mini game that is similar to pang and I managed to get everything working well. I tried to make a menu system so when you die/win it won't simply close the window and you have to open it again, and it will instead go to the main menu and restart everything. So, when restarting everything I stumbled upon a problem with the balls when activating them.
When you activate a ball via starting a game (activate 1 ball), or shooting at an already existing ball (it will activate 2 balls), it will change the texture of the ball to another one with a texture of a smaller ball.
When in the first iteration of the game, every ball will have their texture initialized for the first time, giving no issue, but when in the second+ iteration, it will start having issues when trying to reapply a new texture.
There are no errors or anything but some of the textures are really messed up and it wouldn't look good to make a game like this.

void Ball::init(const int ballSize, const bool active)
{
    m_ballSize = ballSize;
    m_ballWidth = m_ballSizes.sizeX[m_ballSize];
    m_ballHeight = m_ballSizes.sizeY[m_ballSize];

    m_ball.setSize(sf::Vector2f(m_ballWidth, m_ballHeight));
    m_ballTexture.loadFromFile(m_ballSizes.fileNames[m_ballSize]);
    m_ball.setTexture(&m_ballTexture);
    m_hitPointX = m_ballSizes.hitPointX[m_ballSize];
    m_hitPointY = m_ballSizes.hitPointY[m_ballSize];
   
    if(!active)
    {
        deactivate();
    }
}

 
This is the function that activates a new ball
« Last Edit: March 15, 2024, 08:41:21 am by eXpl0it3r »

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Texture error?
« Reply #1 on: March 15, 2024, 06:08:10 am »
When you call setTexture, try adding true as a second parameter. That will reset the texture rectangle used by the sprite (needed if the textures are different sizes).