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

2012/1/29

if ("space".equals(Greenfoot.getKey()))

programmar programmar

2012/1/29

#
I'm programming a 2-player game where a pikachu and a charmander shoot little fireballs and thunder waves at each other. For the shooting, I am using the
if ("shift".equals(Greenfoot.getKey()))
for the thunder waves, and
if ("v".equals(Greenfoot.getKey()))
for the fire balls. However, only the thunder waves work. Is this because Greenfoot only allows for one if ("v".equals(Greenfoot.getKey())) code?
Spilli Spilli

2012/1/29

#
You should try using a different way for example
if(Greenfoot.isKeyDown("shift")) {
      // Your code here
}
Builderboy2005 Builderboy2005

2012/1/29

#
Greenfoot.ketKey() only returns a single key each act cycle. A way to get around this would be to have:
String key = Greenfoot.keyKey()
In your main world, and instead of using getKey() from then on, you would just use the string key instead.
programmar programmar

2012/1/30

#
Okay, so where would be the best place to put that code? Here is what my world looks like so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bg1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bg1 extends World
{
    /**
     * Constructor for objects of class bg1.
     * 
     */
    public bg1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Ground ground = new Ground();
        addObject(ground, 293, 393);
        Platform platform = new Platform();
        addObject(platform, 291, 193);
        Platform platform2 = new Platform();
        addObject(platform2, 107, 290);
        Platform platform3 = new Platform();
        addObject(platform3, 495, 283);
        Pikachu pikachu = new Pikachu();
        addObject(pikachu, 303, 105);
        Pikachu pikachu2 = new Pikachu();
        addObject(pikachu2, 295, 123);
        pikachu2.setLocation(179, 143);
        removeObject(pikachu2);
        pikachu.setLocation(88, 210);
        Charmander charmander = new Charmander();
        addObject(charmander, 517, 198);
    }
    GreenfootSound music = new GreenfootSound("19 Hyrule Field Main Theme.mp3");  
    public void started()  
    {  
        music.playLoop();  
    }  
  
    public void stopped()  
     {  
        music.stop();  
    } 
}
Also, would my code for shooting projectiles now be:
if ("shift".equals(Greenfoot.keyKey()))
?
danpost danpost

2012/1/30

#
You need to use the 'Greenfoot.getKey()' function for shooting projectiles (unless you want a continuous stream of projectiles while the key is pressed down. If 'shift' is the only key your are checking for (as far as being pressed AND released), you can use 'if ("shift".equals(Greenfoot.getKey()))'. If you are checking for more than the 'shift' key, you will have to set a String variable to what is returned with Greenfoot.getKey(), (for example: 'String key = Greenfoot.getKey();') and then perform your checks on the variable that now contains the keystroke (usually one checks to see if the variable is not 'null' before comparing it to the keystrokes you are looking for). The reason you need to set it to a String variable if more than one keystroke is looked for is that it will only return a keystroke (if one is waiting to be returned) on the first call per act() cycle. All subsequent calls will return null during the same act() cycle.
davmac davmac

2012/1/30

#
I don't think that Greenfoot.getKey() can return "shift" - it's not a key that can be typed, as such. You need to use Greenfoot.isKeyDown(...) to detect it.
Duta Duta

2012/1/30

#
davmac wrote...
I don't think that Greenfoot.getKey() can return "shift" - it's not a key that can be typed, as such. You need to use Greenfoot.isKeyDown(...) to detect it.
Here's a method of using isKeyDown() to return the key only once per press (like with getKey()): You have to have a boolean field (I've called it readEnter in this, but you can obviously rename it) - place this in with your fields: (change enter to shift - I copied this from a post I made in a similar discussion, and am in a hurry)
private boolean readEnter = true;
Then you have something like the following in your act() method:
if(!readEnter && !Greenfoot.isKeyDown("enter"))
        readEnter = true;
And then you just do the following if you want to use isKeyDown():
if(Greenfoot.isKeyDown("enter") && readEnter)
{
    //Whatever you want to do.
    readEnter = false;
}
danpost danpost

