I still don't understand the right way to fix the sprites appearing as white.
Here is my Bullet.cpp script:
#include <Bullet.h>
#include <iostream>
Bullet::Bullet(float _x, float _y, float _speed, float _rotation, float _damage)
{
this->bulletTexture.loadFromFile("Resources/Syringe.png");
this->bulletSprite.setTexture(this->bulletTexture);
this->rotation = _rotation;
this->damage = _damage;
this->speed = _speed;
this->radius = 5;
this->x = _x;
this->y = _y;
this->bullet = sf::CircleShape(this->radius);
}
void Bullet::update()
{
float horizontal = -sin(this->rotation * (3.14f / 180.f)) * this->speed;
float vertical = cos(this->rotation * (3.14f / 180.f)) * this->speed;
this->x -= horizontal;
this->y -= vertical;
this->bulletSprite.setPosition(this->x, this->y);
this->bullet.setPosition(this->x, this->y);
}
void Bullet::render(sf::RenderWindow& window)
{
window.draw(this->bulletSprite);
}
and here is my Bullet.h script:
#pragma once
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
class Bullet
{
public:
Bullet(float _x, float _y, float _speed, float _rotation, float _damage);
sf::Texture bulletTexture;
sf::Sprite bulletSprite;
float rotation;
float damage;
float radius;
float speed;
float x;
float y;
sf::CircleShape bullet;
void render(sf::RenderWindow& window);
void update();
};
I even tried to do something like this in the update function:
if (!this->bulletTextured)
{
this->bulletSprite.setTexture(this->bulletTexture);
this->bulletTextured = true;
}
But still like it kind of worked but didn't at the same time. Sometimes the bullets would appear as a white box and sometimes they work with the texture and sometimes they work at the first second after they are shot and they turn to a white box, this is very confusing
and my sprite still appears as a white square.
The only way I managed to get the sprite to work properly is when I put sprite.setTexture() in the update method.
Is there a correct way to fix this problem?