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

2013/4/20

isKeyDown with 2 Keys

mufusion mufusion

2013/4/20

#
I want to control the direction of the bullet, when i press space and right it have to fly right when i press space and left it have to fly left and so on i came up with this idea :
 if (Greenfoot.isKeyDown ("space") && ("right"))
            {
                setLocation(getX() + 36, getY());
            }
            if (Greenfoot.isKeyDown ("space") && ("left"))
            {
                setLocation(getX() - 36, getY());
            }

            if (Greenfoot.isKeyDown ("space") && ("up"))
            {
                setLocation(getX()+0, getY ()+36);
            }
            if (Greenfoot.isKeyDown ("space") && ("down"))
            {
                setLocation(getX() + 0, getY()-36 );
            }
but it says that operator && can not be applied to boolean,java.lang.string
Gevater_Tod4711 Gevater_Tod4711

2013/4/20

#
Try it like this:
if (Greenfoot.isKeyDown ("space") && Greenfoot.isKeyDown("right"))  {

}
The compiler error occours because isKeyDown is a boolean and ("right") is just a string and you can't check a string in a if clause.
mufusion mufusion

2013/4/20

#
Thank you, its working now but when i dont hold the keys the bullet stop moving, thats because isKeyDown only work as long as the keys are down, i want to use if("Key".equals(Greenfoot.getKey())) but how can i combine two of them? or can i use 2 keys in one? edit: if("Key".equals(Greenfoot.getKey())) && if("Key".equals(Greenfoot.getKey())) dont work
danpost danpost

2013/4/20

#
The 'getKey' method will only return one key at a time and therefore cannot be used for a combination. Are you wanting your bullet to be controlled by the user as it is moving or are the keys determining the initial direction that the bullet must travel? For initial direction, the code should be placed where the bullet is created, How exactly do you want the keys to control the creation of the bullets? because you say you wanted to use 'getKey', I will presume the space must be released before creating another bullet; what about the arrow key -- can it stay pressed or must it be released also? You can just say my last method was correct if the following is what you want: all arrows and the space must be released before another bullet can be spawned and only one arrow can be pressed at the time the space is pressed to spawn a bullet.
mufusion mufusion

2013/4/20

#
i want to control the direction in which the bullet flys with the arrow keys. the problem is that they only fly when i hold one of the arrow keys, when i release it they stop moving i want to press space and left,right,up,down 1 time and 1 bullet fly in the direction given by the arrow keys until it hit an enemy or the border.
danpost danpost

2013/4/20

#
The the key detection should be used in the creation and set-up of the bullet, not in the bullets action after it is created (meaning the key detection code should be in the class that creates the bullet; not in the bullet class itself). You probably need something like the following:
// add instance boolean to class that creates bullet
private boolean canCreateBullet = true;
// then, in act or method it calls to check creation of bullet
if (canCreateBullet)
{
    String key = Greenfoot.getKey();
    if ("space".equals(key))
    {
        int arrowsDown = 0;
        int dir = 0;
        if (Greenfoot.isKeyDown("right")) arrowsDown++; else dir = 90;
        if (Greenfoot.isKeyDown("down")) arrowsDown++; else dir = 180;
        if (Greenfoot.isKeyDown("left")) arrowsDown++; else dir = 270;
        if (Greenfoot.isKeyDown("up")) arrowsDown++;
        if (arrowsDown == 1)
        {
            Bullet bullet = new  Bullet();
            bullet.setRotation(dir);
            getWorld().addObject(bullet, getX(), getY());
        }
    }
}
else
{
    canCreateBullet = (!Greenfoot.isKeyDown("right") &&
                       !Greenfoot.isKeyDown("down") &&
                       !Greenfoot.isKeyDown("left") &&
                       !Greenfoot.isKeyDown("up"));
}
mufusion mufusion

2013/4/20

#
i put the code in the spieler ( player) code and put move (30); in the bullet void act() the problem ist that only up und left are working. thats because move (30) is left on the x and up on the y axe move (-30) is right and down ( or reverse, im not sure)
danpost danpost

2013/4/20

#
All the bullet has to do is move forward move(+30) and check for hitting objects or world edge.
mufusion mufusion

2013/4/20

#
Yes, the Bullet code is
{
    private boolean atWorldEdge()  
    {  
        return (getX()==0 ||  
            getY()==0 ||  
            getX()==getWorld().getWidth()-1 ||  
            getY()==getWorld().getHeight()-1);  

    }  

    public void act() 
    {
        move (+30);

        hitEnemy();
        if (
        getWorld() != null)
        {

           
            if(atWorldEdge())  
            {  
                getWorld().removeObject(this);  
                return;  
            }  
        }
    }

    
    public void hitEnemy() {

        gegner1 enemy = (gegner1) getOneObjectAtOffset(0, 0, gegner1.class);  
        if (enemy != null) {  
            enemy.setHealth(-1);
            getWorld().removeObject(this);
        }  

    }
}
but it only fly to the left or up because move (+??) is the one direction and move(-??) is the other, when i write move(-30) it moves to the right or down when i write move(+30) it moves to left or up.
danpost danpost

2013/4/20

