ImCoding2907
ImCoding2907

Reputation: 11

How can I call variables and functions from another class inside of a scene?

I'm making a little prototype game that uses extending classes (not sure what this is called) and I'm trying to detect the collision between the player and the ground blocks. My problem is that processing says that 'player' can't be resolved to a variable. Because of this I can't use any variable from the Player class to detect any sort of collision.

If you have any questions you need answered in order to help me I will answer them gladly.

This here is the PlayScene class

public class PlayScene extends GameScene {
  public WorldBlocks blocks;
  public Player player;

  public PlayScene() {
    blocks = new WorldBlocks();
    player = new Player();
  }

  public void Update() {
    if (!blocks.levelLoaded) {
      blocks.spawnGroundBlocks();
      player.spawnPlayer();
    }
    blocks.updateGroundBlocks();
  }

  public void Draw() {
    background(255);
    blocks.drawGroundBlocks();
  }
}

This is the player class

class Player {
  public float x, y, xVelocity, yVelocity, playerSize;
  public boolean groundCollision;

  void spawnPlayer() {
    x = width/25;
    y = height - height/3;
    xVelocity = 0;
    yVelocity = 0;
    playerSize = width/35;
  }

  void updatePlayer() {

  }
}

Here is the WorldBlocks class (the problem is separated)

class WorldBlocks {
  private final int NUMBER_OF_BLOCKS = 100;
  private boolean levelLoaded = false;

  private float spawnPosX;
  private float spawnPosY;
  public final float BLOCK_SIZE = width/30;

  public float []x = new float[NUMBER_OF_BLOCKS];
  public float []y = new float[NUMBER_OF_BLOCKS];
  private float []blockColor = new float[NUMBER_OF_BLOCKS];

  void spawnGroundBlocks() {
    spawnPosX = -(BLOCK_SIZE/2);
    spawnPosY = height - 1.5 * BLOCK_SIZE;

    for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
      if (spawnPosX < width + BLOCK_SIZE/2 && spawnPosY < height) {
        x[i] = spawnPosX;
        y[i] = spawnPosY;
        blockColor[i] = random(100, 155);
        spawnPosX += BLOCK_SIZE;

        if (spawnPosX > width && spawnPosY < height - BLOCK_SIZE/2) {
          spawnPosX = -BLOCK_SIZE/2;
          spawnPosY = height - BLOCK_SIZE/2;
        }
      } else {
        spawnPosX = 0;
        spawnPosY = -height;
        blockColor[i] = random(200, 255);
      }
    }
    levelLoaded = true;
  }

  void updateGroundBlocks() {
    for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
  if (player.y + player.playerSize/2 > y[i] - BLOCK_SIZE/2) { //This is the problem statement
    collision = true;
  }
      if (x[i] < - BLOCK_SIZE/2) {
        x[i] += width + BLOCK_SIZE;
      }
      if (x[i] > width + BLOCK_SIZE/2) {
        x[i] -= width + BLOCK_SIZE;
      }
    }
  }

  void drawGroundBlocks() {
    for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
      fill(blockColor[i]);
      square(x[i], y[i], BLOCK_SIZE);
    }
  }
}

Upvotes: 1

Views: 50

Answers (1)

micycle
micycle

Reputation: 3820

Extending classes is suitably called inheritance.


In your code:

  • Instances of the PlayScene class each have a Player.

  • Player is not defined within the WorldBlocks class.

Therefore, when you reference a player instance (with player.y and similar) within the WorldBlocks class, there's nothing to find. This is what processing means by 'player' can't be resolved to a variable — it can't find a Player object within the scope of the WorldBlocks class.


To call methods on a player within the WorldBlocks class, it needs a reference to a Player instance.

One way to supply this reference is via the constructor; modify the WorldBlocks class as such:

class WorldBlocks {

    private player;

    public WorldBlocks(Player player) {
           this.player = player;
    }
}

Modify the PlayScene constructor as such:

public PlayScene() {
    player = new Player();
    blocks = new WorldBlocks(player);
}

Now when you refer to the player (player.y) within the collision loop of the WorldBlocks class it knows what to reference -- it will call the private player object belonging to that WorldBlocks instance.

Upvotes: 1

Related Questions