I want my enemy to move up, and then move down blocking a goal. Like a goal keeper.
//move enemy up
//when enemy has moved up
//move enemy down
//loop
int direction = 1;
int speed = 2;
int topY = 100;
int lowerY = 500;
//For the topY and lowerY coordinates, I'm assuming the world is 600 pixels tall
public void act()
{
setLocation(getX(), getY() + (direction * speed));
if(getY() < topY) {
direction = 1;
}
if(getY() > lowerY) {
direction = -1;
}
}// change line 11 to this:
if (getY() < topY && direction == -1) {
// and line 14 to this:
if (getY() > lowerY && direction == 1) { public void act()
{
int direction = 1;
int speed = 2;
int topY = 100;
int lowerY = 500;
if (getY() < topY && direction == -1)
{
direction = 1;
}
if (getY() > lowerY && direction == 1)
{
direction = -1;
}
} int direction = 1; int speed = 2; int topY = 100; int lowerY = 500;
public class defender extends Actor
{
int direction = 1;
int speed = 2;
int topY = 100;
int lowerY = 500;
/**
* Act - do whatever the defender wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (getY() < topY && direction == -1)
{
direction = 1;
}
if (getY() > lowerY && direction == 1)
{
direction = -1;
}
}
}
setLocation(getX(), getY() + (direction * speed));
public void Dribble()
{
Actor player;
player=getOneObjectAtOffset(10,0,player.class);
if ((player !=null) && Greenfoot.isKeyDown("space"))
{
setLocation(getX() - 100, getY());
//For the basic solution this is where you put your new code.
}
}