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

2012/2/21

Sequence of Button Inputs

programmar programmar

2012/2/21

#
How can I make it so that a specific sequence of button inputs would result in an action? For example, pressing p, then i, then e to make some pie appear.
danpost danpost

2012/2/21

#
You would need an instance String variable that is built as the keys are pressed; and compare it the whatever each possible word (or words) you expect the user to type out.
String builder = "";

public void act()
{
    String key = Greenfoot.getKey();
    if (key != null && key.length() == 1)
    {
        builder = builder + key;
        if (builder.endsWith("pie"))
        {
            getWorld().addObject(new Pie(), 
                    Greenfoot.getRandomNumber(getWorld().getWidth()),
                    Greenfoot.getRandomNumber(getWorld().getHeight()));
            builder = ""; // once a string is found, we can clear builder
        }
        if (builder.endsWith( // etc.
        //  rest of act
}
danpost danpost

2012/2/21

#
You might want to change line 8 to
        builder = builder + key.toLowerCase();
to make it case in-sensitive.
programmar programmar

2012/2/24

#
so I have
    private int speed = 7;
   private int vSpeed = 1;
   private int acceleration = 1;
   String builder = "";
   
    public void act() 
     
    {
        checkKeys();
        checkFall();
        getattacked();
        String key = Greenfoot.getKey();  
         if (key != null && key.length() == 1)  
         {  
             builder = builder + key.toLowerCase();  
             if (builder.endsWith("hot"))  
             {  
                 setImage("pikachuduckpro.png");
                 builder = "";

             }  
         }
    }  
but it still doesn't work. What should I change?
danpost danpost

2012/2/24

#
In line 18: setImage takes an image as the parameter (not a string); replace line 18 with
setImage(new GreenfootImage("pikachuduckpro.png"));
davmac davmac

2012/2/24

#
danpost, there is a setImage method accepting a String argument (as well as one accepting a GreenfootImage). programmar: what do you mean by "it still doesn't work"? Can you be a bit more specific - are you getting a compiler error, or does it simply not do what you want?
programmar programmar

2012/2/24

#
davmac, there's no compiler error but it just doesn't do anything when the button sequence is pressed.
danpost danpost

2012/2/24

#
programmar, I am curious, what does checkKeys() look like? (it may not be working because of something there)
davmac davmac

2012/2/25

#
Good question. Are you calling Greenfoot.getKey() more than once?
programmar programmar

2012/2/25

#
public class Pikachu extends Actor
{
    /**
     * Act - do whatever the Pikachu 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;
   String builder = "";
   
    public void act() 
     
    {
        checkKeys();
        checkFall();
        getattacked();
        String key = Greenfoot.getKey();  
         if (key != null && key.length() == 1)  
         {  
             builder = builder + key.toLowerCase();  
             if (builder.endsWith("hot"))  
             {  
                 setImage(new GreenfootImage("pikachuduckpro.png"));
                 builder = "";

             }  
         }
    }  
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") ) 
        {
            setImage("pikafightleftpro2.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") ) 
        {
            setImage("pikfightrightpro.png");
            moveRight();
        }
        if (Greenfoot.isKeyDown("up") ) 
        {   setImage("pikajumppro.png");
            moveUp();
        }
        if (Greenfoot.isKeyDown("down") ) 
        {
            setImage("pikachuduckpro.png");
        }
        if ("shift".equals(Greenfoot.getKey()))
        {
            thunderbolt();
        }
    }
    private void thunderbolt()
    {
        Thunderbolt thunderbolt = new Thunderbolt();
        getWorld().addObject(thunderbolt, getX(), getY());
        thunderbolt.setRotation(Greenfoot.getRandomNumber(360));
    }
    private void thunder()
    {
        Thunder thunder = new Thunder();
        getWorld().addObject(thunder, getX(), getY() - 100);
    }
    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 Ember;
     Ember = getOneObjectAtOffset(0, 0, Ember.class);
      if (Ember !=null)
      {
        bg1 world;
        world = (bg1) getWorld();
        world.removeObject(Ember);
        Greenfoot.playSound("burn.wav");
        world.bar.subtract(3);
    }
     Actor Ember2;
     Ember2 = getOneObjectAtOffset(0, 0, Emberl.class);
     
      if (Ember2 !=null)
      {
            bg1 world;  
        world = (bg1) getWorld();  
        world.removeObject(Ember2);
        Greenfoot.playSound("burn.wav");
        world.bar.subtract(3); 
    }
    }
    }
that's pretty much everything. yes, i use Greenfoot.getKey() more than once.
davmac davmac

2012/2/25

#
yes, i use Greenfoot.getKey() more than once.
This is your problem. It returns the key that was pressed - whatever that is - the first time it's called; the second time it's called it will pretty much always return null. See the documentation for the method: Get the most recently pressed key, since the last time this method was called Seeing as you call it twice, you would have to press a key exactly in between those two times that it is called in order for the second time to return something other than null - extremely unlikely! In any case, I don't think that Greenfoot.getKey() can detect the shift key (it's an omission in the documentation) so your first call (line 50 in the code above) is useless. Use Greenfoot.isKeyDown() for that instead,
danpost danpost

2012/2/25

#
An easy fix might be to move lines 18 through 28 and insert them at line 50 and change what was on line 50 to
if ("space".equals(key))
most programmers will use the space bar to initiate firing an object.
programmar programmar

2012/2/25

#
It works now :) thanks again. By changing the firing method to if ("space".equals(key)) it fired multiple projectiles with one key press. I changed it to the code from http://www.greenfoot.org/topics/802.
You need to login to post a reply.