【问题标题】:How can I check the mouse's position over a 2D array of images for a game?如何在游戏的二维图像阵列上检查鼠标的位置?
【发布时间】:2015-02-04 00:29:45
【问题描述】:

好的,我正在用 Java 创建一个 Candy Crush 游戏副本。 我的问题在于尝试创建一个事件侦听器以单击或悬停在对象上。我尝试了不同的方法,从使用 ImageIcons 和使用事件侦听器到现在使用 JLabels 和使用图标并尝试确定是否单击了 JLabel。

我现在尝试的是像这样使用 Rectangles 和 hitboxes:

public Rectangle mouseHitBox(MouseEvent e)
{
    int x = (int)e.getLocationOnScreen().getX(); //Grab Mouse X Coordinate and set it as an Int
    int y = (int)e.getLocationOnScreen().getY(); //Grab Mouse Y Coordinate and set it as an Int
    mouse.setBounds(x,y,1,1); //Create Mouse Hitbox

    return mouse; //Return it for Mouse Event
}

public Rectangle gemHitBox()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++) //For loops to access all 81 gem images
        {
            int x = gemImage[i][j].getX(); // Get X coordinate of that specific array destination
            int y = gemImage[i][j].getY(); // Get Y coordinate of that specific array destination
            gem.setBounds(x,y,50,47); // Gem Hitbox
        }
    }
    return gem; //Return it for Mouse Event
}

@Override
public void mouseClicked(MouseEvent e) {

   if(mouseHitBox(e).intersects(gemHitBox())) // If the Mouse Hitbox comes in contact with one of the array positions then
   {
      //Prepare to Swap Gems
   }


}

主类实现了 MouseListener,这就是为什么我可以有一个 public void mouseClicked。

无论鼠标监听器中包含什么代码,它总是抛出这个异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Game.mouseHitBox(Game.java:125) //this line: mouse.setBounds(x,y,1,1);
at Game.mouseClicked(Game.java:147) //this line:  if(mouseHitBox(e).intersects(gemHitBox()))
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我在这个问题上卡了三天,任何建议都会很棒。

【问题讨论】:

    标签: java mouseevent multidimensional-array


    【解决方案1】:

    你有没有检查过 mouseHitBox 返回的东西不是 null 吗?

    堆栈跟踪似乎表明此方法在这种情况下返回 null。

    这种情况下鼠标初始化了吗?

    【讨论】:

    • 我知道我做了什么,我确实忘记初始化它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多