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返回0或1时,我仍然不工作,我粘贴在这里的部分。它不会关闭窗口,我不知道为什么??
发布于 2013-04-01 18:58:37
使用sf::Mouse::getPosition().x返回相对于桌面的位置,如果需要相对于renderWindow,则需要执行以下操作: sf::Mouse::getPosition(renderWindow).x
那么Attila关于鼠标/点的比较是完全正确的:)
发布于 2012-06-09 15:44:03
我认为你的问题是,你将位置与x坐标和高度进行比较。您需要比较x和x+height (与y坐标/宽度类似)
尝试:
if (event.type == Event::MouseButtonReleased &&
Mx > x && Mx < x + Dot.height &&
My > y && My < y + Dot.width) {
//...
}
https://stackoverflow.com/questions/10962270
复制