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

2017/11/26

GetKey() Question

abot1776 abot1776

2017/11/26

#
Hi, I was working on a Greenfoot project, and my first step was creating two boats with cannonballs that can hit each other. For some reason, my program will not work. I created a LeftBoat and a RightBoat with Cannonball (for the LeftBoat) and RightCannonball subclasses. I will post the code for my LeftBoat, Cannonball, and World classes. Thank you so much!
public class Cannonball extends Actor
{
    private int life = 1000;
    /**
     * Act - do whatever the Cannonball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(5);
        life--;
        if (getX()==0 || getX() == getWorld().getWidth()-1)
        {
            setRotation(180-getRotation());
        }
        if (getY() == 0 || getY() == getWorld().getHeight()-1)
        {
            setRotation(360-getRotation());
        }
        if(life == 0)
        {
            getWorld().removeObject(this);
        }
    }
}
    public void act() 
    {
        checkKeys();
        if(Greenfoot.isKeyDown("s"))
        {
                turn(-3);
        }   
        if( Greenfoot.isKeyDown("f"))
        {
            turn(3);
        }    
        if( Greenfoot.isKeyDown("e"))
        {
            move(3);
        }    
        if( Greenfoot.isKeyDown("d"))
        {
            move(-3);
        } 
    }

    private void checkKeys()
    {
        String key = Greenfoot.getKey();
        if ("q".equals(key))
        {
            shoot();
        }
    }
    
    public void shoot()
    {
        Cannonball rCannonball = new Cannonball();
        getWorld().addObject(rCannonball, getX(), getY());
        rCannonball.setRotation(getRotation());
    }

}
public class Ocean extends World
{
    public static int leftWins = 0;
    public static int rightWins = 0;
    /**
     * Constructor for objects of class Ocean.
     * 
     */
    public Ocean()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        RightBoat rboat = new RightBoat();
        addObject(rboat, 598, 398);
        
        LeftBoat lboat = new LeftBoat();
        addObject(lboat, 2, 2);
    }
}
danpost danpost

2017/11/26

#
Only one object is allowed to use 'getKey' on any single act cycle. The reason being is that the method will only return an entered key once. You will need to either have the world save any key entered on any act cycle so the actors can access to the saved value or you will need to track the state of each key in the classes that use them so you can determine the exact act cycle the key was pressed or released.
abot1776 abot1776

2017/11/26

#
Could you let me know how I could implement that into my program? I have been trying to do that for over an hour now. Thanks!
danpost danpost

2017/11/27

#
abot1776 wrote...
Could you let me know how I could implement that into my program? I have been trying to do that for over an hour now.
Let us say you wanted to track the state of the "q" key. Then:
// add field to track state of "q" key
private boolean qDown; // default value is 'false'

// in act
    // check if state of "q" key has changed
    if (qDown != Greenfoot.isKeyDown("q"))
    {
        // record new state
        qDown = ! qDown
        // check new state: was key just pressed
        if (qDown) // or:   if (qDown == true)
        {
            // perform triggered action here
        }
    }
abot1776 abot1776

2017/11/27

#
Thank you so much! I may have another question or so while working on this, so I really appreciate it.
You need to login to post a reply.