2017-10-25 19 views
-1

以下は、ウィンドウ内に2つの画像を表示する私のコードです。SFMLマウスの動きを使って複数の画像を個別に移動する方法は?

マウスを動かすと、両方の画像が一緒に移動しますが、別々にアクセスする必要がありますか?

int main() 
{ 
// Let's setup a window 
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML ViewTransformation"); 

// Let's create the background image here, where everything initializes. 
sf::Texture BackgroundTexture; 
sf::Sprite bd; 
sf::Vector2u TextureSize; //Added to store texture size. 
sf::Vector2u WindowSize; //Added to store window size. 

if(!BackgroundTexture.loadFromFile("bg1.jpg")) 
    { 
     return -1; 
    } 
else 
{ 
TextureSize = BackgroundTexture.getSize(); //Get size of texture. 
WindowSize = window.getSize();    //Get size of window. 

float ScaleX = (float) WindowSize.x/TextureSize.x; 
float ScaleY = (float) WindowSize.y/TextureSize.y;  //Calculate scale. 

bd.setTexture(BackgroundTexture); 
bd.setScale(ScaleX, ScaleY);  //Set scale. 
} 
// Create something simple to draw 
sf::Texture texture; 
texture.loadFromFile("background.jpg"); 
sf::Sprite background(texture); 
sf::Vector2f oldPos; 
bool moving = false; 

sf::View view = window.getDefaultView(); 

while (window.isOpen()) { 
    sf::Event event; 
    while (window.pollEvent(event)) { 
     switch (event.type) { 
      case sf::Event::Closed: 
       window.close(); 
       break; 
      case sf::Event::MouseButtonPressed: 
       if (event.mouseButton.button == 0) { 
        moving = true; 
        oldPos = window.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)); 
       } 
       break; 
      case sf::Event::MouseButtonReleased: 

       if (event.mouseButton.button == 0) { 
        moving = false; 
       } 
       break; 
      case sf::Event::MouseMoved: 
       { 
        if (!moving) 
         break; 

        const sf::Vector2f newPos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y)); 

        const sf::Vector2f deltaPos = oldPos - newPos; 
        view.setCenter(view.getCenter() + deltaPos); 
        window.setView(view); 



oldPos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y)); 
        break; 
       } 
} 
    } 

    window.clear(sf::Color::White); 
    window.draw(bd); 
    window.draw(background); 

    window.display(); 
} 
} 

また、私は適切な洞察と適切な説明でそのような例を見つけることができた提案を聞きたいと思います。前もって感謝します。

+0

あなたの質問が何であるかは不明です。私は動く景色についての質問に答えました。複数の画像(背景画像を含まない)がある場合は、クリックとzオーダーを確認するか、別の方法で画像を選択する必要があります。 1、2、3などのキーを押して、この画像だけを移動します。 –

答えて

0

問題は、ビューではなくスプライト自体が動いているということです。

view.setCenter(view.getCenter() + deltaPos); 
window.setView(view); 

代わりにあなたがスプライトを移動する必要があります。上の2行をこの行と交換すれば正常に動作するはずです

bd.setPosition(bd.getPosition() - deltaPos); 
関連する問題