Having issues on using INT variables from one class two another.

edited November 2016 in Questions about Code

Long story short I have three class, two of which have a variable named playerLocX, and botLocX. Simply put the player can control it's LocX through w, a, s, d. Meanwhile the bot is entirely randomized...

I'm trying to bring playerLocX from the player class, and botLocX from the bot class into my main class where I have a method that detects intersection

I'm also doing the same thing for the Y axis, as these two objects are rects()

example of my player class (Bot is done similarly)

class Player(){
        int locX;

  Player(){
        locX;
  }

  void control(){
  (Does all w, a, s, d movement keyPressed if statements)
  }

  void display(){
        control();
        rect(locX, locY, 50, 50);
  }
}

My main class

(I also have display from Player and bot called so that they're visible on screen)
outside  > Player p1;
in setup >  p1 = new Player();
in draw > p1.display();


void intersection(){
(If statements that if Player.locX (locY) intersects with bot.locX (locY) trigger game over)
This is where I'm having issues....

p1.locX doesn't work (and I know it obviously isn't correct, I just don't know the correct way

Answers

  • edited November 2016

    Define a base class for both your Player and Bot classes.
    Then have a method there in order to check collision:

    class Entity {
      int x, y, w, h;
    
      Entity(int _x, int _y, int _w, int _h) {
        x = _x;
        y = _y;
        w = _w;
        h = _h;
      }
    
      void display() {
        rect(x, y, w, h);
      }
    
      boolean isColliding(Entity e) {
        return x+w >= e.x & x < e.x+e.w & y+h >= e.y & y < e.y+e.h;
      }
    }
    
    class Player extends Entity {
      Player(int _x, int _y, int _w, int _h) {
        super(_x, _y, _w, _h);
      }
    }
    
    class Bot extends Entity {
      Bot(int _x, int _y, int _w, int _h) {
        super(_x, _y, _w, _h);
      }
    }
    

    Also check it out this link to learn more about collisions in general: O:-)
    http://JeffreyThompson.org/collision-detection/rect-rect.php

  • Thank you very much for your reply. I'm still learning processing at the moment so I really appreciate all the help I can get.

    I do have some more questions at your response though...

    1 - I can still modify the location of these two entities in their respective classes without one interfering with another right? because I need to have the player move by WASD, and the bot will move randomly and in random speeds on a timer. (The concept is to get from one side of the grid to the other while avoiding the bot).

    2 - If I'm trying to test for their collision while they're moving that still works right? just put the isColliding on a loop();?

    3 - calling the isColliding(entity e) what goes in entity e when I call it? I also assume in my main class I'm going to have

    if(entityObj.isColliding == true){ (Trigger game over) }

    and because it's on a loop() it will continuously run in that method, and putting that if statement in draw() will continually check.

    Am I correct?

Sign In or Register to comment.