So I am working on a basic game where it is an ant vs a spider (I won't get into all the details) but I need help making it so when a key is pressed an actor (SpiderWeb) appears at another actor (Spider). Any help would be appreciated.
if (Greenfoot.isKeyDown("space"))
{
addObject(new SpiderWeb(), getX(), getY());
}
public class Spider extends Actor
{
public void act()
{
if (Greenfoot.isKeyDown ("w"))
move (4);
if (Greenfoot.isKeyDown ("s"))
move(-4);
if (Greenfoot.isKeyDown ("a"))
turn (-5);
if (Greenfoot.isKeyDown ("d"))
turn (5);
Actor Ant1;
Ant1 = getOneObjectAtOffset(0, 0, Ant1.class);
if (Ant1 != null)
{
World world;
world = getWorld();
world.removeObject(Ant1);
}
if (Greenfoot.isKeyDown("e"))
{
addObject(new Web(), getX(), getY());
}
}
}
getWorld().addObject(new Web(), getX(), getY());
private boolean web = true;
if (Greenfoot.isKeyDown("e") && boolean.web = true)
{
getWorld().addObject(new Web(), getX(), getY());
private boolean web = false;
}
// the boolean field
private boolean eKeyDown;
// in your code
if (eKeyDown != Greenfoot.isKeyDown("e")) // is state changing
{
eKeyDown = !eKeyDown; // save new state
if (eKeyDown) // was just pressed
{
getWorld().addObject(new Web(), getX(), getY());
}
}