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

2018/2/12

Trying to write a proper collision detection

Xolkiyr Xolkiyr

2018/2/12

#
And apparently I keep getting this nullpointer exception:
java.lang.NullPointerException
	at Iron.checkHitBox(Iron.java:133)
	at Iron.act(Iron.java:56)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Here's the pertinent code: The CheckHitBox code-
/**
     * Making sure the mouse is within the bounds of the ore.
     */
    private boolean checkHitBox(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(ore < 11){
            return false;
        }
        if(
            mouse.getX() >= this.getX() && 
            mouse.getY() >= this.getY() &&
            mouse.getX() <= this.getX()+SIZE && 
            mouse.getY() <= this.getY()+SIZE
        ){
            return true;
        }else{
            return false;
        }
    }
The act code:
/**
     * Tell us how much ore is left in this vein.
     */
    public void act(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        String oreDisp;
        if(ore > 999){
            oreDisp = String.format("%.02f",(ore/1000)) + "k";
        }else{
            oreDisp = Integer.toString((int) ore);
        }
        if (checkHitBox() && info == null) 
        {
            info = new Label("\n  Iron: " + oreDisp + "  \n ", 24);
            getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
        }
        else if (checkHitBox() == false)
        {
            getWorld().removeObject(info);
            info = null;
        }else if (Greenfoot.isKeyDown("f") && checkHitBox()){
            getWorld().removeObject(info);
            info = null;
            info = new Label("\n  Iron: " + oreDisp + "  \n ", 24);
            getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
        }
    }
danpost danpost

2018/2/12

#
In the 'checkHitBox' method, 'mouse' will be 'null' if no mouse action is detected. You will probably have to keep track of either what coordinate values the mouse is at (in world) or on what ore object it is on (in world or ore class).
Xolkiyr Xolkiyr

2018/2/12

#
Okay, I thought it was something simple like that I was overlooking. so basically:
if(mouse == null){
     return false;
}
Xolkiyr Xolkiyr

2018/2/12

#
Yup, that fixed it. Thanks danpost.
danpost danpost

2018/2/12

#
Xolkiyr wrote...
Okay, I thought it was something simple like that I was overlooking. so basically:
if(mouse == null){
     return false;
}
Unfortunately, that will cause your info box to be intermittent. You would have to continually move the mouse over the vein to be able to see the info box long enough to read it (not pleasant).
Xolkiyr Xolkiyr

2018/2/12

#
That's the way the code is supposed to be, you have to be hovering over the ore to be able to see how much is there. and it even updates as you're mining.
danpost danpost

2018/2/12

#
Xolkiyr wrote...
That's the way the code is supposed to be, you have to be hovering over the ore to be able to see how much is there. and it even updates as you're mining.
Okay -- glad to know you are happy with it.
You need to login to post a reply.