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

2013/3/22

Using getKey()

SlothKing SlothKing

2013/3/22

#
When I use the getKey() method I can only seem to get it to work on one key. As in I can put if("space".equals(Greenfoot.getKey())) and if("q".equals(Greenfoot.getKey())) but it will only work for space not q. any help appreciated
Gevater_Tod4711 Gevater_Tod4711

2013/3/22

#
You could try to use Greenfoot.isKeyDown(String keyname). If this also doesn't work we need to see your code to tell you what's wrong with it.
SlothKing SlothKing

2013/3/22

#
public class MainCharacter extends Actor
{
    public int vSpeed = 0;
    public int walk = 0;
    private GreenfootImage imag;
    public int acceleration = 2;
    private GreenfootImage image1 = new GreenfootImage("IceMan-Facing-Right.png");
    private GreenfootImage image2 = new GreenfootImage("IceMan-Running-Right.png");
    private GreenfootImage image7 = new GreenfootImage("IceMan-Facing-Left.png");
    private GreenfootImage image8 = new GreenfootImage("IceMan-Running-Left.png");
    public int direc = 0;
    public int dir = 0;
    public int shot = 0;

    /**
     * Act - do whatever the MainCharacter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public MainCharacter()
    {
        setImage (image1);
    }

    public void act() 
    {
        checkShoot();
        checkJump();
        fallCheck();
        walk();
        checkDir();

    }

    public void checkDir()
    {
        if (shot == 1)
        {
            dir = 1;
            direc = 36;
        }
        if (shot == 2)
        {
            dir = 1;
            direc = 36;
        }
        if (shot == 7)
        {
            dir = 0;
            direc = -36;
        }
        if (shot == 8)
        {
            dir = 0;
            direc = -36;
        }
    }

    public void walk()
    {
        walk++;
        if(Greenfoot.isKeyDown("right"))
        {
            walkRight();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            walkLeft();
        }
    }

    public void walkRight()
    {
        if(walk<10)
        {
            setImage(image1);
            shot = 1;
        }
        if(walk>10)
        {
            setImage(image2);
            shot = 2;
        }
        if (walk > 20)
        {
            walk = 0;
        }
    }

    public void walkLeft()
    {
        if(walk<10)
        {
            setImage(image7);
            shot = 7;
        }
        if(walk>10)
        {
            setImage(image8);
            shot = 8;
        }
        if (walk > 20)
        {
            walk = 0;
        }  

    }

    public void fallCheck()
    {
        if (onPlatform())
        {
            vSpeed = 0;
        }
        else 
        {
            fall();
        }
    }

    public boolean onPlatform()
    {
        Actor uPlat = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
        return uPlat != null;
    }

    public boolean touchPlatform()
    {
        Actor uPlat = getOneObjectAtOffset(getImage().getWidth()/2,0,Platform.class);
        return uPlat != null;
    }

    public void fall()
    {
        setLocation (getX(),getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }

    public void jump()
    {
        vSpeed = -16;
        fall();
    }

    public void checkJump()
    {
        if("space".equals(Greenfoot.getKey()))
        { 
            jump();
        }

    }

    public void checkShoot()
    {
        if("z".equals(Greenfoot.getKey()))
        {
            shootIce();
        }  
    }

    public void shootIce()
    {
        getWorld().addObject(new Iceshot(dir),getX()+direc,getY());   
    }
}
Alright here is the code. My problem is I cannot have it repeating everytime I want it to go off once.
SlothKing SlothKing

2013/3/22

#
I put all the code in case it would help. I want to be able to shoot and jump but It will only let me do one or the other. As in checkShoot or or checkJump.
danpost danpost

2013/3/22

#
The problem is not that you are using 'getKey'. The problem is that you are using 'getKey' multiple times within the same act cycle. The first call will return either null or a String representation of a key that has been pressed and released. Any following calls within the same act cycle will only return null. The easy fix is to assign a local String variable to the key returned.
String key = Greenfoot.getKey();
Then you can compare 'key' to multiple possibilities (i.e. "space", "q", etc.). Since you are using multiple methods, you can either make the variable an instance object field or pass the String value to each method called.
danpost danpost

2013/3/22

#
Better might be to have one method called 'checkKeys' to replace 'checkJump' and 'checkShoot' as follows:
private void checkKeys()
{
    String key = Greenfoot.getKey();
    if ("space".equals(key)) jump();
    if ("z".equals(key)) shootIce();
}
You need to login to post a reply.