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

2012/9/11

Newtons-Lab-3: problem adding planets with mouseclick

PoDh34d PoDh34d

2012/9/11

#
Hello, I'm currently working trough the Greenfoot book-scenarios and experience a problem with exercise 6.38 (chapter 6, Newtons-Lab-3). The exercise tells me to write a code to add a planet in the world with a mouseclick. The planet should spawn at the position you click with the mouse. I've written the code and it does what it should do (create a planet at the position you click with the mouse), but it does more... It also creates planets on the top-left corner on the screen. These planets are stuck there (they can't move) and, because of their mass, are forcing all other planets in the screen to that position. So I end up with all planets in the top-left corner of my screen. My question is: Why does my code make more than 1 planet and why do they spawn at the top-left corner of the screen and are stuck there? Thanks in advance, PoDh34d So here is there code I created for adding a planet with a mouseclick.
private void createPlanet()
    {                    
            int size = 20 + Greenfoot.getRandomNumber(30);
            double mass = size * 7.0;
            int direction = Greenfoot.getRandomNumber(360);
            double speed = Greenfoot.getRandomNumber(150) / 100.0;
            int r =  Greenfoot.getRandomNumber(255);
            int g =  Greenfoot.getRandomNumber(255);
            int b =  Greenfoot.getRandomNumber(255); 
            if (Greenfoot.mouseClicked(getWorld()))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();   
                int mouseX = mouse.getX();
                int mouseY = mouse.getY();
                getWorld().addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), mouseX, mouseY);
            }
    }
Gevater_Tod4711 Gevater_Tod4711

2012/9/11

#
Maybe the problem is that you add two planets in two acts, because you cant drop the mousekey as far as greenfoot acts. You could try to use this:
private boolean mouseKlicked = false;

private void createPlanet() {
    ...
    if (Greenfoot.mouseClicked(getWorld()) && !mouseKlicked) {  
    MouseInfo mouse = Greenfoot.getMouseInfo();     
    int mouseX = mouse.getX();  
    int mouseY = mouse.getY();  
    getWorld().addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), mouseX, mouseY);
    mouseKlicked = true;
    }
    else {
        mouseKlicked = false;
    }
}  
I hope this will help
PoDh34d PoDh34d

2012/9/11

#
Thanks for you effort, but the same thing is still happening: When I click anywhere in the world it will create a planet on that place (nothing wrong here). But also 4 other planets spawn in the top-left corner of the world. (I tried several times, it's always 4 other planets that spawn and always in the top-left corner).. I think i know where it goes wrong now, but I dont know why... In the world class 'Space' there is a method to create a number of bodies (planets). Looks like this:
 public void randomBodies(int number)
    {
        while (number > 0) {
            int size = 20 + Greenfoot.getRandomNumber(30);
            double mass = size * 7.0;
            int direction = Greenfoot.getRandomNumber(360);
            double speed = Greenfoot.getRandomNumber(150) / 100.0;
            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);
            number--;
        }
    }
The 'number' is set to 5 in 'public Space()', so 5 planets will be created at the start of the simulation. Now when I click anywhere in the world also 5 planets will be created, 1 planet at the position of the mouseclick and 4 planets in the top-left corner. Now if I put:
Public Space()
{
randomBodies(3);
}
3 planets will be created at start and when I click in the world, 1 planet will be created at the location clicked and 2 other planets in the top-left corner.. Seems like randomBodies(int number); and createPlanet(); interfere with eachother for some reason. But why? I hope you still understand the problem :)
Gevater_Tod4711 Gevater_Tod4711

2012/9/11

#
I think the method randomBodies(int number) is ok. There should be no buggs. A nother Idea would be this:
private void createPlanet() {  
    ...  
    MouseInfo mouse = Greenfoot.getMouseInfo(); 
    if (mouse != null) {
        int x = mouse.getX();
        int y = mouse.getY();
        if (Greenfoot.mouseClicked(getWorld()) && x != -1) {
            getWorld().addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y);
            x = -1;
        }
    }
}   
if this also doesn't work the bugg might be somewhere else.
PoDh34d PoDh34d

2012/9/11

#
I'm sorry to say that this did not help. I keep experiencing the same problem and I'm not sure why. My next Java lessons are on thursday so I hope any of teachers are able to help me out and I'll be able to post a solution for others experiencing the same kind of problem. Thanks for your help and efforts!
Gevater_Tod4711 Gevater_Tod4711

2012/9/11

#
Ok I still got one Idea: Try this:
    private void createPlanet() {    
        ...    
        MouseInfo mouse = Greenfoot.getMouseInfo();   
        if (mouse != null) {  
            int x = mouse.getX();  
            int y = mouse.getY();  
            if (Greenfoot.mouseClicked(getWorld()) && x != -1) { 
                System.out.println(x + " " + y);
                getWorld().addObject (new Body (size, mass, new Vector(direction, speed), new Color(r, g, b)), x, y);  
                x = -1;  
            }  
        }  
    }     
it will show you the numbers x and y on the terminal. If the numbers are 0 or very small the bugg is in this code. If the numbers aren't small (maybe on or two) the bugg is not in this part of your project. In this case you have to search somwhere else for the bugg. I realy hope this will help you.
PoDh34d PoDh34d

2012/9/11

#
Interessting outcome: 455 442 455 442 455 442 455 442 455 442 Seems 1 mouseclick creates 5 planets at exact the same location and 4 of the 5 planets go straight to the corner of the screen. (This happens so fast, even in slowmotion you can barely see this happening). But why does it create 5 planets and not just 1? I experimented a bit and it seems like a mouseclick duplicates the amount of planets already created and sends them all to the corner of the screen except for 1. Even if i pause the simulation and drag all those planets out of the corner and unpause it. They immediately fly back to the corner. Really strange... I'm going to ask my teacher thursday, but if you have any more suggestions, they're always welcome :)
nccb nccb

2012/9/11

#
It's not obvious why that would create five planets -- the click should only register once. Where is createPlanet() -- can it be called more than once per frame? For example, if you put the code in a Spaceship class, but you have five spaceship, then every time you click, you'll get five new planets (because each spaceship will create one). Could it be some problem like that?
PoDh34d PoDh34d

2012/9/11

#
nccb, Thanks for your input, a very interessting point you made there. Quite funny how easy it can be if you know what you're doing. :P I'm just a first year student beginning to understand the basics of Java and this really helped me out! Not sure if I solved it the right way, but it works perfectly fine now... I moved the method createPlanets() to another class. Cheers
Nuraini0795 Nuraini0795

2014/11/11

#
where is scenario ?
You need to login to post a reply.