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

2014/4/1

Problem with Greenfoot.isKeyDown().

Croco Croco

2014/4/1

#
I'm making an application where buttons are either toggled on or off. (Boolean available) I want to use multiple inputs to reset the status of the buttons to available=false. However, the buttons do not change status with this code except for the spacebar one. Is there different syntax I need to use to get Greenfoot.isKeyDown() to work with these numbers. It's actually not working for basic inputs like "w","a","s", and "d" either. Any help would be greatly appreciated.
public Button(String day,int time)
    {
        
    }
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))
        {
            available=true;
        }
        if(available)
        {
            setImage("buttonTrue.png");
        }
        if(Greenfoot.isKeyDown("space"))
        {
            available=false;
        }
        if(Greenfoot.isKeyDown("Q"))
        {
            available=false;
        }
        if(Greenfoot.isKeyDown("2"))
        {   
            available=false;
        }
        if(Greenfoot.isKeyDown("three"))
        {
            available=false;
        }
        if(Greenfoot.isKeyDown("Four"))
        {
            available=false;
        }
    }    
danpost danpost

2014/4/1

#
The code as given will set 'available' to 'true' when the object is clicked on and to 'false' when 'space', 'q', or '2' is pressed. 'three' and 'Four' are not valid key names and will never trigger 'available' to be 'false'. It might help if you had an 'else' part in setting the image so you could see when the value changes.
Croco Croco

2014/4/3

#
Thank you so much, Danpost. I did end up adding an else to all of the button press checks. I realized that I also overlooked the fact that I should probably set an image for when available=false. Haha. So, for "q", and "2", it might have been working, and I just didn't have a visual cue to realize. However thank you very much for your help. have a good day! :D
You need to login to post a reply.