Hi everyone I am new and I have a problem where one of the sprites(npc) being drawn is a different texture than I set it to. It is the same texture as another sprite. I am loading the texture and setting it in the constructor of the class. The texture shouldn't be destroyed since it is not local and I don't have the white square problem.
int main()
{
TileMap t; //contains each tile in a map and other info like coordinates.
Player p;
.....
}
Npc.class
#include "Npc.h"
Npc::Npc(int x, int y,int gid, std::string location)
{
......
Npc::texture1.loadFromFile(Npc::location);
Npc::sprite1.setTexture(texture1);
}
The tilemap gets its data from a file and has a vector of Npcs. Also, If I switch the tilemap and player lines around, the sprite being drawn becomes the one being declared last.
Sorry for not being clear but I was actually storing the parameter to an attribute.
Npc.cpp
Npc::Npc(int x, int y,int gid, std::string location)
{
Npc::x = x;
Npc::y = y;
Npc::location = location;
Npc::gid = gid;
Npc::texture1.loadFromFile(Npc::location);
Npc::sprite1.setTexture(texture1);
}
TileMap.cpp
while(count >= 0)
{
if(atoi(attr->value()) >= locations[count].gid)
{
Npc temp(j,i,atoi(attr->value()) -
locations[count].gid, locations[count].location);
Npcs.push_back(temp);
break;
}
count--;
}
//I omitted alot of code but basically its just loops that take data from file. Then creating and storing each npc's position, image path etc inside the vector.
Main.cpp
int main()
{
TileMap t;
Player p;
//inside the game loop draws the tiles
for(int q = 0; q < t.Npcs.size(); q++)
{
//t.Npcs[q].sprite1.setTexture(t.Npcs[q].texture1);
t.Npcs[q].sprite1.setPosition(t.Npcs[q].getX()*32,t.Npcs[q].getY()*32);
t.Npcs[q].sprite1.setTextureRect(IntRect(0,0,32,48));
window.draw(t.Npcs[q].sprite1);
}
p.sprite.setTextureRect(IntRect(p.source.x%4*p.width,p.source.y*p.height,p.width,p.height));
window.draw(p.sprite);
window.display();
//above cove: draws the npc to screen and then draw the character.
*EDIT: Although it works if I add that commented out line t.Npcs[q].sprite1.setTexture(t.Npcs[q].texture1). I am not sure if that is a good way to do this because it will need to set the texture for every iteration in the loop.