我正在制作动物森友会克隆,但在渲染播放器时遇到问题

I'm making a Animal Crossing clone, but I'm having problems rendering the Player

本文关键字:问题 遇到 播放器      更新时间:2023-10-16

最近,我决定将动物克隆制成C 和SFML 2.1。但是我有一些问题。当命令渲染时,玩家不会出现。该程序会编译并运行正常,但玩家不会出现。

这是我的代码:

#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(700, 500), "Animal Crossing: Old oak");
View view(FloatRect(1000, 1000, 300, 200));
class Villager{
public:
int x, y, w, h;
Sprite pl;
string loadDir;
Villager(int x, int y, int w, int h, Color c, string loadDir){
    this->x = x;
    this->y = y;
    this->w = w;
    this->h = h;
    Image image;
    image.loadFromFile(loadDir);
    image.createMaskFromColor(Color::Magenta);
    Texture tex;
    tex.loadFromImage(image);
    pl.setTexture(tex);
}
}
};
int main(){
Villager villager(1100, 1000, 100, 100, Color::Blue, "player.png");
view.zoom(5);
Image grasstexloader;
grasstexloader.loadFromFile("grass.png");
Texture grasstex;
grasstex.loadFromImage(grasstexloader);
Sprite grass;
grass.setTexture(grasstex);
while(window.isOpen()){
    Event event;
    while(window.pollEvent(event)){
        if(event.type == Event::Closed)
            window.close();
        if(Keyboard::isKeyPressed(Keyboard::Up))
            villager.moveUp();
        if(Keyboard::isKeyPressed(Keyboard::Down))
            villager.moveDown();
        if(Keyboard::isKeyPressed(Keyboard::Left))
            villager.moveLeft();
        if(Keyboard::isKeyPressed(Keyboard::Right))
            villager.moveRight();
        if(Keyboard::isKeyPressed(Keyboard::Escape))
            window.close();
    }
    window.setView(view);
    window.draw(grass);
    window.draw(villager.pl);
    window.display();
    window.clear();
}
}

我已经盯着这个代码了一个小时。但是我只是找不到错误!

请帮助!

编辑:我解决了问题,因为精灵不可见,但是精灵只是白色而不是适当的颜色。它可能与我加载文件的方式有关。请发布有关如何解决这个新问题的任何建议!

您的精灵是白色的,因为在您的村民构造函数中,您正在为setTexture提供本地纹理变量,然后在构造函数范围的末端被破坏。