lacking return type?!

apparently this is lacking a return type, I can’t see how I can only return true if I have to place a return outside the if block, which is what the exception seems to be suggesting :S anyhow here’s the code any help would be appreciated:

public boolean hitTarget (int x, int y) {
         if ((x>= this.boxX)&&(x<= (this.boxX +this.boxW))&&
                 (y>= this.boxY)&&(y<=this.boxY + this.boxH)) {
             return true;
         
         }//endIf
}

and if the “if” is false, what would you return, eh?

I think what you meant to do was:


return (x>= this.boxX)&&(x<= (this.boxX +this.boxW))&&
                 (y>= this.boxY)&&(y<=this.boxY + this.boxH);

Cas :slight_smile:

so that would return true, without an if block?

In a conditional statement like if, the statement “((x>= this.boxX)&&(x<= (this.boxX +this.boxW))&& (y>= this.boxY)&&(y<=this.boxY + this.boxH))” would be evaluated to a boolean. So if its true it will run the statement “return true”, but you are not telling the compiler what to do if the statement is false, returning false seems like the obvious solution.

Because all your doing then is returning whatever the the condition evaluates to, then just skip the if all together and return the evaluation like Cas pointed out.

cheers, that makes a lot of sense and saves a lot of time and headache :slight_smile:

also, just using return instead of an if block, solves the exception

For the sake of completeness, you could also have solved this by:

public boolean hitTarget (int x, int y) {
         if ((x>= this.boxX)&&(x<= (this.boxX +this.boxW))&&
                 (y>= this.boxY)&&(y<=this.boxY + this.boxH)) {
             return true;
         
         }//endIf
         return false; // just to make sure all code branches return a value
}

or by:

public boolean hitTarget (int x, int y) {
         if ((x>= this.boxX)&&(x<= (this.boxX +this.boxW))&&
                 (y>= this.boxY)&&(y<=this.boxY + this.boxH)) {
             return true;
         
         } else {//endIf
            return false; // just to make sure all code branches return a value
         }
}

My personal preference would be:


public boolean hitTarget (int x, int y) {
         return x >= this.boxX &&
                  x < this.boxX +this.boxW &&
                  y >= this.boxY &&
                  y < this.boxY + this.boxH;
}

No nasty parentheses, and (assuming no runtime compiler optimisation) more efficient too.

More importantly though, i’ve corrected your boundary logic.
By definition ObjectA.X+ObjectA.Width is the first coordinate falling outside ObjectA.
Therefore your right edge and bottom edge comparison operators should be “<” not “<=”.

To be honest if this problem is anything more than a learning excercise you are already doing it wrong, as you are reinventing the wheel.
Use java.awt.Rectangle (abstracted through either java.awt.Rectangle2D or java.awt.Shape)

cheers for the optimisation, yer, this is a learning excercise. I’m learning with an O’Reilly book on java game programming and to get certain aspects concrete in my head I’m editing (and adding to) one of their example codes.

would java.awt.rectangle work as it’s checking the boundaries for a jPanel, it seems a little wierd to create an abstracted rectangle when I already have the reference to the jPanel, am I just being a complete noob or does that make sense.

and for the record yes I am a noob, but you guys are friendly so I don’t mind it being obvious =3

If it’s checking the boundaries for a JPanel then you can use the getBounds() method, which returns a Rectangle object.


public boolean hitTarget(int x, int y) {
    return panel.getBounds().contains(x,y);
}

However, may I ask why you are using a JPanel for an entity?

because I got mixed up and I’m not lol :wink:

I’m checking the boundaries of a “drawn” rectangle