Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ian Z
14,584 PointsMy subclass is not inheriting the extended classes methods, what am i doing wrong?
I am trying to make the classic breakout paddle game that uses interfaces and inheritance. I have classes Ball.java Brick.java Paddle.java that extend a class called GameObject.java that is used to make stuff dryer
however when i call i try to call GameObject's .getXPosition() method via a Paddle object player, in Game class, it says "cannot find symbol" Craig Dennis
Here is GameObject
package GameObjects;
import Interfaces.Drawable;
import java.awt.Color;
import java.awt.Rectangle;
public abstract class GameObject implements Drawable{
private int xPos;
private int yPos;
private Color color;
public GameObject(int xpos,int ypos,Color newColor){
xPos = xpos;
yPos = ypos;
color = newColor;
}
public abstract Rectangle getBounds();
public int getXPosition(){
return xPos;
}
public int getYPosition(){
return yPos;
}
public Color getColor(){
return color;
}
public void setXPosition(int xPosition){
xPos = xPosition;
}
public void setYPosition(int yPosition){
yPos = yPosition;
}
public void setColor(Color newColor){
color = newColor;
}
public boolean isColliding(GameObject other){
if(getBounds().intersects(other.getBounds())){
return true;
}
else
return false;
}
}
Here is paddle object
package GameObjects;
import Interfaces.Moveable;
import java.awt.Color;
import Controls.KeyboardController;
public class Paddle extends GameObject implements Moveable {
private int xPos, yPos;
private int height, width;
private int speed;
private Color color;
private KeyboardController controller;
public Paddle(int xPosition, int yPosition, int newWidth, int newHeight, int newSpeed, Color newColor, KeyboardController newController){
xPos = xPosition;
yPos = yPosition;
speed = newSpeed;
color = newColor;
controller = newController;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public int getSpeed(){
return speed;
}
public void setWidth(int newWidth){
width = newWidth;
}
public void setHeight(int newHeight){
height = newHeight;
}
public void setSpeed(int newSpeed){
speed = newSpeed;
}
public void move(){
}
}
here is where it is called in Game
package Game;
import Controls.KeyboardController;
import GameObjects.Ball;
import GameObjects.Paddle;
import GameObjects.GameObject;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Game extends JPanel
{
public static final int HEIGHT = 800;
public static final int WIDTH = 600;
public static final int PADDLE_Y_POS = HEIGHT - 30;
public static final Color PADDLE_COLOR = Color.black;
public static final int BALL_DIAMETER = 15;
public Color color;
private KeyboardController controller;
public Paddle player = new Paddle(110, HEIGHT-30, 80, 10, 5, color.black, controller);
public Ball gameBall = new Ball((player.getXPosition()), (PADDLE_Y_POS - (BALL_DIAMETER + 10)), 15, color.black);
//line above^ .getXPosition() not working
and heres ball
package GameObjects;
import Interfaces.Moveable;
import java.awt.Color;
import java.awt.Graphics2D;
public class Ball extends GameObject implements Moveable {
private int xPos, yPos;
private int xVel, yVel;
public static int diameter;
public static Color ballColor;
public Ball(int newXPos, int newYPos, int newDiameter, Color newColor){
xPos = newXPos;
yPos = newYPos;
diameter = newDiameter;
ballColor = newColor;
}
public int getXVelocity(){
return xVel;
}
public int getYVelocity(){
return yVel;
}
public void setXVelocity(int xVelocity){
xVel = xVelocity;
}
public void setYVelocity(int yVelocity){
yVel = yVelocity;
}
public int getDiameter(){
return diameter;
}
public void move(){
xPos += xVel;
yPos += yVel;
}
public void drawBall(Graphics2D g){
g.setColor(ballColor);
g.fillOval(xPos, yPos, diameter, diameter);
g.setColor(Color.gray);
g.drawOval(xPos, yPos, diameter, diameter);
}
}
Craig Dennis
Treehouse TeacherAlso how are your folders laid out? Packages seem a little off.
1 Answer
Ian Z
14,584 PointsAs soon is i tried to make a stack trace (run it?) the red underline error went away, i guess it needed to prime the gameObject or something.? must be a little net beans quirk. So its working now thank you.
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherCool! Can you include the stack trace please? Might be more obvious after seeing that.