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

2019/9/3

MouseInfo is always null? - Getting coordinates of the mouse

ihjufaeshiju ihjufaeshiju

2019/9/3

#
I want to spawn an object at the cursor position. However the MouseInfo seems to always be null. (If Crab spawns -> mouse == null) I've also tried this with a while loop instead of for because i thought maybe its just null right after executing the method. The results were the same.
public void SpawnMode() {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null) {
            int x = mouse.getX();
            int y = mouse.getY();
            addObject(new Crab(),x,y);
        }
        addObject(new Crab(),500,500);
    }
danpost danpost

2019/9/3

#
Each time this method is called, a Crab object will be created and placed into the world at (500, 5000. If there is any change in the position or button states of the mouse, one will also be created and placed at the location of the mouse at that time as well. You will probably want to remove line 8 and then add a secondary condition on spawning crabs; otherwise you will be inundated with them. That is, of course, unless you only call the method under specific conditions to begin with.
ihjufaeshiju ihjufaeshiju

2019/9/3

#
What is not working properly is spawning an object at the mouse coordinates as in the if loop. As far as i know the if loop is never executed because mouse is always null. What should happen is an object being spawned at the mouse XY and then another one appearing at x500 and y500. Any other suggestions?
danpost danpost

2019/9/3

#
ihjufaeshiju wrote...
What is not working properly is spawning an object at the mouse coordinates as in the if loop. As far as i know the if loop is never executed because mouse is always null. What should happen is an object being spawned at the mouse XY and then another one appearing at x500 and y500. Any other suggestions?
Please show the codes for the entire class.
nuicweaniufcewa nuicweaniufcewa

2019/9/3

#
Here is the whole scenario. drive<INSERT DOT HERE>google<INSERT DOT HERE>com/open?id=1heIY4mf4MUPbT2QVdmTzgpewzGenFNNA
nuicweaniufcewa nuicweaniufcewa

2019/9/3

#
I kinda solved it myself now. The problem is that MouseInfo is always null while not in run mode. Moving the SpawnMode function over to the Actor worked. Thanks for your reply anyways :)
ihjufaeshiju ihjufaeshiju

2019/9/3

#
Will this be enough? https://drive.google.com/open?id=1heIY4mf4MUPbT2QVdmTzgpewzGenFNNA just right click on the World and execute SpawnMode() method to test it.
danpost danpost

2019/9/3

#
ihjufaeshiju wrote...
Will this be enough? https://drive.google.com/open?id=1heIY4mf4MUPbT2QVdmTzgpewzGenFNNA just right click on the World and execute SpawnMode() method to test it.
Maybe you did not know that you can add an act method into your World subclass codes.
You need to login to post a reply.