获取对象以在Java / Greenfoot中跟踪鼠标

时间:2015-02-14 07:13:53

标签: java greenfoot

我刚刚开始使用greenfoot上学,我想创建一个像这样的游戏:http://www.scirra.com/arcade/action/455/squared但我无法让演员/对象跟随我的鼠标。就像上面提到的游戏中的黑色方块一样。到目前为止,我已尝试过此代码,但无济于事。

MouseInfo mouse = Greenfoot.getMouseInfo();
        setLocation(mouse.getX(), mouse.getY());
        if (mouse != null)
        {
        setLocation(mouse.getX(), mouse.getY());
        }

2 个答案:

答案 0 :(得分:0)

getMouseInfo

public static MouseInfo getMouseInfo()返回一个鼠标信息对象,其中包含有关鼠标状态的信息。

返回:有关鼠标当前状态的信息,如果鼠标光标位于世界边界之外,则返回null(除非被拖动)。 基本上,这意味着当调用该方法时,如果超出边界,则使用鼠标。

为了帮助您更多,请描述您希望实现的目标。

答案 1 :(得分:0)

我在Google上搜索答案时发现了这一点,它展示了如何让对象跟随你的鼠标。 http://www.datraughber.com/prog1/greenfoot/unit3.pdf

原始代码:

    MouseInfo mouse = Greenfoot.getMouseInfo();
    setLocation(mouse.getX(), mouse.getY());

    if (mouse != null)
    {
    setLocation(mouse.getX(), mouse.getY());
    }

新代码

if(Greenfoot.mouseMoved(null))
        {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                setLocation(mouse.getX(),mouse.getY());
        }

感谢您尝试帮助Ajay Venugopal