In my soccer game, I have a bug where the ball keeps getting caught in the edges of the world. I would like to add a reset command that would basically involve moving the ball to the middle of the pitch by pressing 'R' WITHOUT resetting the score counters. How can I do this? Below is my current code for the ball.
Thanks in advance
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
/**
* Write a description of class ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ball extends Actor
{
private int direction;
private int deltaX; // change in x direction
private int deltaY; // change in y direction
private LeftCounter leftCounterPoints;
private RightCounter rightCounterPoints;
private int leftCounterPointsAdd;
private int rightCounterPointsAdd;
public Ball(LeftCounter leftcounter,RightCounter rightcounter)
{
leftCounterPoints = leftcounter;
rightCounterPoints = rightcounter;
leftCounterPointsAdd = 0;
rightCounterPointsAdd = 0;
do
{
direction = Greenfoot.getRandomNumber(6);
}
while (direction == 3);
deltaX = direction - 3;
deltaY = direction - 3;
}
/**
* Act - do whatever the ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
detectPaddle();
ballMove();
if (leftCounterPointsAdd == 10)
{
leftGameWon();
}
if (rightCounterPointsAdd == 10)
{
rightGameWon();
}
}
/**
* move the ball
*/
public void ballMove()
{
int xCoordinate = getX() + deltaX;
int yCoordinate = getY() + deltaY;
setLocation(xCoordinate, yCoordinate);
int direction;
if (atTopOrBottom())
{
deltaY = deltaY * (-1);
}
if (atSides() == true)
{
setLocation(300, 215);
do
{
direction = Greenfoot.getRandomNumber(20);
}
while (direction == 3);
deltaX = direction - 3;
deltaY = direction - 3;
}
}
/**
* test if the ball is at the top or the bottom
*/
public boolean atTopOrBottom()
{
if (getY() < 63 || getY() > getWorld().getHeight() - 33)
{
return true;
}
else
{
return false;
}
}
public void detectPaddle()
{
Actor rightPaddle = getOneIntersectingObject(Player2.class);
Actor leftPaddle = getOneIntersectingObject(Player1.class);
if (rightPaddle!= null)
{
deltaX = -deltaX;
int offset = getY() - rightPaddle.getY();
deltaY = deltaY + offset/10;
Greenfoot.playSound("oof.wav");
}
if (leftPaddle!= null)
{
deltaX = -deltaX;
int offset = getY() - leftPaddle.getY();
deltaY = deltaY + offset/10;
}
}
public boolean atSides()
{
if (getX() < 50 )
{
rightCounterPoints.add(1);
rightCounterPointsAdd = rightCounterPointsAdd + 1;
return true;
}
if(getX() > getWorld().getWidth() - 50)
{
leftCounterPoints.add(1);
leftCounterPointsAdd = leftCounterPointsAdd + 1;
return true;
}
else
return false;
}
public void rightGameWon()
{
World world = getWorld();
world.removeObjects(world.getObjects(null));
Greenfoot.setWorld(new GameOverScreen2());
}
public void leftGameWon()
{
World world = getWorld();
world.removeObjects(world.getObjects(null));
Greenfoot.setWorld(new GameOverScreen1());
}
}
