x = x + 1
class spaceshipModel {
private:
Vector2f position;
float speed, acceleration, energy, fuel;
public:
//Contructor
spaceshipModel() : position(0, 0), speed(0), acceleration(0), energy(0), fuel(0) {}
//Destructor
~spaceshipModel() {}
//Sets
void setPosition(float _x, float _y) { position.x = _x; position.y = _y; }
void setSpeed(float _speed) { speed = _speed; }
void setAcceleration(float _acceleration) { acceleration = _acceleration; }
void setEnergy(float _energy) { energy = _energy; }
void setFuel(float _fuel) { fuel = _fuel; }
//Gets
Vector2f getPosition() { return position; }
float getSpeed() { return speed; }
float getAcceleration() { return acceleration; }
float getEnergy() { return energy; }
float getFuel() { return fuel; }
};
剩下很多代码,但我在main中调用它:
class spaceshipView {
private:
Texture* image;
Sprite sprite;
spaceshipModel model;
public:
//Constructor
spaceshipView() : image(0) {}
//Destructor
~spaceshipView() {}
//Setting the image
void setImage(Texture* _image) { image = _image; }
//Drawing the image
void drawImage(RenderWindow* _window) {
sprite.setTexture(*image);
sprite.setPosition(model.getPosition());
sprite.setScale(Vector2f(0.2f, 0.2f));
_window->draw(sprite);
_window->display();
}
};
宇宙飞船画得很完美,但只设置为(0,0)。即使将位置设置为(100,100),如上所示。图像保持在(0,0)。由于我使用View类中Model类的getPosition函数,我不认为数据正在正确更新,即使cout测试确实显示了更改。
我做错了什么?有人可以给我一些指示吗?
答案 0 :(得分:1)
在上面的代码段中,来自shipModel
和main()
的{{1}}对象是两个不同的对象。您可以让shipView.model
使用shipView
中的setter了解模型,也可以直接调用spaceshipView
的方法。