Using variable for x, y coordinates in GRect? (Java newbie)
I'm just learning Java, and I am working with a GRect object. I'm trying to assign the x and y coordinates to an int, but not having luck. I'm not sure what I'm missing?
Specifically, I'm looking at the getHeight():
Basically, I've got
add(new GRect(moveRight,getHeight(), 开发者_如何学CBRICK_WIDTH, BRICK_HEIGHT));
That is working fine, but I assigned getHeight to an int, and then it doesn't work:
int displayHeight = getHeight();
add(new GRect(moveRight,displayHeight,BRICK_WIDTH,BRICK_HEIGHT));
Any ideas?
To clarify-from what I have gathered from these online classes, getHeight() will return the height of the graphics window if it isn't assigned to a class.
With the above, I'm trying to get the GRect to start at the bottom of the graphics window...as I said, when having the first example above, the rectangles move correctly, in the second example, they remain at the top of the display.
Thanks! Joel
The problem could be that getHeight() is returning a double that has a value <1.0, if it does you may be losing your information and being stuck with a 0. Try changing int into double and see if that makes any difference.
what's the return type of getHeight()?
It's hard to say without a lot of context. Obviously my earlier guess was not good (not a compilation error). It seems getHeight()
returns an int.
Do you know if x=0, y=0
point is the top left corner or the bottom left corner? I think it's possible you need to call:
int y = getHeight() - BRICK_HEIGHT;
add(new GRect(moveRight,y,BRICK_WIDTH,BRICK_HEIGHT));
That does not explain why the first instance works, though...
The problem ended up being that I was not properly declaring the variable within the method. Thanks for all your help!
精彩评论