#
Sorry, that is my fault. It is in the way I coded it. Just give me a minute. Try this instead:
// add instance boolean to class that creates bullet
private boolean canCreateBullet = true;
// then, in act or method it calls to check creation of bullet
if (canCreateBullet)
{
    String key = Greenfoot.getKey();
    if ("space".equals(key))
    {
        int arrowsDown = 0;
        int dir = 0;
        if (Greenfoot.isKeyDown("right")) arrowsDown++; else dir = 90;
        if (Greenfoot.isKeyDown("down")) arrowsDown++; else if (arrowsDown == 0) dir = 180;
        if (Greenfoot.isKeyDown("left")) arrowsDown++; else if (arrowsDown == 0) dir = 270;
        if (Greenfoot.isKeyDown("up")) arrowsDown++;
        if (arrowsDown == 1)
        {
            Bullet bullet = new  Bullet();
            bullet.setRotation(dir);
            getWorld().addObject(bullet, getX(), getY());
        }
    }
}
else
{
    canCreateBullet = (!Greenfoot.isKeyDown("right") &&
                       !Greenfoot.isKeyDown("down") &&
                       !Greenfoot.isKeyDown("left") &&
                       !Greenfoot.isKeyDown("up"));
}
mufusion mufusion

2013/4/21

#
Thank you wery much, my game is almost finished. but now theres again the problem that we had a few days before: when i hold space and one of the arrow keys it can happen that the bullets spawn rapidly and kill the enemies in seconds, thats way to easy.
danpost danpost

2013/4/21

#
There are several ways to regulate the stream of bullets. (1) add a time interval between shots (2) limit the number of shots in the world at one time (3) allow one shot per press of the spacebar Which way do you want to proceed?
mufusion mufusion

2013/4/21

#
the third way, we did it before with :
    // instance field to add  
    private boolean spaceDown;  
    // in act or a method it calls  
    if (!spaceDown && Greenfoot.isKeyDown("space"))  
    {  
        spaceDown = true;  
        getWorld().addObject(new Bullet(), getX(), getY());  
    }  
    if (spaceDown && !Greenfoot.isKeyDown("space"))  
    {  
        spaceDown = false;  
    }  
but i dont know how to combine it with the code above
danpost danpost

2013/4/21

#
To combine the two (this is one way):
// instance fields
private boolean spaceDown;
private boolean canCreateBullet = true;
// act method (or method it calls)
if (!canCreateBullet &&
    !Greenfoot.isKeyDown("right") &&
    !Greenfoot.isKeyDown("down") &&
    !Greenfoot.isKeyDown("left") &&
    !Greenfoot.isKeyDown("up")) canCreateBullet = true;
if (spaceDown && !Greenfoot.isKeyDown("space")) spaceDown = false;
if (spaceDown || !canCreateBullet) return;
if (Greenfoot.isKeyDown("space"))
{
    spaceDown = true;
    int arrowsDown = 0;
    int dir = 0;
    if (Greenfoot.isKeyDown("right")) arrowsDown++; else dir = 90;
    if (Greenfoot.isKeyDown("down")) arrowsDown++; else if (arrowsDown == 0) dir = 180;
    if (Greenfoot.isKeyDown("left")) arrowsDown++; else if (arrowsDown == 0) dir = 270;
    if (Greenfoot.isKeyDown("up")) arrowsDown++;
    if (arrowsDown == 1)
    {
        Bullet bullet = new  Bullet();
        bullet.setRotation(dir);
        getWorld().addObject(bullet, getX(), getY());
        canCreateBullet = false;
    }
}
Another way is to combine 'spaceDown' with 'canCreateBullet' as follows:
// instance field
private boolean canCreateBullet = true;
// act method (or method it calls)
if (!canCreateBullet)
    if (!Greenfoot.isKeyDown("right") &&
        !Greenfoot.isKeyDown("down") &&
        !Greenfoot.isKeyDown("left") &&
        !Greenfoot.isKeyDown("up") &&
        !Greenfoot.isKeyDown("space"))
            canCreateBullet = true;
    return;
}
if (Greenfoot.isKeyDown("space"))
{
    canCreateBullet = false;
    int arrowsDown = 0;
    int dir = 0;
    if (Greenfoot.isKeyDown("right")) arrowsDown++; else dir = 90;
    if (Greenfoot.isKeyDown("down")) arrowsDown++; else if (arrowsDown == 0) dir = 180;
    if (Greenfoot.isKeyDown("left")) arrowsDown++; else if (arrowsDown == 0) dir = 270;
    if (Greenfoot.isKeyDown("up")) arrowsDown++;
    if (arrowsDown == 1)
    {
        Bullet bullet = new  Bullet();
        bullet.setRotation(dir);
        getWorld().addObject(bullet, getX(), getY());
    }
}
If you want to allow the arrow key to be continuously pressed while the spacebar creates multiple bullets (by indivudual presses), the remove the conditions related to the arrows in the first 'if' statement in the act method (in the first code-set, remove the first 'if' block in its entirety and remove any code related to the boolean 'canCreateBullet').
mufusion mufusion

2013/4/21

#
now its working like i wanted it. Well, Thats it! im Finished. it dont look wery good and is not as funny as the games made by professionals but i only had 1 and a half week and nearly no experience ( we worked with the first 3 chapters of the book in school, thats nearly anything we learned about the topic) Thank you very much for you support, not just in the topics i created, also in old topics i read and your help not only helped me, it helped also my teacher who is too new at greenfoot and he helped with your help the others in school. if you want it, i mention you in the game credits, but i dont know if i make the game puplic.
You need to login to post a reply.