我有一個主類遊戲名爲打磚塊的訪問Veriables從另一個類
public class BrickBreaker extends Activity {
// there is lot of other code but i am only pointing to the issue
class BreakoutView extends SurfaceView implements Runnable {
// The size of the screen in pixels
int screenX;
int screenY;
// Get a Display object to access screen details
Display display = getWindowManager().getDefaultDisplay();
// Load the resolution into a Point object
Point size = new Point();
display.getSize(size);
screenX = size.x;
screenY = size.y;
}}
而且從另一個類(下)我想從主類訪問screenX
:
public class Paddle {
// This the the constructor method
// When we create an object from this class we will pass
// in the screen width and height
public Paddle(int screenX, int screenY){
// 130 pixels wide and 20 pixels high
length = 130;
height = 20;
// Start paddle in roughly the sceen centre
x = screenX/2;
y = screenY - 20;
rect = new RectF(x, y, x + length, y + height);
// How fast is the paddle in pixels per second
paddleSpeed = 550;
}
public void update(long fps){
rect.left = x;
rect.right = x + length;
if (x<0){
x=0;
}
else if (x+length > screenX){
x = screenX-length;
}
}
}
哪有我從Paddle
類訪問screenX
?
通過使'screenX'訪問。可以通過公開,也可以通過爲其提供吸氣劑,並且通過給「BrickPad」對象引用「Paddle」。 – cello
我完全不擅長Java,請告訴我如何使用你解釋的兩種方法使它可訪問。請@cello – Andriopak