Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  lacking return type?!  (Read 1265 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2011-06-06 10:43:30 »

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:

1  
2  
3  
4  
5  
6  
7  
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
}

Offline princec

JGO Kernel


Medals: 390
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2011-06-06 10:54:43 »

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

I think what you meant to do was:
1  
2  
return (x>= this.boxX)&&(x<= (this.boxX +this.boxW))&&
                 (y>= this.boxY)&&(y<=this.boxY + this.boxH);


Cas Smiley

Offline westloar

Junior Member





« Reply #2 - Posted 2011-06-06 10:56:33 »

so that would return true, without an if block?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoto

Senior Member


Medals: 4



« Reply #3 - Posted 2011-06-06 11:46:25 »

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.
Offline westloar

Junior Member





« Reply #4 - Posted 2011-06-06 12:55:44 »

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

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

JGO Ninja


Medals: 50



« Reply #5 - Posted 2011-06-06 14:30:34 »

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:

1  
2  
3  
4  
5  
6  
7  
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
}



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

1  
2  
3  
4  
5  
6  
7  
8  
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
}


Mathias - I Know What [you] Did Last Summer!
Offline biro

Junior Member


Medals: 1
Projects: 1



« Reply #6 - Posted 2011-06-06 14:57:00 »

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:

1  
2  
3  
4  
5  
6  
7  
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
}



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

1  
2  
3  
4  
5  
6  
7  
8  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
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
        }
}

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #7 - Posted 2011-06-06 15:08:10 »

My personal preference would be:

1  
2  
3  
4  
5  
6  
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)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline westloar

Junior Member





« Reply #8 - Posted 2011-06-06 15:15:50 »


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
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-06-06 19:52:57 »

If it's checking the boundaries for a JPanel then you can use the getBounds() method, which returns a Rectangle object.
1  
2  
3  
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?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline westloar

Junior Member





« Reply #10 - Posted 2011-06-07 19:06:08 »

because I got mixed up and I'm not lol Wink

I'm checking the boundaries of a "drawn" rectangle
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 07:13:29

Pippogeek (30 views)
2014-09-24 07:12:22

Pippogeek (19 views)
2014-09-24 07:12:06

Grunnt (45 views)
2014-09-23 05:38:19

radar3301 (27 views)
2014-09-21 14:33:17

BurntPizza (63 views)
2014-09-20 17:42:18

BurntPizza (33 views)
2014-09-20 16:30:30

moogie (41 views)
2014-09-20 15:26:15

UprightPath (50 views)
2014-09-20 11:14:06

BurntPizza (54 views)
2014-09-18 18:14:18
List of Learning Resources
by Longor1996
2014-08-16 01:40:00

List of Learning Resources
by SilverTiger
2014-08-05 10:33:27

Resources for WIP games
by CogWheelz
2014-08-01 07:20:17

Resources for WIP games
by CogWheelz
2014-08-01 07:19:50

List of Learning Resources
by SilverTiger
2014-07-31 07:29:50

List of Learning Resources
by SilverTiger
2014-07-31 07:26:06

List of Learning Resources
by SilverTiger
2014-07-31 02:54:12

HotSpot Options
by dleskov
2014-07-07 16:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!