[英]Mouse position and click detect in sfml
int Game::MouseOnDot(float x, float y, RenderWindow &renderWindow) {
Rect<int> Dot;
Event event;
Dot.left = x;
Dot.top = y;
Dot.width = 20;
Dot.height = 20;
while (renderWindow.pollEvent(event)) {
float Mx = sf::Mouse::getPosition().x;
float My = sf::Mouse::getPosition().y;
if (event.type == Event::MouseButtonReleased&&Mx > x && Mx < Dot.height && My > y && My < Dot.width){
return 1;
}
else
return 0;
}
}
我不知道為什么這不起作用,如果按下按鈕它返回1,告訴其他功能關閉窗口。 我在鼠標位置上做錯了嗎?
while (renderWindow.isOpen()) {
processEvents(renderWindow);
float Time = clock.getElapsedTime().asSeconds();
float TimeDifference = Time - LastUpdateTime;
if (TimeDifference >= UpdateTime) {
processEvents(renderWindow);
y += 3;
if (y <= 770) {
if(Game::MouseOnDot(x, y, renderWindow)==1)
renderWindow.close();
Game::Spawn(renderWindow, Green_Dots, x, y);
LastUpdateTime = Time;
return;
仍然不工作我粘貼在這里的部分MouseOnDot reutrns 0或1.它不會關閉窗口,我不知道為什么??
使用sf :: Mouse :: getPosition()。x返回相對於桌面的位置,如果你想要它相對於你需要做的renderWindow: sf :: Mouse :: getPosition(renderWindow).x
然后Attila完全正確的鼠標/點對比:)
我認為你的問題是你將位置與x坐標和高度進行比較。 你需要比較x和x +高度(類似於y坐標/寬度)
嘗試:
if (event.type == Event::MouseButtonReleased &&
Mx > x && Mx < x + Dot.height &&
My > y && My < y + Dot.width) {
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.