View类未接收更新的Model类数据

时间:2016-11-01 19:38:33

标签: c++ model-view-controller sfml

模型类

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测试确实显示了更改。

我做错了什么?有人可以给我一些指示吗?

1 个答案:

答案 0 :(得分:1)

在上面的代码段中,来自shipModelmain()的{​​{1}}对象是两个不同的对象。您可以让shipView.model使用shipView中的setter了解模型,也可以直接调用spaceshipView的方法。