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

2014/2/21

Getting my dragon to "flap" when I click the mouse button?

askgriff askgriff

2014/2/21

#
I know it's a simple fix but I have tried multiple ways to get this working. I want my dragon to flap its' wings when someone clicks the mouse button. I currently have (which isn't working, obviously):
if(Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("up") || Greenfoot.mousePressed(null))
        {
            wingFlap.play(); 
            flap();
        }
Flappy Dragon Scenario
lordhershey lordhershey

2014/2/21

#
Make it kick off a flap action that takes several cycles to go through and successive calls to the starter function will have no effect until the flap cycle has completed. Is that what you are looking for? maybe use Greenfoot.getMouseInfo().getClickCount() > 0 ?
danpost danpost

2014/2/21

#
You just need an instance boolean field to track the state of the mouse:
// add this instance field
private boolean mouseDown;
// change 'checkKey' method to this
public void checkKey()
{
    // check mouse pressed
    if (!mouseDown && Greenfoot.mousePressed(null)) mouseDown = true;
    // check mouse release
    if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
    if(Greenfoot.isKeyDown("space") || Greenfoot.isKeyDown("up") || mouseDown)
    {
        wingFlap.play(); 
        flap();
    }
}
askgriff askgriff

2014/2/21

#
Thanks Danpost! Much better!
You need to login to post a reply.