0

I'm new at the Java world, and I started to take the Stanford cs106A course. I made the "breakout" problem which consists of making a simple breakout game. I want to go even further, and my point is to create new Classes that can incorporate objects on the GCanvas of the main class.

So, now I have something like this:

public class Breakout extends GraphicsProgram {

        //...

    public void run();
    private void playGame();
    private void checkForPaddleCollisions();
    private boolean passedBorderline();
    private boolean moreBricksRemainig();
    private void checkForCollisions();
    private void updatePuntuation(GObject obj);
    private void changeDirection(int temp);
    private GObject getCollidingObject(double x, double y);
    private void moveBall();
    private void checkForBounderies();
    private boolean hitsLeftWall();
    private boolean hitsRightWall();
    private boolean hitsRoof();
    private void setRandomVx();
    private void setBall();
    private void setUp();
    private void setLabels();
    private void setPaddle();
    public void mouseDragged (MouseEvent e);
    public void mousePressed(MouseEvent i);
    private void placeBricks();

    private GOval ball;
    private GRect paddle;
    private GLabel win, lose, lifes, puntuationLabel;
    private GPoint last;
    private double vx, vy = 3.0;
    private RandomGenerator rgen = RandomGenerator.getInstance();
    private int Delay = 50;
    private int counterBricks = NBRICKS_PER_ROW * NBRICK_ROWS;
    private AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au"); 
    private int noPaddleBugs = 0;
    private int puntuation = 0;
    private int actualPunt = 0;
}

This is the main class of the game. What I want to do is to create a new class named PowerUps. This class can put any object on the GCanvas object of the main class at any point of the game. I tried to access the GCanvas of the main class by saying something like Breakout."canvasproperty".add(newObject), but it seems like it's not permitted.

Then I thought: I can create a class that extends GCanvas and then initialize a public property in the main class of that "new" canvas. With this method, I am able to insert new objects in this canvas from outer classes, but the problem is that when I run the Breakout class, the "new" canvas property doesn't show...

I don't know if I expressed myself well, but I tried, if anyone can help me, i would be very grateful? Thanks in advance.

Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • Normally in a graphics environment, the graphics context is generated by the system, this means, you shouldn't maintain a reference to beyond the time it was supplied to you by the system. In the case, you will need to pass a reference to all the parts of the application hat need it, when the system asks you to update ;) – MadProgrammer Sep 15 '13 at 20:59
  • I'm not sure I understood your answer... maybe it's too difficult to understand on my level and I need to learn more about java first. I think I will wait for this. Thanks anyway :) – Carles Mitjans Sep 15 '13 at 21:08

0 Answers0