We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
There are lotsa misunderstandings in your sketch right now.
For starters, let's pick 1 of them for now... :-\"
class
PApplet.class
won't automatically trigger them.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!
Here comes the 2nd batch of bugs. ;;)
abstract class
GamePiece already got fields:float x, y, w, h, vx, vy, dx, dy;
class Paddle extends GamePiece { }
, Paddle receives those 8 fields automatically.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)
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