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

2017/10/31

Key Control Sporadic

bigShovel bigShovel

2017/10/31

#
Hi there, I'm teaching coding and we're doing some very basic movement. I have made a project and it simply has a "hero" class and it can be moved by pressing keys (left, right, up or down). I've probably taught this 10 times over the past 5 years using Greenfoot without issue, but this time the program runs okay for awhile and then the hero just stops responding to the key presses, or does so sporadically. Initially I thought it might be an issue with my computer, but my students are able to replicate on their devices. I'm not sure what is wrong? Any thoughts? Here is the only code that is in the hero act() method:
public void act() 
    {
        xPos = getX();
        yPos = getY();
        
        if (Greenfoot.isKeyDown("a"))
        {
            setLocation(xPos-5, yPos);
        }
        else if (Greenfoot.isKeyDown("d"))
        {
            setLocation(xPos+5, yPos);
        }
        else if (Greenfoot.isKeyDown("w"))
        {
            setLocation(xPos, yPos-5);
        }
        else if (Greenfoot.isKeyDown("s"))
        {
            setLocation(xPos, yPos+5);
        }
    }   
I would love to get this solved as my students are getting frustrated with activity one!!! Thanks very much.
danpost danpost

2017/10/31

#
The code, as given, appears to be okay. In fact, I ran it with no issues (win 7/gf 3.1.0 usb). The only thing I can think of is that you could have other code in the project which relocates the hero (like an enemy or obstacle). I would doubt it to be your version of greenfoot, unless you and all your students had the same one on your systems. If you are all on the same network using the same program, you may try re-installing it.
You need to login to post a reply.