我想要一个游戏,当鼠标到达窗口的外边缘时,视图将四处移动(类似于许多RTS游戏)。我读过使用MouseMotionListener时会产生大量开销。鼠标移动优化
是否有可能具有游戏窗口(的JPanel)不影响游戏播放内的第二透明部件的一种方式,但当鼠标离开经由MouseAdapter.mouseEntered()/的mouseExited内组件将寄存器() ?
boolean mouseOnScreen;
boolean mouseWithinInnerComponent; //is (10 <= mouse.x <= screenWidth - 10) && (10 <= mouse.y <= screenHeight)
if(mouseOnScreen && !mouseWithinInnerComponent)
{
//do something...
}
我在对于如何确定哪个屏幕边界,而无需具有上述部件在角部重叠的四到形成屏幕周围的边框能够检测鼠标是否在被穿越一个损失任何边缘或角落。这是我想象的要相当昂贵(其同时运行游戏,检查每个部件)...
boolean mouseOnScreen;
boolean mouseWithinTopComponent; //is (0 <= mouse.y <= 10)
boolean mouseWithinBottomComponent; //is (screenHeight - 10 <= mouse.y <= screenHeight)
boolean mouseWithinLeftComponent; //is (0 <= mouse.x <= 10)
boolean mouseWithinRightComponent; //is (screenWidth - 10 <= mouse.x <= screenWidth)
if(mouseOnScreen)
{
if(!mouseWithinBottomComponent)
{
//move view up
}
if(!mouseWithinTopComponent)
{
//move view down
}
if(!mouseWithinLeftComponent)
{
//move view right
}
if(!mouseWithinRightComponent)
{
//move view left
}
}
到底有多少开销的MouseMotionListener存在?如果检测只需要沿着游戏窗口的边界进行,那么这个或类似的方法可能会更有效率吗?
注意:这将用于窗口模式以及可能的全屏应用程序。
非常好的算法描述 – 2009-10-28 07:08:32