import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* 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, speed;
private int deltaX;
private int deltaY;
private int score = 0;
private int scheck = 1;
/**
* 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()
{
move(); //#Runs the Move() statement
start(); //#Runs the Start() statement
checkOut(); //#Runs the Checkout() statement
}
public void start() //Start() statement
{
if (Greenfoot.isKeyDown("space")) //#Checking condition for If space is pressed.
{
if (scheck == 1)
{
deltaY = 7; //#Sets the movement to 7 y.
scheck = 0;
}
}
}
public void move()
{
setLocation (getX() + deltaX, getY() + deltaY); //#Sets the X and Y speeds
checkpaddle(); //#Runs the CheckPaddle() statement
checkwall(); //#Runs the CheckWall() statement
}
/**
* Check whether we've hit one of the three walls. Reverse direction if necessary.
*/
private void checkwall() //#CheckWall() statement
{
if (getX() == 0 || getX() == getWorld().getWidth()-1) {
deltaX = -deltaX;
}
if (getY() == 0) {
deltaY = -deltaY;
}
}
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;
}
}
}
public void move(int dist)
{
setLocation (getX() + dist, getY());
}
private void checkOut()
{
if (getY() == getWorld().getHeight()-1) {
getWorld().removeObject(this);
scheck = 1;
respawn();
//score++;
}
else if (getY() == getWorld().getHeight()-500) {
getWorld().removeObject(this);
scheck = 1;
respawn();
//score++;
}
}
private void respawn()
{
getWorld().addObject(new Ball(), 350, 250);
}
}
java.lang.NullPointerException at Ball.respawn(Ball.java:98) at Ball.checkOut(Ball.java:85) at Ball.act(Ball.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
