This site requires JavaScript, please enable it in your browser!
Greenfoot back
DMCGames
DMCGames wrote ...

2012/1/6

Mouse Right Click

DMCGames DMCGames

2012/1/6

#
I am having trouble using the right click of the mouse... Whenever I use the method mouse.getButton() == 3, it gives me an error: java.lang.NullPointerException at Steve.checkKeys(Steve.java:27) at Steve.act(Steve.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) and whenever I try to add a object after another one got removed
(getWorld().removeObject(this); getWorld().addObject( new Ground(), 250,10);
it doesn't appear. I am very confused right now...... Please and Thank you for your help.
danpost danpost

2012/1/6

#
I cannot say much about the NullPointerException error without more info. But as far as Remove and Add, I can tell you that 'getWorld().addObject(...)' after 'getWorld().removeObject(this);' will never work, because there is no world to reference anymore (after deleting 'this'). Easy fix, Add before Remove. Maybe, both problems are related. After fixing the later (which was possibly creating the NullPointerException), the first may go away.
davmac davmac

2012/1/6

#
Whenever I use the method mouse.getButton() == 3, it gives me an error: java.lang.NullPointerException
In that case, "mouse" must be null. You should check that it is not null before calling getButton() on it.
DonaldDuck DonaldDuck

2012/1/7

#
MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse != null) { //your code here with the switch Dan mentioned }
DMCGames DMCGames

2012/1/8

#
When I right click it doesn't do anything....
MouseInfo mouse = Greenfoot.getMouseInfo();
if(mouse != null)
{
    if (mouse.getButton() == 3)
       {
             setLocation(mouse.getX(), mouse.getY());
       }
}
danpost danpost

2012/1/8

#
I just copy/pasted the code in your last post to the act() method of an Actor and ran the scenario. Right-clicking summoned the actor to the mouse as required. There must be something more to your code that is being overlooked. Are you possibly using 'getMouseInfo()' earlier in the method?
DMCGames DMCGames

2012/1/8

#
hmmm... When I try to add an object using that method it doesn't work, but when sending it to another location, it works. Well umm.. How do you move just one of them not all?
danpost danpost

2012/1/8

#
In the world class, you could use 'getObjects(null).get(Greenfoot.getRandomNumber(numberOfObjects() - 1)).setLocation(mouse.getX(), mouse.getY());', but you will have to 'import java.util.List;' to use the method 'getObjects(class)' since it returns a List object. If you wanted to randomly choose an Actor to add, use
addObject(getRandomActor(), mouse.getX(), mouse.getY());
and create the method
private Actor getRandomActor()
{ 
    switch (Greenfoot.getRandomNumber(3)) // replace '3' with number of different objects you can add
    {
        case 0: return (new Object1());
        case 1: return (new Object2());
        case 2: return (new Object3());
        //  continue with number of objects
    }
    return null; // execution should never reached this statement, but needed to compile
}
davmac davmac

2012/1/8

#
In the world class, you could use 'getObjects(null).get(Greenfoot.getRandomNumber(numberOfObjects() - 1)).setLocation(mouse.getX(), mouse.getY());', but you will have to 'import java.util.List;' to use the method 'getObjects(class)' since it returns a List object.
Actually, if you chain the method calls (as you do here) there is no need to import the List interface. You only need to import List if you refer to it directly, eg declare a variable of type List.
You need to login to post a reply.