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

2014/1/1

Enemy AI (Move up 5 spaces, then move down 5 spaces)

Moynzy Moynzy

2014/1/1

#
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
shrucis1 shrucis1

2014/1/1

#
I would use code like this in the act 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;
    }
}
Moynzy Moynzy

2014/1/1

#
Hello, schruis1, it's not working for some reason :/ I put the code and nothing happens.
danpost danpost

2014/1/1

#
Do the following with the code shrucis1 provided:
// change line 11 to this:
if (getY() < topY && direction == -1) {
// and line 14 to this:
if (getY() > lowerY && direction == 1) {
Moynzy Moynzy

2014/1/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; } } Still does not move :/ thank you though.
Moynzy Moynzy

2014/1/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;  
            }  
           
   }  
shrucis1 shrucis1

2014/1/1

#
Moynzy, the problem is you need the
int direction = 1;    
int speed = 2;    
int topY = 100;    
int lowerY = 500;
Above the act method. You can't declare them in the act method or the variables get reset every time the act method is called. Also, danpost, the change you suggested doesn't really alter anything...
Moynzy Moynzy

2014/1/1

#
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;  
            }  
           
   }    
}
sorry, i'm new to this, still no dice.
shrucis1 shrucis1

2014/1/1

#
Good, you got everything right except for the piece of code that moves it:
setLocation(getX(), getY() + (direction * speed));
Put that one in the act method, but above the if statements
Moynzy Moynzy

2014/1/1

#
shrucis1, you are a star! I one more solution for you if you don't mind. I will be sure to reference and credit you in my work too.
 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.
        }
        
    }
//If the baby is touching the ball && the space bar is pressed //Set the location of ball to go left 50 spaces. Thank you if you can fix my code for me.
You need to login to post a reply.