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

2022/1/7

need help removing the actor that i click when right clicking over it

SportingLife7 SportingLife7

2022/1/7

#
It just won't remove the body when I am right clicking on it, also the body doesn't generate in the position of where the mouse clicked either if anyone can offer some guidance with that too. Any help is truly appreciated :) Space class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Space. The final frontier. 
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Space extends World
{
    private String[] soundFiles =
        {"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d","4e", "4f", "4g"};
    /**
     * Create space.
     */
    public Space()
    {    
        super(960, 620, 1);
        makeObstacles();
        //randomBodies (5); // uncomment to start with 5 random bodies when world iniiated
        // Uncomment one of the following method calls if you want the objects created automatically:

        //sunAndPlanet();
        //sunAndTwoPlanets();
        //sunPlanetMoon();
    }

    /**
     * implements our action methods in our world
     */
    public void act()
    {
        checkKeys();
        addPlanetsWithMouse();
    }

    /**
     * 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);
    }

    /**
     * It will create the obtacles in the world.
     */
    public void makeObstacles()
    {
        int i = 0;
        for (i = 0; i < soundFiles.length; i++) 
        {
            //addObject (new Obstacle (soundFiles [i] + ".wav"), 150 + i*60, 310);
            // this one(above)is specifically for the middle row requirement
            addObject (new Obstacle (soundFiles [i] + ".wav"), 100 + i*70, 5);//horizontal
            addObject (new Obstacle (soundFiles [i] + ".wav"), 100 + i*70, 615);//horizontal
            addObject (new Obstacle (soundFiles [i] + ".wav"), 15 + 10, i*55);//vertical walls
            addObject (new Obstacle (soundFiles [i] + ".wav"), 925 + 10, i*55);//vertical walls
        }
    }

    /**
     * this method generates random bodies in our world
     */
    public void randomBodies (int n)
    {
        
        removeObjects(getObjects(Body.class));
        for (int i = 0; i < n; i++)
        {

            int s = 20 + Greenfoot.getRandomNumber(30);
            double m = Greenfoot.getRandomNumber(75)+10;
            double speed = Greenfoot.getRandomNumber(40) / 25;
            int d = Greenfoot.getRandomNumber(360);
            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 (s, m, new Vector(d, speed), new Color(r, g, b)), x, y);

        }
    }

    /**
     * keys press method for generating the number of bodies in the world
     */
    public void checkKeys()
    {
        // find a way like in the piano code, to ameliorate this, maybe with a counter
        // that we can increase the # of random bodies without having to write these 
        // 12 lines of code! THIS IS CAUSING LAGGING IN THE PROGRAM!! FIX BEFORE CONTINUING!
        
        if (Greenfoot.isKeyDown("1"))
        {
            randomBodies(1);
        }
        else if (Greenfoot.isKeyDown("2")) 
        {
            randomBodies(2);
        }
        else if (Greenfoot.isKeyDown("3")) 
        {
            randomBodies(3);
        }
        else if (Greenfoot.isKeyDown("4")) 
        {
            randomBodies(4);
        }
        else if (Greenfoot.isKeyDown("5")) 
        {
            randomBodies(5);
        }
        else if (Greenfoot.isKeyDown("6")) 
        {
            randomBodies(6);
        }
        else if (Greenfoot.isKeyDown("7")) 
        {
            randomBodies(7);
        }
        else if (Greenfoot.isKeyDown("8"))
        {
            randomBodies(8);
        }
        else if (Greenfoot.isKeyDown("9"))
        {
            randomBodies(9);
        }
        else if (Greenfoot.isKeyDown("0")) 
        {
            removeObjects (getObjects(Body.class));
            //or you can write...
            //randomBodies(0);
        }
    }
    /**
     * 
     */
    private void addPlanetsWithMouse()
    {
       // MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse.getButton() == 1)
            {
             //   Body body = new Body (Greenfoot.getRandomNumber(41));
             //   addObject (body, mouse.getX(), mouse.getY());
            int s = 20 + Greenfoot.getRandomNumber(30);
            double m = Greenfoot.getRandomNumber(75)+10;
            double speed = Greenfoot.getRandomNumber(40) / 25;
            int d = Greenfoot.getRandomNumber(360);
            int x = Greenfoot.getRandomNumber(mouse.getX());
            int y = Greenfoot.getRandomNumber(mouse.getY());
            int r =  Greenfoot.getRandomNumber(255);
            int g =  Greenfoot.getRandomNumber(255);
            int b =  Greenfoot.getRandomNumber(255);
            addObject (new Body (s, m, new Vector(d, speed), new Color(r, g, b)), x, y);
             
            // Bodies++;
            }
            Actor body = mouse.getActor();
            if (mouse.getButton() == 3)
            {
                if (body!= null && body.getClass() == Body.class)
                {
                    removeObject(body);
                }
            }
        }
       
    }
    /**
     * Remove all objects currently in the world.
     */
    private void removeAllObjects()
    {
        removeObjects (getObjects(Actor.class));
    }

}
danpost danpost

2022/1/7

#
Try the following for line 191:
if (body != null && (body instanceof Body))
SportingLife7 SportingLife7

2022/1/7

#
That worked! Thank you so much! I do have one other question if you don't mind. I noticed that my program is kind of lagging a bit and it started when i wrote lines 119 to 160, is there any way i can reduce the amount of code for this?
danpost danpost

2022/1/8

#
SportingLife7 wrote...
I noticed that my program is kind of lagging a bit and it started when i wrote lines 119 to 160, is there any way i can reduce the amount of code for this?
It is not the bulkiness of the code that causes lag -- it is what the code actually does. As written, your code will not just remove and create new actors when a number is pressed, but it will do so multiple times in a row (for multiple act steps, for as long as the number key is down). It would be better, in this case to use getKey instead of isKeyDown:
public void checkKeys()
{
    String key = Greenfoot.getKey();
    if (key == null) return;
    int index = "0123456789".indexOf(key);
    if (index < 0) return;
    randomBodies(index);
}
SportingLife7 SportingLife7

2022/1/8

#
That works so much better, thank you times a thousand :)
You need to login to post a reply.