2012-11-06 5 views
0

mouseX 및 mouseY 위치를 클래스에 전달하는 올바른 구문을 고수하려고합니다.SFML 이벤트에서 마우스 위치 보내기

내 시도는 다음과 같이이었다 :

// Start the game loop 
while (window.isOpen()) 
{ 
    // Process events 
    sf::Event event; 
    while (window.pollEvent(event)) 
    { 
     // Close window : exit 
     if (event.type == sf::Event::Closed) { 
      window.close(); 
     } 
    } 

    // Clear screen 
    window.clear(); 

    // Draw the sprite 
    window.draw(sprite); 
    window.draw(lsprite); 

    if(btn_quit.IsIn(sf::Event::MouseMoveEvent::x,sf::Event::MouseMoveEvent::y){ 
     btn_quit.RenderImg(window,"button_on.png"); 
    } else { 
     btn_quit.RenderImg(window,"button.png"); 
    } 
    ///rest of the code not relevant to this issue 

이 내 수업에 있습니다

bool IsIn(int mouseX, int mouseY) 
{ 
    if (((mouseX > m_x) && (mouseX < m_x + m_w)) 
    && ((mouseY > m_y) && (mouseY < m_y + m_h))) { 
     return true; 
    } else { 
     return false; 
    } 
} 

내가 x와 y의 입력에 오류가 점점 계속하는 방법이 밝히는 적 :

error C2597: illegal reference to non-static member 'sf::Event::MouseMoveEvent::x'

error C2597: illegal reference to non-static member 'sf::Event::MouseMoveEvent::y'

+2

음 ...이 오류 메시지가 말한다 것처럼. x 및 y 멤버가 정적 인 것처럼 액세스하려고 시도했지만 분명히 그렇지 않습니다. – Cubic

+0

'btn_quit.IsIn (event.x, event.y)'를 호출해야하는 것처럼 보입니까? –

답변

3

구조체의 비 정적 멤버 필드에 정적으로 액세스하려고합니다. 너는 그렇게 할 수 없다. 다음과 같이 시도하십시오.

// Start the game loop 
while (window.isOpen()) 
{ 
    // Process events 
    sf::Event event; 
    while (window.pollEvent(event)) 
    { 
     // Close window : exit 
     if (event.type == sf::Event::Closed) { 
      window.close(); 
     } 
     else if (event.type == sf::Event::MouseMove) 
     { 
      if(btn_quit.IsIn(event.MouseMoveEvent.x, event.MouseMoveEvent.x){ 
       btn_quit.RenderImg(window,"button_on.png"); 
      } else { 
       btn_quit.RenderImg(window,"button.png"); 
      } 
     } 
    } 

    // Clear screen 
    window.clear(); 

    // Draw the sprite 
    window.draw(sprite); 
    window.draw(lsprite); 
1

현재 수행중인 작업은 몇 가지면에서 올바르지 않습니다. 우선 x와 y를 정적 데이터로 액세스 할 수 없으며 단순히 존재하지 않습니다.

다음으로 SFML 이벤트는 본질적으로 공용체입니다. 즉, 실제 내용은 처리하려고하는 이벤트 유형에 따라 다릅니다. 최소한 이벤트 객체가 아닌 모든 이벤트에 대해 x 및 y 값을 가져올 수 없습니다. 당신이 마우스 이벤트가있는 경우는 다음과 같이 뭔가를 할 수 있습니다 :

if(event.type == sf::Event::MouseMoved) 
{ 
    // do something with event.mouseMove.x and event.mouseMove.y 
} 

을 양자 택일로, 당신은 mouseMoved 이벤트 범위를 벗어난이 작업을 수행하려는 경우, 당신은 항상 Mouse 클래스를 사용할 수 있습니다. 이 xy를 포함하는 2D 벡터를 반환하는 getPosition 방법을 포함

sf::Mouse::getPosition(); //Absolute coordinates 
sf::Mouse::getPosition(window); //Relative to window