我正在制作 Animal Crossing 克隆,但我在渲染播放器时遇到问题
I'm making a Animal Crossing clone, but I'm having problems rendering the Player
最近,我决定在
C++ 和 SFML 2.1。但我遇到了一些问题。 Player 在被命令渲染时不会出现。该程序将编译并且 运行 很好,但播放器不会出现。
这是我的代码:
#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();
}
}
我已经盯着这段代码看了一个小时了。但是我就是找不到错误!
请帮忙!
编辑:我解决了 sprite 不可见的问题,但 sprite 只是白色而不是适当的颜色。它可能与我加载文件的方式有关。请 post 关于如何解决这个新问题的任何建议!
您的 sprite 呈现为白色,因为在您的 Villager 构造函数中,您将局部纹理变量赋予 setTexture,然后在构造函数作用域的末尾被破坏。
最近,我决定在 C++ 和 SFML 2.1。但我遇到了一些问题。 Player 在被命令渲染时不会出现。该程序将编译并且 运行 很好,但播放器不会出现。
这是我的代码:
#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();
}
}
我已经盯着这段代码看了一个小时了。但是我就是找不到错误!
请帮忙!
编辑:我解决了 sprite 不可见的问题,但 sprite 只是白色而不是适当的颜色。它可能与我加载文件的方式有关。请 post 关于如何解决这个新问题的任何建议!
您的 sprite 呈现为白色,因为在您的 Villager 构造函数中,您将局部纹理变量赋予 setTexture,然后在构造函数作用域的末尾被破坏。