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

2012/4/20

Working with keyboard in greenfoot

montseviyu montseviyu

2012/4/20

#
Hello, I am a beginner in Greenfoot and i have trouble with understanding how to manage the keyboard. My problem is: I have a class TestWorld with an actor Texto. What iI want is showing in Texto the key that has been pressed (whatever key). How can I detetect that a key has been pressed? And wich is the best act() method to put it this code, in act() of testWorld or in act() of Texto. This is my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * */ public class TestTeclado extends World { private Texto texto; /** * Constructor para objetos de clase TestTeclado. * */ public TestTeclado() { // Crea un nuevo mundo de 600x400 celdas con un tama�o de celda de 1x1 pixeles. super(600, 400, 1); GreenfootImage bg = getBackground(); bg.setColor(Color.BLACK); bg.fill(); inicializar(); } /** * */ private void inicializar() { texto = new Texto("Tecla pulsada :"); this.addObject(texto, 80, 332); } public void act() { } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * */ public class Texto extends Actor { private String texto; public Texto(String texto) { this.texto = texto; this.setImage(new GreenfootImage(texto, 20, Color.WHITE, Color.BLACK)); } /** * Act - hace lo que Texto quiere hacer. Este m�todo se llama "cuando quiera" o whenever * los botones 'Actuar or 'Ejecutar' son presionados en el entorno. */ public void act() { String tecla = Greenfoot.getKey(); if ( ! tecla.equals(null)) // bad code, if no key is pressed tecla is null and fails this.actualizar(Greenfoot.getKey()); } /** * */ public void actualizar(String tecla) { this.getImage().clear(); this.getImage().drawString(texto + " " + tecla, this.getX(), this.getY()); } }
h123n h123n

2012/4/20

#
In order for it to know when a key is down,you use if Greenfoot.isKeyDown("up"); { code here }
davmac davmac

2012/4/20

#
Correct version of the above:
if (Greenfoot.isKeyDown("up"))
{ 
    code here
}
Brackets around the 'if' condition expression, and definitely no semicolon at the end of it!
xillius200 xillius200

2012/4/21

#
if ("up".equals(Greenfoot.getKey())
{
    Do Stuff
}
just another way to do stuff xD
Busch2207 Busch2207

2012/4/21

#
xillius200 wrote...
if ("up".equals(Greenfoot.getKey())
{
    Do Stuff
}
just another way to do stuff xD
That's not completely true... with Greenfoot.isKeyDown(String) you can check, if more keys are pressed at once. e.g. with:
if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("left"))
{}
With Greenfoot.getKey, you just can check for one key.
xillius200 xillius200

2012/4/21

#
true but it's still an alternative if only one key is needed ;) plus you can mix and match xD if ("space".equals(Greenfoot.getKey()) && Greenfoot.isKeyDown("right")) { Do Stuff } btw Busch2207 know anyway of checking for example if you want one space bar press to create only one bullet and they have to release the space bar and re press to shoot another bullet?? atm I have a shoot delay so it's one bullet but still would be nice to know if possible xD any ideas??
Busch2207 Busch2207

2012/4/21

#
xillius200 wrote...
if ("space".equals(Greenfoot.getKey()) && Greenfoot.isKeyDown("right")) { Do Stuff }
this will just work if space was pressed as the last key, but if you want that, it'll work of course ;) Try this for your problem:
    private boolean bool_SpaceDown=false;

    private void KeyCheck()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            if(!bool_SpaceDown)
            {
                // Do Whatever should be done when space was pressed
                bool_SpaceDown=true;
            }
        }
        else
            bool_SpaceDown=false;
    }
That just do, what is in the bracket, if the space key was changed from a none pressed to a pressed status.
xillius200 xillius200

2012/4/21

#
naa still shoots loads instead of one at a time :'( thanks for trying to help though :)
Busch2207 Busch2207

2012/4/21

#
What u mean then? Cause the method, I wrote checks, if the spacebar was repressed. (From not pressed to pressed)
You need to login to post a reply.