Subclass won't display or move -- Help!

edited August 2017 in Questions about Code

Back again with another class issue, this time after three days of troubleshooting, I am still stuck on why my paddle won't display. I would appreciate any help I can get. Thanks!

Main Tab //Gwendolyn Leister - Pong Game

GamePiece Paddle;
GamePiece Ball;

boolean gameStart = false;
boolean restart = false;
boolean endgame = false;
//Declare Fonts
PFont myFont;
PFont myFont2;

//for Ball 
int rad = 15;
float xPos=width/2;
float yPos=height/2;
float Speed = 3;

int xDirection = 1;
int yDirection = 1;
int SPD = 5;

int score = 0;


final GamePiece ball = new Ball();

final GamePiece paddle = new Paddle();

void setup () {
  size(800, 800);
  smooth();
  background(0);
  myFont = loadFont("AnonymousPro-30.vlw");
  myFont2 = loadFont("AnonymousPro-80.vlw");
  Ball = new Ball();
}

void draw() {
  //table


  background (0);

  //Layout
  fill(#CBFEFF);
  noStroke();
  rect(0, 640, 800, 260);
  stroke(255);
  fill(#CBFEFF);
  strokeWeight(10);
  line(0, 640, 800, 640);
  fill(#EAFFFF);
  rect(200, 700, 400, 160);
  //text

  //score text
  textFont(myFont);
  textAlign(RIGHT);
  textSize(20);
  text("Score:", 70, 25);

  Ball.display();
  //Paddle.display();

  if (gameStart) {
    Ball.move();
    Ball.bounce();
    //Paddle.move();
  } else {
    textAlign(CENTER);
    textFont(myFont2);
    textSize(80);
    text("P O N G", 400, 100);
    textSize(20);
    text("press enter to start", 400, 600);
    text("move mouse horizontally to control the", 400, 140);
    text("paddle and to bounce the ball off of", 400, 162);
  }
  //if (keyPressed) {
  //  restart = !restart;
  //}
}

//if (Ball.checkScore) {
//  score = score+1;
//  textSize(20);
//  textAlign(LEFT);
//  text("

void checkScore() {
  if (yPos>700) {
    score=score+1;
  } else if (yPos>750) {
    score=score-1;
  }
}

void keyPressed() {
  if (keyCode == ENTER){
    gameStart = !gameStart;
  }
  else{

  }
}

Paddle SubClass class Paddle extends GamePiece {

  boolean mouseMoved;
  boolean overPaddle = false;
  boolean locked = false;
  float xOffset = 0.0;
  float yOffset = 0.0;
  int px;
  int py;
  int pw=100;
  int ph=30;
  int pSpeed= 5;


  //float xPos = mouseX;
  //float yPos = 680;
  //int x;
  //int y;
  //int w=20;
  //int h=80;
  //Paddle (int tx,int ty) {
  //  x=tx;
  //  y=ty;
  //}



  Paddle () {
    super();
  }

  void display() {
    rectMode(CENTER);
    fill(#CBFEFF);
    stroke(255);
    strokeWeight(10);
    //rect(this.xPos, this.yPos, 50, 20);
  }
  //void mouseMove() {
  //  if (x==1) {
  //    py = py +- pSpeed;
  //  } else if (x == 0);
  //}
  void move () {

    if (mouseX > px-(pw*ph) && mouseX < px+(pw*ph) && 
      mouseY > py-(pw*ph) && mouseY<py+(pw*ph)) {
      overPaddle=true;
      if (!locked) {
        stroke(255);
        fill(255);
      } else {
        fill(#CBFEFF);
        stroke(255);
        strokeWeight(10);
      }
      rect(px, py, pw, ph);
    }
  }
  void mousePressed() {
    if (overPaddle) {
      locked = true;
      fill (#CBFEFF);
    } else {
      locked = false;
    }
    xOffset =mouseX-px;
  }
  void bounce() {
  }
}

and GamePiece Superclass

abstract class GamePiece {
  float x, y, w, h, vx, vy, dx, dy;
  abstract void move(); 
  abstract void display();
  abstract void bounce();
}

Answers

  • edited August 2017

    There are lotsa misunderstandings in your sketch right now.
    For starters, let's pick 1 of them for now... :-\"

    • In Processing, there are some pre-established callbacks.
    • Most important of them all are setup() & draw().
    • There are user input callbacks too; e.g. keyReleased(), mousePressed(), etc.
    • But all of them belong to Processing's class PApplet.
    • Placing any of them inside some other class won't automatically trigger them.
    • By default, a Processing sketch can't "see" its callbacks when they're defined inside other classes!
    • However, you may choose to invoke your Paddle::mousePressed() method from inside the actual PApplet::mousePressed() callback: https://Processing.org/reference/mousePressed_.html

    final GamePiece paddle = new Paddle();
    
    void mousePressed() {
      ((Paddle) paddle).mousePressed();
    }
    

    B/c abstract class GamePiece doesn't have any method named mousePressed(), variable paddle had to be temporarily downcast to its more specific datatype (Paddle), so its mousePressed() could be seen. :ar!

  • Thank you, I chose to actually remove the mousePressed() callbacks as they were not effective to what I was trying to do. What I am trying to do here is to get the Paddle.display & Paddle.move to show up when I run the program, as I get a nullpointererror when I implement them into my main tab. I put in the same code that is within the paddle tab into the main tab and it worked just fine as well (though I am still working on getting the ball to bounce off of the paddle).

    Sorry for the abundance of errors...I'm probably going to retake the class inperson next time so that I get a better learning experience. All your help and other's guidance on other forums has helped me a lot!

  • edited August 2017

    Here comes the 2nd batch of bugs. ;;)

    • You need to determine which variables should be global and which 1s should be local.
    • Also, you need to pay attention to which variables a subclass already inherits rom its parent class.
    • abstract class GamePiece already got fields: float x, y, w, h, vx, vy, dx, dy;
    • Therefore, when you class Paddle extends GamePiece { }, Paddle receives those 8 fields automatically.
    • So you're not gonna need to define redundant 1s like you're doing right now.
    • For example, fields px, py, pw, ph, pSpeed aren't needed.
    • Just use the 1s it's got from GamePiece: x, y, w, h, vx.
    • Or else, remove those fields from parent GamePiece if you're not gonna use them in your subclasses! :-@
  • I moved the variables used in my subclasses into the parent GamePiece class and removed the existing variables that were there, as I did not use them anywhere else. Thanks for the tip! Makes a lot of sense.

  • Still not sure why the paddle.display & .move are receiving nullpointerexpection errors though (insert crying emoji here)

  • edited August 2017 Answer ✓
    • In Java's convention, variables should follow the lowerCaseNaming. ~O)
    • Weirdly, you've got global variables Paddle & paddle; Ball & ball! @-)
    • You should erase variables Paddle & Ball and use only paddle & ball global variables. :-B
  • YAY! That seemed to have worked! THe paddle now shows up, but now it doesn't seem to move (it was working earlier when I check my paddle code...) not sure what is up with that... Hopefully it will be a quick fix. Thank you!

  • when you made several changes in your code I recommend to post the current entire code here again

  • Chrisir, I ended up making a new thread just a few minutes ago since I ran into a different issue. Thank you for the tip! I will be sure to do so in the future. I will share the link: https://forum.processing.org/two/discussion/23866/bounce-ball-off-of-paddle

Sign In or Register to comment.