我想要一個遊戲,當鼠標到達窗口的外邊緣時,視圖將四處移動(類似於許多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