2012/1/30

#
I had a feeling that was the case, but when I looked in the Greenfoot API, I saw: Part of the functionality provided by this class is the ability to retrieve keyboard input. The methods getKey() and isKeyDown() are used for this and they return/understand the following key names: •"a", "b", .., "z" (alphabetical keys), "0".."9" (digits), most punctuation marks. getKey() also returns uppercase characters when appropriate. •"up", "down", "left", "right" (the cursor keys) •"enter", "space", "tab", "escape", "backspace", "shift", "control" •"F1", "F2", .., "F12" (the function keys) and in the getKey() description, nothing is mentioned about 'shift' and 'control' not being detected.
davmac davmac

2012/1/30

#
True, the documentation doesn't mention this - we should probably fix that.
programmar programmar

2012/1/31

#
/**
 * Write a description of class Charmander here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Charmander extends Actor
{
    /**
     * Act - do whatever the Charmander wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     private int speed = 7;
   private int vSpeed = 1;
   private int acceleration = 1;
   
    public void act() 
     
    {
        checkKeys();
      checkFall();
      getattacked();
      if(!shoot && !Greenfoot.isKeyDown("enter"))  
        shoot = true; 

    }  
    private boolean shoot = true;
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("a") ) 
        {
            setImage("charleft.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("d") )
        {
            setImage("charright.png");
            moveRight();
        }
        if (Greenfoot.isKeyDown("w") )
        {   setImage("charup.png");
            moveUp();
        }
        if (Greenfoot.isKeyDown("s") )
        {
            setImage("charduck.png");
        }
        if(Greenfoot.isKeyDown("v") && shoot)  
        {  
            flame();  
            shoot = false;  
        }  
        if (Greenfoot.isKeyDown("c") && shoot)
        {
            flamel();
            shoot = false;
        }
    }
    private void flame()
    {
        Ember flame = new Ember();
        getWorld().addObject(flame, getX() + 20, getY());
    }
    private void flamel()
    {
        Ember2 flamel = new Ember2();
        getWorld().addObject(flamel, getX() - 20, getY());
    }
    public boolean onGround()
    {
     Actor under = getOneObjectAtOffset(0, 50, Ground.class);  
     return under != null;  
    }
    public void checkFall()
    {
        if(onGround() ) 
            vSpeed= 0;
        else {
            fall();
        }
    }
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    public void moveRight()
    {
            setLocation ( getX() + speed, getY() );
    }
    public void moveLeft()
    {
            setLocation ( getX() - speed, getY() );
    }
    public void moveUp()
    {
            setLocation ( getX(), getY() - speed * 2);
    }
    private void getattacked()
    {
        Actor Thunderbolt;
        Thunderbolt = getOneObjectAtOffset(0, 0, Thunderbolt.class);
     
      if (Thunderbolt !=null)
      {
        World world;
        world = getWorld();
        world.removeObject(Thunderbolt);
        Greenfoot.playSound("shock.wav");
    }
    }
    }
that's what it looks like so far. it's still shooting multiple projectiles per press.
danpost danpost

2012/1/31

#
In line 23, you have
if(!shoot && !Greenfoot.isKeyDown("enter"))
Since you are using 'v' and 'c' to shoot the flames, change that line to read
if(!shoot && (!Greenfoot.isKeyDown("v") && !Greenfoot.isKeyDown("c")))
In fact, you can reduce the code to just
if(!Greenfoot.isKeyDown("v") && !Greenfoot.isKeyDown("c"))
danpost danpost

2012/1/31

#
An alternate way (more stream-lined) is to change lines 48 through 57 to read
String key = Greenfoot.getKey();
if ("v".equals(key))
{
    flame();
}
if ("c".equals(key))
{
    flame1();
}
and then delete line 27 and lines 23 through 25.
programmar programmar

2012/2/1

#
It works now! Thank you so much to everyone who helped. Now I will add a hit counter.
You need to login to post a reply.