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

2017/10/23

clickAndGen

xusui xusui

2017/10/23

#
I'm trying to make it so when I left click, it places ONE planet at the cursor, but instead, it creates planets for as long as you hold the left click down. I tried to use sort of a "flip-switch" system.
private void clickAndGen() {

        MouseInfo mouse =  Greenfoot.getMouseInfo();
        if(mouse != null) {
            int button = mouse.getButton();
            boolean isDown = false;

            if(button == 1 && !isDown)
            {
                int msx = mouse.getX();
                int msy = mouse.getY();
                addObject(new Body(false, Greenfoot.getRandomNumber(100)+1, Greenfoot.getRandomNumber(300)+1, new Vector(getRandomNumber(0,360),getRandomNumber(-2,1)), new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255))), msx, msy);
                isDown = true;
            }
            else if(isDown && button != 1) {
                isDown = false;
            }
        }
    }
}
Help would be much appreciated!
danpost danpost

2017/10/23

#
You are getting responses to button action(s) -- but, you are not specifying what action. Use the 'mouseClicked' method of the Greenfoot class (with that, you will not need the 'boolean isDown' variable).
You need to login to post a reply.