We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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
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.
class
, a good idea is to declare itabstract
.abstract
methods inside it.abstract void move();
.class
whichextends
it gotta implement its own method move().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 Tab
Ball Tab
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)
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. *-:)