我为玩家制作了一个角色类,我想在该类中添加一名玩家成员然后绘制该玩家。我正在使用sfml和xcode。我得到的错误是:没有匹配的成员函数来调用' draw'在:window.draw(播放器);线。看起来我需要将一个精灵对象放入.draw()中,那么我如何制作属于该类的玩家精灵并将其绘制到窗口上?我是c ++和sfml的新手所以非常感谢任何帮助。
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
class Character{
public:
string sprite;
int health;
int defense;
int speed;
int experience;
bool move;
int x_pos;
int y_pos;
sf::Texture texture;
//Constructor - Ran everytime a new instance of the class is created
Character(string image){
health = 100;
defense = 100;
speed = 100;
experience = 0;
x_pos = 0;
y_pos = 0;
texture.loadFromFile(image);
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, 100, 100));
sprite.setPosition(x_pos, y_pos);
}
//Destructor - Ran when the object is destroyed
~Character(){
}
//Methods
void forward();
void backward();
void left();
void right();
void attack();
};
void Character::forward(){
cout << "Go Forward";
}
void Character::backward(){
cout << "Go Backward";
}
void Character::left(){
cout << "Go Left";
}
void Character::right(){
cout << "Go Right";
}
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Player.png");
int main() {
// insert code here...
int windowWidth = 1150;
int windowHeight = 750;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
while(window.isOpen()){
window.draw(player);
window.display();
window.setFramerateLimit(60);
}
}
答案 0 :(得分:0)
你可以简单地添加一个会返回玩家精灵的成员函数:
sf::Sprite Character::getSprite() {
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, 100, 100));
sprite.setPosition(x_pos, y_pos);
return sprite;
}
然后将结果传递给window.draw
:
window.draw(player.getSprite());
或者,您可以反转依赖关系并让玩家负责所有绘图:
void Character::draw(sf::RenderWindow& w) {
w.draw(...);
}
还有其他方法,选择在很大程度上取决于很多外部因素。不过,这应该可以让你开始。