I need help spawning in planets with the numpad. Here is the Space code and Body code could anyone tell me what I am doing wrong?
******Body*******
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* A 'Body' is any kind of object in space that has a mass. It could be a star, or a planet,
* or anything else that floats around in space.
*
* @author Michael Kolling
* @version 0.1
*/
public class Body extends SmoothMover
{
// constants
private static final double GRAVITY = 5.8;
private static final Color defaultColor = new Color(255, 216, 0);
// fields
private double mass;
private boolean keydown;
/**
* Construct a Body with default size, mass, movement and color.
*/
public Body()
{
this (20, 300, new Vector(0, 1.0), defaultColor);
}
/**
* Construct a Body with a specified size, mass, movement and color.
*/
public Body(int size, double mass, Vector movement, Color color)
{
this.mass = mass;
addForce(movement);
GreenfootImage image = new GreenfootImage (size, size);
image.setColor (color);
image.fillOval (0, 0, size-1, size-1);
setImage (image);
}
/**
* Act. That is: apply the gravitation forces from
* all other bodies around, and then move.
*/
public void act()
{
move();
applyForces();
bounceAtEdge();
numPad();
}
/**
* Return the mass of this body.
*/
public double getMass()
{
return mass;
}
private void applyForces()
{
List<Body> bodies = getWorld().getObjects(Body.class);
for(Body body : bodies)
{
if (body != this)
{
applyGravity (body);
}
}
}
private void applyGravity(Body other)
{
double dx = other.getExactX() - this.getExactX();
double dy = other.getExactY() - this.getExactY();
Vector force = new Vector (dx, dy);
double distance = (dx*dx + dy*dy);
double strength = (GRAVITY * this.mass * other.mass / (distance * distance));
double acceleration = strength / this.mass;
force.setLength (acceleration);
addForce (force);
}
private void bounceAtEdge()
{
if(getX() == getWorld().getWidth()-1)
{
setLocation((double)getX(), (double)getY());
getMovement().revertHorizontal();
accelerate(0.9);
}
if(getX() == 0)
{
setLocation((double)getX(), (double)getY());
getMovement().revertHorizontal();
accelerate(0.9);
}
if(getY() == getWorld().getHeight()-1)
{
setLocation((double)getX(), (double)getY());
getMovement().revertVertical();
accelerate(0.9);
}
if(getY() == 0)
{
setLocation((double)getX(), (double)getY());
getMovement().revertVertical();
accelerate(0.9);
}
}
public void numPad()
{
int num = 0;
if (Greenfoot.isKeyDown("0"))
{
getWorld().removeObjects(getWorld().getObjects(Body.class));
}
if (Greenfoot.isKeyDown("1")){num = 1;}
if (Greenfoot.isKeyDown("2")){num = 2;}
if (Greenfoot.isKeyDown("3")){num = 3;}
if (Greenfoot.isKeyDown("4")){num = 4;}
if (Greenfoot.isKeyDown("5")){num = 5;}
if (Greenfoot.isKeyDown("6")){num = 6;}
if (Greenfoot.isKeyDown("7")){num = 7;}
if (Greenfoot.isKeyDown("8")){num = 8;}
if (Greenfoot.isKeyDown("9")){num = 9;}
for (int loop = 0; loop < 0; loop--)
{
int size = 20 + Greenfoot.getRandomNumber(40);
double mass = size * Greenfoot.getRandomNumber(10);
int degree = Greenfoot.getRandomNumber(360);
double speed = Greenfoot.getRandomNumber(5);
int r = Greenfoot.getRandomNumber(255);
int g = Greenfoot.getRandomNumber(255);
int b = Greenfoot.getRandomNumber(255);
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
getWorld().addObject (new Body(size, mass, new Vector(degree, speed), new Color(r,g,b)), x, y);
num--;
}
}
}
*******Space********
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Space. The final frontier.
*
* @author Michael Kolling
* @version 1.0
*/
public class Space extends World
{
/**
* Create space.
*/
public Space()
{
super(960, 620, 1);
randomPlanets();
// Uncomment one of the following method calls if you want the objects created automatically:
//sunAndPlanet();
//sunAndTwoPlanets();
//sunPlanetMoon();
}
/**
* Set up the universe with a sun and a planet.
*/
public void sunAndPlanet()
{
removeAllObjects();
addObject (new Body (50, 240.0, new Vector(270, 0.03), new Color(255, 216, 0)), 460, 270);
addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 260);
}
/**
* Set up the universe with a sun and two planets.
*/
public void sunAndTwoPlanets()
{
removeAllObjects();
addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 310);
addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 300);
addObject (new Body (24, 4.6, new Vector(270, 1.8), new Color(248, 160, 86)), 180, 290);
}
/**
* Set up the universe with a sun, a planet, and a moon.
*/
public void sunPlanetMoon()
{
removeAllObjects();
addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 270);
addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 720, 260);
addObject (new Body (5, 0.8, new Vector(90, 3.25), new Color(240, 220, 96)), 748, 260);
}
/**
* Remove all objects currently in the world.
*/
private void removeAllObjects()
{
removeObjects (getObjects(Actor.class));
}
//Randomly Generate Planets
public void randomPlanets()
{
for(int i = 10 + Greenfoot.getRandomNumber(10); i > 0; i--)
{
int size = 20 + Greenfoot.getRandomNumber(40);
double mass = size * Greenfoot.getRandomNumber(10);
int degree = Greenfoot.getRandomNumber(360);
double speed = Greenfoot.getRandomNumber(5);
int r = Greenfoot.getRandomNumber(255);
int g = Greenfoot.getRandomNumber(255);
int b = Greenfoot.getRandomNumber(255);
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Body(size, mass, new Vector(degree, speed), new Color(r,g,b)), x, y);
}
}
}
