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

2013/10/31

HotKey to Game

BahJiy BahJiy

2013/10/31

#
I wish to make a game in which the user can change the hotkeys within the game itself. I have gotten to the part where it can read the key's press but it doesn't exactly work correctly. What I want it to do is I click on the text and it start a count down timer and will set time to 0 when I press a button. It will then change the string variable in the Config class to correspond to the new hotkey. If I don't press anything then the timer will just count down and once it reaches 0, it will do nothing, no changes to the Config class.
double time = 50000; // Time allow to change   

                while (time > 0.0) { // Minus the time allow to time and resigter the key pressed               
                    if (Greenfoot.getKey() != null && !Greenfoot.isKeyDown(Hkey)) { // If a key was press change the Config HotKey


                        if (type == 51) Config.x1 = Hkey;
                        else if(type == 52) Config.x2 = Hkey;
                        else if(type == 53) Config.x3 = Hkey;
                        else if(type == 54) Config.x4 = Hkey;
                        else if(type == 55) Config.x5 = Hkey;
                        else if(type == 56) Config.x6 = Hkey;

                        else if(type == 61) Config.y1 = Hkey;
                        else if(type == 62) Config.y2 = Hkey;
                        else if(type == 63) Config.y3 = Hkey;
                        else if(type == 64) Config.y4 = Hkey;
                        else if(type == 65) Config.y5 = Hkey;
                        else if(type == 66) Config.y6 = Hkey;

                        getWorld().addObject(new Text(type, 0), getX(), getY());
                        getWorld().removeObject(this);

                        time = 0; // Quit While Loop
                    } else 
                        time -= 1; // Minus time if no key was press
                }
            }
danpost danpost

2013/10/31

#
I think you are not understanding what the method 'isKeyDown' does. Properly used, that method will return a true/false value indicating whether the supplied key was down or not. It does not return any key and assign it to the field you supplied. What you want to do is save what 'getKey' returns in a field and check its value:
// inside while loop
String key = Greenfoot.getKey();
if (key != null)
{
    switch (type)
    {
        case 51: Config.x1 = key; break;
        case 52: Config.x2 = key; break;
        // etc
    }
    getWorld().removeObject(this);
    return; // exits the method (or 'break;' exits the loop)
}
Sixteen minutes is ample time to press a key to set the configuration. Use 250 or 300 for the initial 'time' value to allow about 5 seconds to press a key. You may need to check all Config values to make sure that the key given is not already being used.
BahJiy BahJiy

2013/10/31

#
I copy the code and it works to an extent. It changes the hotkey to the last pressed key. So if I press a key before clicking the text, it will instantly change the hotkey without allowing me time to press after clicking the text. However, I modify the code so that it somewhat now like the way I intend it to but there's a problem.
int time = 50000; // Time allow to change   
                Greenfoot.getKey(); // Reset Greenfoot.getKey();
                while (time > 0) { // Minus the time allow to time and resigter the key pressed                   
                    String Hkey = Greenfoot.getKey();  // Get the Key
                    System.out.println(Hkey);

                    if (Hkey != null) { // If a key was pressed
                        switch (type) { // Change the Config file according to the "Hkey"
                            case 51: Config.x1 = Hkey; break; 
                            case 52: Config.x2 = Hkey; break;
                            // ect
                        }  

                        // Create new image and remove old image
                        getWorld().addObject(new Text(type, 0), getX(), getY());
                        getWorld().removeObject(this);
                        return; // Break loop
                    }  

                    time--; // Minus time if no Key were press
                }
The problem is that I have to put System.out.println(Hkey) for it to allow changing of hotkey during the count down timer. Without it, the code won't work at all. Is there a way to fix this? Also I can't change the time to 250 or 300 because then the timer only last for about 0.2 sec
danpost danpost

2013/11/1

#
The problem is this. Line 2 is robbing line 4 of every getting a value and the while loop is useless in this case. You need to remove the 'while' loop as well as line 2.
BahJiy BahJiy

2013/11/1

#
If I do that then how can I make the game allow me to change the hotkeys by pressing a key after click the text, not change the hotkey regardless whether I press a key. What happens when I use your code above, is this. I have to press a key then click the text to change the hotkey. If I press a key after clicking the text it does nothing, I have to click the text again for it to change. I want it so (if possible) that I click the text and once I press a key, it changes the hotkey to the key that I press during the timer.
danpost danpost

2013/11/1

#
When a text object is clicked on, set the timer to some value. Then, if the timer is non-zero, check for a key to set the hotkey to.
BahJiy BahJiy

2013/11/1

#
That's what the code above is.
 public void act() {
        /**
         * Do "x" if player click on this
         */
        if (Greenfoot.mouseClicked(this)) {
        // etc
        else if (type < 70){                
                int time = 50000; // Time allow to change   
                Greenfoot.getKey(); // Reset Greenfoot.getKey();
                while (time > 0) {
                // etc (above code)
danpost danpost

2013/11/1

#
Should be more like this:
public void act()
{
    if (time == 0 && type < 70 && Greenfoot.mouseClicked(this)) time = 300;
    if (time > 0)
    {
        time--;
        String key = Greenfoot.getKey();
        if (key == null) return;
        switch (type)
        // etc.
    }
}
BahJiy BahJiy

2013/11/1

#
Thank for the help. I have something to add for others who are doing something like this.
if (time == 0 && type >= 50 && type <= 66 && Greenfoot.mouseClicked(this)) {
            // Add this code in so that i reset the other hotkey timers so only one will be changed.
            List<[name of class]> times = getWorld().getObjects([name of class].class);
            for ([name of class] t : times) {
                t.time = 0;
            }
            time = 250;
        }
You need to login to post a reply.