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

2017/10/20

How can i get my two actors to fire once per button press?

SPCWW SPCWW

2017/10/20

#
I have been playing around with a two player game idea but I cant quite get it to work as I am trying to use the if ("space".equals(Greenfoot,getKey())) command to shoot and the same with a different button on the enemy however it only seems to work on one of the actors and I have been searching for a different method however I cant find one.
danpost danpost

2017/10/20

#
The 'getKey' method will only return one key per act cycle and only on the first call to the method. Use the 'isKeyDown' method with a boolean field to track the state of each key. For example:
// instance field
private boolean spaceDown; // to track state of 'space' key

// to shoot
if (Greenfoot.isKeyDown("space") != spaceDown) // if change in state
{
    spaceDown = ! spaceDown; // record new state
    if (spaceDown) shoot(); // if key pressed, shoot
}
SPCWW SPCWW

2017/10/21

#
That doesn't seem to work and on one of the actors it just spams the bullets without me pressing anything and on the other it just displays my gameover message whenever I press the key to shoot.
danpost danpost

2017/10/21

#
SPCWW wrote...
That doesn't seem to work and on one of the actors it just spams the bullets without me pressing anything and on the other it just displays my gameover message whenever I press the key to shoot.
Show the classes where you are using the codes. You probably got something misplaced.
SPCWW SPCWW

2017/10/21

#
It turns out I forgot to remove shoot from the act class however I am still having problems with the other one ending the game when it shoots.
    private boolean spaceDown;
    
    public void check()
    {
    if (Greenfoot.isKeyDown("space") != spaceDown)
        {
            spaceDown = ! spaceDown;
            if (spaceDown) shoot();
        }
    }
    public void shoot()
    {
        RBullet bullet = new RBullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.setRotation(getRotation());
    }
}
SPCWW SPCWW

2017/10/21

#
Actually I just figured out I was using the wrong bullet so as soon as I fired it was killing me. Sorry about that.
You need to login to post a reply.