[英]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;
}
}
I don't know why this won't work if the button is pressed on the dot it returns 1 which tell other function to close window. 我不知道为什么这不起作用,如果按下按钮它返回1,告诉其他功能关闭窗口。 Am i doing something wrong on mouse position?
我在鼠标位置上做错了吗?
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;
Still don't work i paste here the part when MouseOnDot reutrns 0 or 1. It won't close the windows and i don't know why?? 仍然不工作我粘贴在这里的部分MouseOnDot reutrns 0或1.它不会关闭窗口,我不知道为什么??
Using sf::Mouse::getPosition().x returns the position relative to the desktop, you if you want it relative to your renderWindow you need to do: sf::Mouse::getPosition(renderWindow).x 使用sf :: Mouse :: getPosition()。x返回相对于桌面的位置,如果你想要它相对于你需要做的renderWindow: sf :: Mouse :: getPosition(renderWindow).x
then Attila is exactly right about the mouse/dot comparison :) 然后Attila完全正确的鼠标/点对比:)
I think your problem is that you compare the position to the x coordinate and the height. 我认为你的问题是你将位置与x坐标和高度进行比较。 You need to compare to x and x+height (similarly for y coordinate/width)
你需要比较x和x +高度(类似于y坐标/宽度)
Try: 尝试:
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.