I have built the first level for my game based upon breakout game I have created the classes for my ball, paddle, egg and world objects.
I now want my ball object to collide with my egg object only once and then return back to its Paddle object before moving again can someone at Greenfoot help?
Here is the Code for my Ball Object
reenfoot help?
import java.util.List;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The Robot of the game. It moves and bounces off the walls and the paddle.
*
* @author
* @version (Version 1.0)
*/
public class Robot extends Actor
{
private int points = 0;
private Counter counter;
private HealthBar healthBar;
private int deltaX; // delat is movement in the x direction
private int deltaY; // delta is movement in the y direction
private int count = 2;
private boolean fixed = true; // ????
public Robot(Counter c, HealthBar hb)
{
counter = c;
healthBar = hb;
}
public Robot()
{
}
/**
* Act. Move the Robot if we're not fixed to paddle.
*/
public void act()
{
if (!fixed)
{
move();
makeSmoke();
checkEggs();
checkOut();
}
}
/**
* Move the Robot. Thencheck what we've hit.
*/
public void move()
{
setLocation (getX() + deltaX, getY() + deltaY);
checkPaddle();
checkWalls();
}
/**
* Check whether we've hit one of the three walls. Reverse direction
*/
private void checkWalls()
{
if (getX() == 0 || getX() == getWorld().getWidth()-1) {
deltaX = -deltaX;
}
if (getY() == 0) {
deltaY = -deltaY;
}
}
/**
* Check whether we're out (bottom of screen).
*/
private void checkOut()
{
if (getY() == getWorld().getHeight()-1)
{
List<Paddle> paddles = getWorld().getObjects(Paddle.class);
Robot newRobot = new Robot(counter, healthBar);
getWorld().addObject(newRobot, paddles.get(0).getX(), paddles.get(0).getY() - 35);
paddles.get(0).setNewRobot(newRobot);
getWorld().removeObject(this);
}
}
/**
* Check whether we have hit an egg, and make the egg disappear if we have
*/
private void checkEggs()
{
Actor egg = getOneIntersectingObject(goldEgg.class);
if (egg != null)
{
getWorld().removeObject(egg);
deltaY = -deltaY;
counter.add(5);
}
egg = getOneIntersectingObject(yellowEgg.class);
if (egg != null)
{
getWorld().removeObject(egg);
deltaY = -deltaY;
counter.add(3);
}
egg = getOneIntersectingObject(redEgg.class);
if (egg != null)
{
getWorld().removeObject(egg);
deltaY = -deltaY;
counter.add(1);
}
egg = getOneIntersectingObject(blueEgg.class);
if (egg != null)
{
getWorld().removeObject(egg);
deltaY = -deltaY;
healthBar.add(-10);
}
//if (counter.getValue() >= 60) {
// gameOver();
}
private void checkPaddle()
{
Actor paddle = getOneIntersectingObject(Paddle.class);
if (paddle != null) {
deltaY = -deltaY;
int offset = getX() - paddle.getX();
deltaX = deltaX + (offset/10);
if (deltaX> 7) {
deltaX = 7;
}
if (deltaX < -7) {
deltaX = -7;
}
//SquareWorld mySquareWorld = (SquareWorld) getWorld();
//mySquareWorld.score();
}
}
/**
* Move the Robot a given distance sideways
*/
public void move(int dist)
{
setLocation (getX() + dist, getY());
}
/**
* Put out a puff of smoke (only on every second call).
*/
private void makeSmoke()
{
count--;
if (count == 0) {
getWorld().addObject ( new Smoke(), getX(), getY());
count = 2;
}
}
/**
* Release the Robot from the paddle.
*/
public void release()
{
deltaX = Greenfoot.getRandomNumber(11) - 5;
deltaY = -5;
fixed = false;
}
}
