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

2018/8/9

Programming A Bot!

comlink comlink

2018/8/9

#
Hey Guys, I need help for a project on Greenfoot. Can any of guys tell me, how I can programm a "bot", which repeatedly presses a key. Imagine: for example: I press the "SPACE" button once and because of that, the bot keeps pressing SPACE over and over again: Do you know how this or something similar could work? Thanks in advance!
Super_Hippo Super_Hippo

2018/8/9

#
The bot (obviously) won't physically press your space button and it also won't press the space button for you in a context out of your project. But inside your project, you could set a variable.
//instead of
if (Greenfoot.isKeyDown("space"))
{
    shoot();
}
//you can do
private boolean shooting=false, spaceDown=false;

//in act
if (Greenfoot.isKeyDown("space") && !spaceDown)
{
    spaceDown = true;
    shooting = !shooting;
}
else (spaceDown && !Greenfoot.isKeyDown("space")) {spaceDown = false;}


if (shooting) shoot();
The first one executes the 'shoot' method whenever and as long you hold the space button. In the second one the user can toggle shooting or not shooting with a click on space.
comlink comlink

2018/8/9

#
Okay, I'll try it out! Thanks for your quick response!
You need to login to post a reply.