如何在一个选项卡中将程序分成几个类

时间:2016-03-15 14:48:39

标签: java class processing

我在使用java语言处理时创建了一个简单的游戏,但是我在一个选项卡中创建了游戏,现在我需要将所有内容放入自己的类中,因此它看起来很有条理并且更容易阅读,但是我之前创建了类我正在努力将我的代码放入单独的类中并让它们工作。以下是我的一个课程

的示例
int pixelsize = 5;   // size of pixels on screen
int gridsize  = pixelsize * 8 + 5;
Player player;
ArrayList enemies = new ArrayList();
ArrayList bullets = new ArrayList();
int direction = 1; // where the wave moves to 
boolean incy = false;

void setup() {
    background(0);
    fill(255);
    size(800, 800);
}

void draw() {
    background(0);

    player.draw();

    for (int i = 0; i < bullets.size(); i++) {
        Bullet bullet = (Bullet) bullets.get(i);
        bullet.draw();
    }

void createEnemies() {
    for (int i = 0; i < width/gridsize/2; i++) {
        for (int j = 0; j <= 4; j++) {
            enemies.add(new Enemy(i*gridsize, j*gridsize + 20 ));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

第1步:点击此按钮:

第2步:点击新标签选项。为选项卡指定一个与该选项卡中所需类的名称相匹配的名称。我将使用Ball

第3步:在该标签中为您的课程编写代码:

class Ball {

  float x;
  float y;

  void setPosition(float x, float y) {
    this.x = x;
    this.y = y;
  }

  void drawBall() {
    fill(ballColor);
    ellipse(x, y, 25, 25);
  }
}

第4步:在第一个标签中写下草图的代码:

Ball ball = new Ball();

color ballColor = #00ff00;

void setup(){
  size(500, 500);
}

void draw(){
  background(0);
  ball.setPosition(mouseX, mouseY);
  ball.drawBall();
}

请注意,此草图可以在其他选项卡中使用该类,并且该类可以使用草图选项卡中定义的函数或变量。

可以在the Processing reference找到更多信息。