【发布时间】:2009-10-28 17:48:23
【问题描述】:
我想要一个游戏,当鼠标到达窗口的外边缘时,视图会四处移动(类似于许多 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 到底有多少开销?如果只需要沿着游戏窗口的边界进行检测,这种方法或类似方法可能会更有效吗?
注意:这将用于窗口模式以及可能的全屏应用程序。
【问题讨论】:
标签: java optimization mouse