2016-11-01 14 views
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; } 

}; 

Viewクラス

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(); 
} 
}; 

Aが出て多くのコードを残したが、私は、その後でこれを呼び出すメインメイン:Viewクラスは、モデルクラスのデータを更新受信していない

int main() { 

//Call instance of the Spaceship model 
spaceshipModel shipModel; 

//Call instance of the Spaceship view 
spaceshipView shipView; 

//Create the texture of the spaceship from file 
Texture spaceship; 
spaceship.loadFromFile("spaceship.png"); 

//Create the window 
RenderWindow window(VideoMode(800, 600), "Spaceship with MVC"); 

//Run the program as long as the window is open 
while (window.isOpen()) { 

    //Check all the window's events that were triggered since the last iteration of the loop 
    Event event; 

    while (window.pollEvent(event)) { 

     //"Close requested" event: we close the window 
     switch (event.type) { 

     //Window closed by pressing the X 
     case Event::Closed:  
      window.close(); 
      break; 

     //Checking for key pressed event 
     case Event::KeyPressed: 

      //Pressing esc to close the window 
      if (event.key.code == Keyboard::Escape) { 
       window.close(); 
      } 
      break;  

     //We don't process other types of events 
     default:    
      break; 
     } 

     //Clear screen with white BG 
     window.clear(Color::White); 

     //TESTING THE SETTING OF THE POSITION 
     std::cout << shipModel.getPosition().x << ", " << shipModel.getPosition().y << std::endl; 
     shipModel.setPosition(100, 100); 
     std::cout << shipModel.getPosition().x << ", " << shipModel.getPosition().y << std::endl; 

     //Set and draw the image 
     shipView.setImage(&spaceship); 
     shipView.drawImage(&window); 

    } 
} 

return 0; 

} 

ペシシップは完全に描かれますが、(0、0)に設定されています。上記のように(100,100)に設定しても画像は(0、0)のままです。 ViewクラスのModelクラスからgetPosition関数を使用しているので、データが正しく更新されているとは限りません。

私は間違っていますか?誰かが私にいくつかの指針を与えることができますか?

+0

もっとコードが必要な場合は、尋ねてみてください。 – Sectah

+0

投稿する[mcve](http://stackoverflow.com/help/mcve) – wally

+0

メインコード – Sectah

答えて

1

上記コードスニペットでは、shipModelオブジェクトはmain()shipView.modelの2つの異なるオブジェクトです。 shipViewspaceshipViewのセッターを使用してモデルを認識させるか、shipView.modelのメソッドに直接呼び出すかのいずれかを行うことができます。

+0

を更新しましたもちろん。ありがとうございました。 – Sectah

関連する問題