Reputation: 479
Maybe I'm crazy, but I want to incorporate some logic I learned in my XNA Game Development class into android/java game programming. For example, I'm using Point rather than Vector2 to manipulate the location of graphics programmatically, and adjust graphic size based on an Android device's dpi and orientation.
I thought finding the center of the graphic would be a great asset when laying out the GUI and determining which folder (hdpi, ldpi, or mdpi) the graphics should be taken from. Unfortunately, Eclipse seems to have a problem with the plus sign. This is interesting because I am trying to perform a simple mathematical operation.:
The operator + is undefined for the argument type(s) android.graphics.Point, android.graphics.Point
//Java code was both copied & pasted as well as written to make sure Visual Studio
public Point Center()
{
return new Point(_frameWidth/2, _frameHeight/2)+_location;
}
//Original XNA code
//Determines the coordinates of a sprite's center
public Vector2 Center
{
get
{
return _location + new Vector2(_frameWidth / 2, _frameHeight / 2);
}
}
Upvotes: 0
Views: 155
Reputation:
It's not an issue with Eclipse; Java does not have operator overloading (besides for a very small set of cases).
Why doesn't Java offer operator overloading?
An interesting article on this:
http://java.dzone.com/articles/why-java-doesnt-need-operator
Upvotes: 0
Reputation: 160291
And it's true; you can't add arbitrary types in Java--the error message is correct.
You get some syntactic sugar for string concatenation, and that's it--anything else you add had better be a primitive or mappable object wrapper for the same (e.g., I'm looking at you, BigDecimal
, no +
).
Whatever _location
is, if you're trying to add it to a Point
or Vector2
, it won't work using +
. You may add individual components, use vector/point methods or utility libraries, etc.
Upvotes: 2