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

2011/9/18

"checkKeypress" Help!

Stekpannadeath Stekpannadeath

2011/9/18

#
I'm very new to greenfoot and I am supposed to get a thing to move when i press a certain key. When I write this code below the thing moves without me pressing any key, it just doesn't react to me pressing it. What have done wrong? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Banan here. * * @author (your name) * @version (a version number or a date) */ public class Banan extends Mat { public void act() { checkKeypress(); } public void checkKeypress() { if(Greenfoot.isKeyDown("left")); { setLocation(getX()+1, getY()); } if(Greenfoot.isKeyDown("right")); { setLocation(getX(), getY()); } }
kiarocks kiarocks

2011/9/18

#
it should work, let me see what happens if i try it.
kiarocks kiarocks

2011/9/18

#
here you go,
public void act() 
    {
        checkKeypress();
    }

    public void checkKeypress()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()+1, getY());

        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()-1, getY());

        }  
    }
Stekpannadeath Stekpannadeath

2011/9/18

#
Oh it works! Thank you so so much!
kiarocks kiarocks

2011/9/18

#
your welcome, the semi colons at the end of if statements signal the end of the statement, and the other code is run without any keys pressed.
You need to login to post a reply.