我目前正在學習SFML,並試圖弄清楚如何在pollevent
部分的bposX
和bposY
中打印點擊區域的位置。氏s是我的代碼:使用SFML獲取點擊位置
sf::RectangleShape smallRectangle (float width = 1.0f, float height = 1.0f,
sf::Vector2f position = sf::Vector2f(1.0f,1.0f),
const sf::Color& myFillColor = sf::Color(0,51, 51)
)
{
sf::RectangleShape sboard;
sf::Vector2f myRectangleSize(width, height);
sboard.setSize(myRectangleSize);
sboard.setPosition(position);
sboard.setFillColor(myFillColor);
return sboard;
}
case sf::Event::MouseButtonPressed:
switch(event.mouseButton.button)
case sf::Mouse::Left:
{
float bposX = sboard.getPosition().x;
float bposY = sboard.getPosition().y;
cout << "Current Position : ";
cout << bposX << " " << bposY << endl;
break;
}
for(int i=0; i < sgridSize; i++)
{
for (int j = 0; j < sgridSize; j++)
{
window.draw(sgrid[i][j]);
sf::FloatRect sgridBounds = sgrid[i][j].getGlobalBounds();
if(sgridBounds.contains((sf::Vector2f)pointerPos))
{
shighlightCell.setPosition(sgrid[i][j].getPosition());
window.draw(shighlightCell);
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sgrid[i][j].setFillColor(sf::Color::Green);
}
}
}
}
當我在矩形點擊它僅顯示1 1,根據我Vector2f位置。
很酷。問題是什麼? – Conduit 2014-09-05 17:24:44
當我點擊矩形時,它應該顯示在cmd中的點擊位置。相反,它只顯示我的Vector2f值的位置。併爲我的壞英語感到抱歉。 – 2014-09-05 17:29:28
[奇怪的SFML行爲]的可能的重複(http://stackoverflow.com/questions/25149152/strange-sfml-behavior) – Hiura 2014-09-05 17:51:43