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

2012/9/30

Mouse click

Stephon231 Stephon231

2012/9/30

#
how do i make it so that when the mouse clicks on a area in the world a bomb will be placed, i mean put in by a player not when im adding actors to the world
danpost danpost

2012/9/30

#
What have you tried (and where)?
Gevater_Tod4711 Gevater_Tod4711

2012/9/30

#
If you want the bomb to be placed only if you clicked at a specific area you first have to know where the mouse cursor is and then add the bomb if the mouse is clicked:
// add this variables as globals;
private int x = 0;
private int y = 0;

public void act() {
    ...
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse != null) {
        x = mouse.getX();
        y = mouse.getY();
        if (Greenfoot.mouseClicked(null)) {
            if (x > minX && x < maxX && y > minY && y < maxY) {//minX, maxX, ... should be the coordinates for the field in which you want the actor to add a bomb;
                getWorld().addObject(new Bomb(), x, y);//adds the Bomb at the coordinates of the mouse. If you want the bomb on other coordinates chage x and y;
            }
        }
    }
    ...
}
Stephon231 Stephon231

2012/9/30

#
would this go in an actor or a world
Gevater_Tod4711 Gevater_Tod4711

2012/9/30

#
both is possible. but if you add it in a world you have to write addObject(...) instead of getWorld().addObject(...
Stephon231 Stephon231

2012/9/30

#
so you would put 17, 9, 64 (the size of the world) for minX and MaxX
Gevater_Tod4711 Gevater_Tod4711

2012/10/1

#
If in the construktor of the world (probably the first method in world: public world() { ...)) stands code like this: super(17, 9, 64) minX would be 0, maxX would be 16, minY would be 0 and maxY would be 8.
You need to login to post a reply.