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

2016/6/16

Right key works, but left doesn't Help!

MNastasi MNastasi

2016/6/16

#
public void act() { //checkKeyPress(); if("right".equals(Greenfoot.getKey())) { //getWorld().removeObject(this); //getWorld().addObject(new Goalie(),326, 261); //goalie dives to the right if right arrow key is pressed //move(50); setLocation(getX()+100, getY()); } if("left".equals(Greenfoot.getKey())) { //getWorld().removeObject(this); //getWorld().addObject(new FlippedGoalie(),326, 261); //goalie dives to the left if left arrow key is pressed setLocation(getX()-100, getY()); } }
Super_Hippo Super_Hippo

2016/6/16

#
I think it would work this way:
String key = Greenfoot.getKey();

if ("right".equals(key))
{
    //...
}
if ("left".equals(key))
{
    //...
}
I personally avoid using the getKey-method and use the isKeyDown-method though.
danpost danpost

2016/6/16

#
Super_Hippo wrote...
I personally avoid using the getKey-method and use the isKeyDown-method though.
As you should -- the getKey' method is best suited for string input where typing in words or numbers outside of gameplay is required.
You need to login to post a reply.