Method being called at the start

So, I have a gameOver() method that should when oval goes out of bounds abort game but, as soon as I start the game it runs the gameOver method. I've been looking over it for a while trying different things. I think what stood out to me is removing the abort sequence the game runs mostly as it should after, popup is closed and that if I replace game.gameOver(); with ya = -1 the ball bounces off the wall.

gaveOver()
public void gameOver(){
		JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
		System.exit(ABORT);
	}

move()
void move(){
		if (x + xa < 0) // Makes left bounds
			xa = 1; // Moves the ball right
		if (x + xa > game.getWidth() - DIAMETER) // Makes right bounds
			xa = -1; // Moves the ball left
		if(y + ya < 0) // Makes top bounds
			ya = 1; // Moves ball down
		
		if(y + ya > game.getHeight() - DIAMETER)
			game.gameOver();

		if (collision()){ // Makes collision with Racquet
			ya = -1;
			y = game.racquet.getTopY() - DIAMETER;
		}
		x = x + xa;
		y = y + ya;
	}
	
	private boolean collision(){
		return game.racquet.getBounds().intersects(getBoundsBall());
	}
	
	public void paint(Graphics2D g){
		g.fillOval(x, y, DIAMETER, DIAMETER);
	}
	
	public Rectangle getBoundsBall(){
		return new Rectangle(x, y, DIAMETER, DIAMETER);
	}

Comments

  • ahaha!!! :) I did it.

    I just rewrote the move() method and I fixed it. Here's the code
    	void move(){
    		 if(x + xa < 0)// Makes left bounds
    			 xa = 1;// Moves the ball right
    		 if(x + xa == game.getWidth() - DIAMETER)// Makes right bounds
    		 	 xa = -1;// Moves the ball left
    		 if (y + ya < 0)// Makes top bounds
    			 ya = 1;// Moves ball down
    		 if(y + ya == game.getHeight() - DIAMETER){	// Makes bottom bounds
    		 	ya = 0;// Moves ball up
    			xa = 0;
    			game.gameOver();
    		 }
    
Sign In or Register to comment.