Java 如何将变量导入另一个类?

Java 如何将变量导入另一个类?,java,processing,Java,Processing,我正在尝试为我必须为学校制作的游戏制作一个原型。但进展并不顺利。我的问题是如何将变量从一个类导入到另一个类?我需要playerX和Y变量,以便检查碰撞。这是用处理(Java)编写的 如果有更好的方法检查碰撞,请告诉我:)我没有主意了。提前谢谢 我的代码: 主类 Player thePlayer = new Player(); Guard theGuard = new Guard(); SpeedPWRUP speedPowerUp = new SpeedPWRUP(); void setup(

我正在尝试为我必须为学校制作的游戏制作一个原型。但进展并不顺利。我的问题是如何将变量从一个类导入到另一个类?我需要playerX和Y变量,以便检查碰撞。这是用处理(Java)编写的

如果有更好的方法检查碰撞,请告诉我:)我没有主意了。提前谢谢

我的代码:

主类

Player thePlayer = new Player();
Guard theGuard = new Guard();
SpeedPWRUP speedPowerUp = new SpeedPWRUP();

void setup() {
  size(1000, 500);
  theGuard.init();
  thePlayer.init();
  speedPowerUp.init();
}

void updateGame() {
  theGuard.update();
  thePlayer.update();
  speedPowerUp.update();
}

void drawGame() {
  thePlayer.draw();
  theGuard.draw();
  speedPowerUp.draw();
  fill(color(255, 255, 255));
  text("Score:", 10, 20);
}

void draw() {
  background(255);
  fill (0, 0, 0);
  rect(-10, 401, 1100, 100);
  noFill();

  updateGame();
  drawGame();
}
警卫级

class Guard {
  public float guardX, guardY;
  float guardVX, guardVY;
  int fillColor;
  float guardHeight, guardWidth;

void init() {
   guardHeight = 80;
   guardWidth = 40;
   guardX = 300;
   guardY = 400-guardHeight;
   fillColor = color(255,0,0);
}

void update() {
  if (guardX == (random(width)-100)) 
    guardVX = 3;

  if (guardX == (random(width)+100)) 
    guardVX = -3;

  guardX += guardVX;
  guardY += guardVY;
}

void draw() {
  fill(fillColor);
  rect(guardX, guardY, guardWidth, guardHeight);
  noFill();
}
}
球员级别

class Player {
  public float playerX, playerY;
  float vx, vy;
  int fillColor;
  float playerHeight, playerWidth;
  float jumpTime;
  float jumpHeight;
  boolean isJumping;

void init() {
  playerHeight = 80;
  playerWidth = 40;
  fillColor = color(0, 0, 0);
  jumpTime = 200;
  jumpHeight = 100;
  isJumping = false;

  playerX = 100;
  playerY = 400-playerHeight;

  vx = 0;
  vy = 0;
}

void update() {

  if (keyPressed) {
    if (key == 'a' || key == 'A') {
      vx = -2;
    }
  } else {
    vx = 0;
  }

  if (keyPressed) {
    if (key == 'd' || key == 'D') {
      vx = 2;
    }
  } else {
    vx = 0;
  }

  if (keyPressed) {
    if ((key == 'w' || key == 'W') && ( playerY > 400 - jumpHeight)) {
      isJumping = true;
    }
  } else {
    isJumping = false;
  }

  if (playerY < 400 - jumpHeight) {
  }

  if (keyPressed) {
    if (key == 's' || key == 'S') {
      playerHeight = 40;
    }
  } else {
    playerHeight = 80;
  }

  if(isJumping == true) {
    vy = -15;
  } else {
    vy = 0;
  }

  if (playerY < (400-playerHeight)) {
  vy = vy + 2.5;
  }

  playerX += vx;
  playerY += vy;
}

void draw() {
  fill(fillColor);
  rect(playerX, playerY, playerWidth, playerHeight);
  noFill();
}
}
职业玩家{
公共花车游乐场;
浮动vx,vy;
int fillColor;
浮球运动员的高度,运动员的宽度;
浮动跳时;
浮动高度;
布尔跳跃;
void init(){
演奏者身高=80;
playerWidth=40;
fillColor=color(0,0,0);
跳时=200;
跳跃高度=100;
isJumping=false;
playerX=100;
playerY=400演奏者的高度;
vx=0;
vy=0;
}
无效更新(){
如果(按键){
如果(键=='a'| |键=='a'){
vx=-2;
}
}否则{
vx=0;
}
如果(按键){
if(key='d'| | key='d'){
vx=2;
}
}否则{
vx=0;
}
如果(按键){
如果((键='w'| |键='w')&&(playerY>400-跳跃高度)){
isJumping=true;
}
}否则{
isJumping=false;
}
if(游戏性<400-跳跃高度){
}
如果(按键){
如果(键=='s'| |键=='s'){
演奏者身高=40;
}
}否则{
演奏者身高=80;
}
如果(isJumping==真){
vy=-15;
}否则{
vy=0;
}
if(演奏家<(400个演奏者){
vy=vy+2.5;
}
playerX+=vx;
游戏性+=vy;
}
作废提款(){
填充(填充颜色);
rect(playerX、playerY、playerWidth、playerHeight);
noFill();
}
}
加速班

class SpeedPWRUP {

  float diameter;
  public float pwrUpX, pwrUpY;
  int fillColor;

  void init() {
    diameter = 40;
    pwrUpX = 100;
    pwrUpY = 100;
    fillColor = color(0, 0, 255);
  }

  void update() {
    if (((playerX>pwrUpX) && (playerX < pwrUpX+diameter)) && (playerY > pwrUpY)&&(playerY>pwrUpY+diameter))
        end();
  }

  void draw() {
    fill(fillColor);
    ellipse(pwrUpX, pwrUpY, diameter, diameter);
    noFill();
  }
}
class-SpeedPWRUP{
浮子直径;
公开浮动pwrUpX,pwrUpY;
int fillColor;
void init(){
直径=40;
pwrUpX=100;
pwrUpY=100;
fillColor=color(0,0255);
}
无效更新(){
如果((playerX>pwrUpX)和(playerXpwrUpY)和(playerY>pwrUpY+直径))
end();
}
作废提款(){
填充(填充颜色);
椭圆(pwrUpX,pwrUpY,直径,直径);
noFill();
}
}

您可以在Player类中使用Getter函数。 例如: 公共浮点getPlayerX(){ 返回playerX; }要检查碰撞:
您可以在主类中定义静态对象:

或球员名单

public static ArrayList<Player> Players = new ArrayList<>();
publicstaticarraylistplayers=newarraylist();

在这里发布之前,请进行一些研究。请只放相关的代码片段。我没有主意了。这里有一个。通过阅读Oracle Java教程或一本好的Java入门教材来学习Java语言的基础知识。是否尝试
thePlayer.playerX
?当然,你最好是用能手/二传手。
public static ArrayList<Player> Players = new ArrayList<>();