"Cannot make a static reference to the non-static field Blue.Pos"

edited March 2018 in Questions about Code

i'm attempting to create a football game for my university project, however when attempting to create collision physics between my players and the ball i get this error. i'm open to a different way of achieving this should you consider it easier.`

The error itself appears in the 'Contact' function held in the Ball class.

thanks in advance for any help provided

    Ball ball;
    Timer startTimer;
    Blue blue;
    Red red;


    void setup () {
      size (1200, 800);
      startTimer = new Timer (0);
      ball = new Ball (600,400,30,200,0,0,0);

      blue  = new Blue (400,230,50,200,0,0,255);

      red = new Red (800,230,50,200,255,0,0);
    }












    void draw () {
      drawPitch ();
      ball.display();
      ball.update();
      ball.contact();

      startTimer.countUp();

      blue.display();
      blue.move();

      red.display();
      red.move();


      fill(0);
      textSize (20);
      text (startTimer.getTime(),575,20);


      println (mouseX, mouseY);
    }
    class Ball {

      PVector Pos = new PVector (0, 0);
      float Size = 30;
      float Speed = 200;
      PVector direction = new PVector (0, 0);
      color Colour = color(0, 0, 0);
      PVector distance = new PVector (0, 0);


      Ball (float x, float y, float size, float speed, int red, int green, int blue) {  //creates ball
        Pos = new PVector (x, y);
        Size = size;
        Speed = speed;
        Colour =  color (red, green, blue);
        direction = new PVector (random(-1, 1), random (-1, 1));
      }

      void display () {  //displays the ball to the screen. needs to be inside void draw ()
        fill (Colour);
        noStroke();
        ellipse (Pos.x, Pos.y, Size, Size);
      }

      void contact () {

        if (Pos.x.dist(Blue.Pos.x)<=51){
        direction.x=-direction.x;



        }
      }









      void update () { //updates the balls location and collisions. needs to be inside void draw ()
        Pos.x += direction.normalize().x *1/frameRate * Speed; 
        Pos.y += direction.normalize().y *1/frameRate * Speed; 

        if (Pos.x <0) {   //left wall collision
          direction.x = -direction.x;
        }

        if (Pos.x >width) { //right  wall collision
          direction.x = -direction.x;
        }

        if (Pos.y >0) {  //top wall collision
          direction.y = -direction.y;
        }
        if (Pos.y <height) { //bottom wall collision
          direction.y = -direction.y;
        }
      }
    }
class Blue {
  public PVector Pos;

  float Size = 50;
  float Speed = 0.1;
  color Colour = color (0, 0, 255);
  PVector Direction;

  Blue (float x, float y, float size, float speed, int red, int green, int blue) {
    Pos = new PVector(x,y);

    Size = size;
    Speed = speed;
    Colour = color (red, green, blue);
  }

  void display () {
    fill (Colour);
    strokeWeight (2);
    stroke(255);
    ellipse(Pos.x, Pos.y, Size, Size);
  }


  void move () {

    if ((keyPressed == true) && (key == 'a')) {
      Pos.x = Pos.x -2;
    }

    if ((keyPressed == true) && (key == 'd')) {
      Pos.x = Pos.x +2;
    }

    if ((keyPressed == true) && (key == 'w')) {
      Pos.y = Pos.y -2;
    }

    if ((keyPressed == true) && (key == 's')) {
      Pos.y = Pos.y +2;
    }





  }
}````
    `

Answers

  • Answer ✓

    well, i would start by reading about 'java naming convention' (just google it). in short:

    variables and methods look like variableName

    classes look like ClassName

    constants look like CONSTANT_NAME

    you problem is that you are calling a method on a class rather than on an instance of a class. normally this would be easy to see because it'd look like Class.method() rather than variable.method(), see, the initial capital is a clue. but all your variables start with an uppercase character so it's hard to see.

    (although if you'd given us the line number that would help)

    class MyClass {
      void m() {
        println("m");
      }
    }
    
    MyClass myClass = new MyClass();
    
    myClass.m(); // this is ok, a method call on a variable of type MyClass
    
    MyClass.m(); // bad. attempting to call the static method m on MyClass.
    

    (you can do the latter. but it's not what you want. fix the names)

  • line 76 fwiw

  • you problem is that you are calling a method on a class rather than on an instance of a class.

    i'm extremely new to processing and i still don't fully understand, do you think you could show me some specifics in what i would need to change? (with regards to the Blue.Pos.x)

    thanks so much for the first reply

  • Answer ✓

    line 76, Blue is the name of the Class, defined on line 112

    you want the Pos of the member blue defined on line 3

    so you need blue.Pos.x rather than Blue.Pos.x

    blue is a terrible name for the member variable anyway because there's a method blue() in processing, which is why the syntax highlighter is displaying it the same way it's highlighting draw(). this is ok in java processing, but in javascript it will cause you no end of pain. so get into the habit of picking better names.

  • Thanks again, i'm getting a separate error now on the same line 76 that states "Cannot invoke dist(float) on the primitive type float, i assumed this had something to do with my variables 'Pos' however they're both declared as PVectors?

  • Pos.x is a single float.

    Pos1.dist(Pos2) is what you probably want, the distance between two points defined as pvectors.

  • or, if you're only bothered with the x dimension, just abs(pos1.x - pos2.x) < 51 will do

  • It's still giving me the same error, this is what i've got now

    void contact () {
    
       if (pos1.x.dist(blue.pos.x)<51){
         direction.x = -direction.x;
        }
      }
    

    just tell me if i'm being stupid this is all just so confusing :P

  • Answer ✓

    Pos is a Pvector. Pvector.dist() is OK.

    Pos.x is a float, float.dist() is not ok.

  • Please check the ref:

    https://processing.org/reference/dist_.html
    https://processing.org/reference/PVector_dist_.html

    just tell me if i'm being stupid this is all just so confusing

    All are good questions but it is likely either it has been asked before in the forum or the answer can easily be found in the reference. For the former, do not use the forum's search engine but better to use google with the keyword "Processing"

    Kf

Sign In or Register to comment.