myClass.move does not exist (but it does)

edited August 2017 in Questions about Code

Hey all, I'm making a game for my online Processing class and I am a bit stuck. I'm very new to the program (and programming in general) so bare with me. I have called move() in my Ball class but when I call ball.move(); in void draw(), I get an error that says.move() doesn't exist. If anyone could look at my code to see whats wrong with it, I would appreciate it a lot!

My code:

float ballSpeed=5;
float PaddleSpeed=6;
gamePieces paddle;
gamePieces ball;
boolean mousePressed = false;


//paddle p1;

int playerscore = 0;
int cpuscore = 0;
int lastlevel = 3;
boolean keypress = false;
boolean endgame = false;

void setup () {
  size(800, 800);
  smooth();
  background(255);
  ball = new ball();
}

void draw() {
  //table
  background(0);


  //displays

  ball.display();
  if (endgame) {
    textSize(30);
    textAlign(CENTER);
    fill (255);
    text("Click to Start", 400, 100);
  } else {
    if (mousePressed == true) {
      ball.move();
      //paddle.moveX(moveX);

      //paddle.intersectTop(ball);
    }
  }
}

Ball clas (was in a separate tab)

class ball extends gamePieces {

  // processing.org/examples/bounce.html
  int rad = 20;
  float xpos, ypos;

  float xspeed = 2.6;
  float yspeed = 2.5;

  int xdirection = 1;
  int ydirection = 1;


  ball() {
    super();
  }

  void display() {
    stroke(255);
    strokeWeight(3);
    fill(#FFFEE8);
    frameRate(30);
    ellipseMode(RADIUS);
    ellipse(xpos, ypos, rad, rad);
  }

  void move () {
    xpos = width/2;
    ypos = height/2;

    xpos = xpos + (xspeed * xdirection);
    ypos=+ ypos + (yspeed * ydirection);

    if (xpos > width-rad || ypos < rad) {
      xdirection *= -1;
    }
    if (ypos > height-rad || ypos < rad) {
      ydirection *= -1;
    }
  }
}

Answers

  • edited August 2017

    Highlight all the code in your text and click the 'C' icon to format multiple lines of code. The ` only works for a single line.

  • Thank You Dinoswarleafts! I fixed the formatting now I believe

    • By Java's standards, classes and interfaces should follow the UpperCaseNaming.
    • And it's advisable for the name be singular too.
    • So ball should be Ball instead.
    • And gamePieces should be named as GamePiece.
    • The reason method move() doesn't "exist" is b/c you've declared variable ball as gamePieces.
    • Even though variable ball holds an instance of ball, Java "sees" it as being gamePieces instead.
    • Solution: declare ball as of datatype ball instead of gamePieces.
    • Even better, rename those types as Ball & GamePiece. :-bd
  • GoToLoop, Thank you for the advice! I have looked at other forums where you have answered and your feedback has helped me on many projects during this class, so I really appreciate all your help.

    I switched the class names to UpperCaseNaming. For the project I am working on, I have to include a class and a subclass. I have used GamePiece as the top class, and ball as the subclass (and eventually will add Paddle as another subclass of GamePiece). Will I still be able to use the Ball subclass and call the Ball.move ? Is that done in the main tab still? Or would I have to call it (or some part of Ball) in the GamePiece class tag?

    Hope that makes some sense... Thanks again for your help.

    • Since GamePiece is your base class, a good idea is to declare it abstract.
    • Doing so allows you to declare abstract methods inside it.
    • For example, you can declare abstract void move();.
    • Now any class which extends it gotta implement its own method move().
    • And that also allows variables declared as GamePiece to be able to invoke method move().

    /** 
     * Abstract Inheritance (v1.0)
     * GoToLoop (2017/Aug/17)
     *
     * Forum.Processing.org/two/discussion/23842/
     * myclass-move-does-not-exist-but-it-does#Item_5
     */
    
    final GamePiece ball = new Ball(), paddle = new Paddle();
    
    void setup() {
      frameRate(1);
    }
    
    void draw() {
      background((color) random(#000000));
      ball.move();
      paddle.move();
      println("ball:", ball.x, "\tpaddle:", paddle.x);
    }
    
    abstract class GamePiece {
      int x;
      abstract void move();
    }
    
    class Ball extends GamePiece {
      static final int SPD = 5;
    
      @ Override void move() {
        x += SPD;
      }
    }
    
    class Paddle extends GamePiece {
      static final int SPD = 3;
    
      @ Override void move() {
        x += SPD;
      }
    }
    
  • GoToLoop, I was able to get the ball to appear in the center of my sketch now so that's a step in the right direction! Now the problem I am facing is that that new Ball wont move from the center. This is what I got so far: Pong Game (main tab)

    GamePiece Paddle;
    GamePiece Ball;
    boolean mousePressed = false;
    
    //for Ball 
    int rad = 15;
    float xPos, yPos;
    
    float xSpeed = 3;
    float ySpeed = 2;
    
    int xDirection = 2;
    int yDirection = 1;
    
    int SPD = 5;
    
    
    int playerscore = 0;
    boolean keypress = false;
    boolean endgame = false;
    
    final GamePiece ball = new Ball(), paddle = new Paddle();
    
    void setup () {
      size(800, 800);
      smooth();
      background(0);
      Ball = new Ball();
    }
    
    void draw() {
      //table
    
      background (0);
      //displays & moves
      Ball.move();
      Ball.display();
    
      //Paddle.move();
      //Paddle.display();
    }
    

    gamePiece Tab

            abstract class GamePiece {
    
              GamePiece () {
              }
    
    
              abstract void move();
              abstract void display();
            }
    

    Ball Tab

    class Ball extends GamePiece {
    
      //https://processing.org/examples/bounce.html
      Ball() {
        super();
      }
    
      void display() {
        stroke(255);
        strokeWeight(3);
        fill(#FFFEE8);
        frameRate(30);
        ellipseMode(RADIUS);
        xPos = width/2;
        yPos = height/2;
    
        ellipse(xPos, yPos, rad, rad);
      }
    
      void move () {
        //scales ball position
        ellipse(xPos, yPos, rad, rad);
        xPos = xPos + (xSpeed * xDirection);
        yPos = yPos + (ySpeed * yDirection);
    
        if (xPos > width-rad || xPos < rad) {
          xDirection *= -1;
        }
        if (yPos > height-rad || yPos < rad) {
          yDirection *= -1;
        }
      }
    }
    

    And Paddle tab (which isnt working either.. getting the dreaded nullpointerexception error when i try to call the display and move... but thats the next thing I have to tackle)

    class Paddle extends GamePiece {
      float xPos;
      float yPos = 700;
    
      Paddle () {
      }
    
      void display () {
        rectMode(CENTER);
        rect(xPos, yPos, 50, 20);
      }
      void move () {
        xPos = mouseX;
      }
    }
    
  • edited August 2017 Answer ✓

    You need to define the coordinate, dimension, speed & direction pairs inside class as well.
    You can even define them directly in abstract class GamePiece.
    So its children classes automatically inherit them all. *-:)

    abstract class GamePiece {
      float x, y, w, h, vx, vy, dx, dy;
    
      abstract void move();
      abstract void display();
    }
    
    class Ball extends GamePiece {
      @ Override void move() {
      }
    
      @ Override void display() {
      }
    }
    
    class Paddle extends GamePiece {
      @ Override void move() {
      }
    
      @ Override void display() {
      }
    }
    
Sign In or Register to comment.