This site requires JavaScript, please enable it in your browser!
Greenfoot back
VanishingDog
VanishingDog wrote ...

2019/12/17

Need help deleting actors and remaking them using numpad like in the sunPlanetMoon() method

VanishingDog VanishingDog

2019/12/17

#
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; /** * 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(); } //Makes the planets bounce of the edge of the world private void bounceAtEdge() { if (getX() == 0 || getX() == getWorld().getWidth()-1) { setLocation((double)getX(), (double)getY()); getMovement().revertHorizontal(); accelerate(0.9); int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); Color color2 = new Color(r,g,b); //image.setColor(color2); } else if (getY() == 0 || getY() == getWorld().getHeight()-1) { setLocation((double)getX(), (double)getY()); getMovement().revertVertical(); accelerate(0.9); } } /** * Return the mass of this body. */ public double getMass() { return mass; } //Applies gravity from other bodies and prevents us from going over 7 ? speed public void applyForces() { List<Body> bodies = getWorld().getObjects(Body.class); for (Body body: bodies) { if (body != this) { applyGravity (body); } } if (getSpeed() > 7) { accelerate (0.9); } } //Calculates distance from other planet and applies gravity 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 = Math.sqrt (dx * dx + dy * dy); double strength = GRAVITY * this.mass * other.mass / (distance * distance); double acceleration = strength / this.mass; force.setLength(acceleration); addForce(force); } } 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); numPad(); randomPlanets(7); makeObstacles(); // 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)); } //creates planets and gives them random stats private void randomPlanets(int num) { for (int i= 1;i<num; i++) { int size = 20+ Greenfoot.getRandomNumber(30); double mass = size * 6; int direction = Greenfoot.getRandomNumber(360); int speed = Greenfoot.getRandomNumber(200) / 140; int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y); } } //sets up the obstacles private void makeObstacles() { for(int i = 0; i< 11 ; i++) { addObject (new Obstacle (), 65 + i*80, 50); } for(int i = 2; i < 10 ; i++) { addObject (new Obstacle (), 65, i*65); } for(int i = 0; i< 11 ; i++) { addObject (new Obstacle (), 65 + i*80, 585); } for(int i = 2; i < 10 ; i++) { addObject (new Obstacle (), 865, i*65); } } //deletes all other objects and adds as many actors as you want on screen private void numPad() { if (Greenfoot.isKeyDown("g")) { addPlanets(1); Greenfoot.playSound("Bounce12678623.wav"); } } private void addPlanets(int pad) { removeAllObjects(); for (int i= 1;i<pad; i++) { int size = 20+ Greenfoot.getRandomNumber(30); double mass = size * 6; int direction = Greenfoot.getRandomNumber(360); int speed = Greenfoot.getRandomNumber(200) / 140; int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y); } } } Obstacle: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Obstacle here. * * @author (your name) * @version (a version number or a date) */ public class Obstacle extends Actor { public boolean contact = false; /** * Act - do whatever the Obstacle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { contact(); } public void contact(){ Actor body = getOneIntersectingObject(Body.class); if (contact && body == null) { setImage ("block.png"); contact = false; } if (!contact && body != null) { setImage ("block-light.png"); contact = true; Greenfoot.playSound("Bounce12678623.wav"); //setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()),Greenfoot.getRandomNumber(getWorld().getHeight())); } } }
danpost danpost

2019/12/17

#
You only call numPad once -- when the world is being created. In other words, you are calling it from the wrong place. It needs to be continuously called for any key press to be detected.
VanishingDog VanishingDog

2019/12/17

#
danpost wrote...
You only call numPad once -- when the world is being created. In other words, you are calling it from the wrong place. It needs to be continuously called for any key press to be detected.
So should i call it from body?
VanishingDog VanishingDog

2019/12/17

#
Nevermind I got it working :) Thanks for the help!
You need to login to post a reply.