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

2014/11/26

Move Enemies from left to right and vice versa.

Anne2403 Anne2403

2014/11/26

#
My game is doing just a little bit well. haha. anyways, I want to make my game more challenging.. I wanted to move the enemies from left to right and then vice versa while shooting. For example, I have a set of actors of pig who's from left , I wanted to move it to right and there's another actor, a set of cow who's from right and I wanted to move it to right while shooting. IS THIS POSSIBLE?? HOW AM I GONNA DO IT???
danpost danpost

2014/11/26

#
'move(1);' for the pig and 'move(-1);' for the cow; or 'setLocation(getX()+1, getY());' for the pig and 'setLocation(getX()-1, getY());' for the cow. You can find descriptions of these methods and others that can be used for actors here.
Anne2403 Anne2403

2014/11/27

#
I did it @danpost but the problem is they just move on the side and stuck there at the edge of the world but I wanted them to move continuosly while shooting until they got shot by the player. And another I did make shoot the enemies but the problem there's a lot a fires that is shooting by the enemies and the fires that is shooting by the enemies are not moving down like I wanted to happen. :( how am I gonna fix this??
danpost danpost

2014/11/27

#
Set the rotation of the actors in the direction you want them to go. At zero rotation, they will go right, and 90 degrees rotation, they will go down. As the rotation increases, their direction of movement using the 'move' method will continue to turn clockwise. At very slow speeds however, the number of possible direction your actor can go will be limited because they cannot end up in-between pixels on the screen.
Anne2403 Anne2403

2014/11/28

#
Okay thank you @danpost. I will try to change the rotation of my actors. Hope it will work.
Anne2403 Anne2403

2014/11/28

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Chicken here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Chicken extends Actor
{
   public void act()
   {
       move();
       shoot();
   }
   public void move()
   {
       if(Greenfoot.getRandomNumber(200) < 30)
       {
           move(Greenfoot.getRandomNumber(110) - 50);
       }
       if(getX() <= 5 || getY() >= getWorld().getWidth() -5)
       {
           move(0);
       }
       if(getY() <= 5 || getY() >= getWorld().getHeight()-5)
       {
           move(-0);
        }
       setLocation(getX()-5, getY());
    }
   public void shoot()
   {
       getWorld().addObject(new Fire(), getX(), getY());
       setLocation(getX()+90,getY());
}
}
What did I do wrong? It didn't move right and didn't fire downward -_-
danpost danpost

2014/11/28

#
First, lets review what code you have here (I removed your comments and added mine)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import greenfoot.*;
// import java.util.*;     *******    this is not needed in this class   *******
 
public class Chicken extends Actor
{
   public void act()
   {
       move();
       shoot();
   }
 
   public void move()
   {
       if(Greenfoot.getRandomNumber(20) < 3)
       // approximately 8 times a second
           move(Greenfoot.getRandomNumber(110) - 50);
       } //  shift left and right averaging about 5 right one every about 20 acts
       /**    does absolutely nothing
       if(getX() <= 5 || getY() >= getWorld().getWidth() -5)
       {
           move(0);
       }
       if(getY() <= 5 || getY() >= getWorld().getHeight()-5)
       {
           move(-0);
        }
        */
       setLocation(getX()-5, getY()); // move 5 left every act
    }
 
    public void shoot()
    {
        getWorld().addObject(new Fire(), getX(), getY()); // adds Fire object into world
        setLocation(getX()+90,getY()); // moves chicken right 90
    }
}
Now, the actor (chicken) will not move unless you tell it to. Your code should tell it when to move, not when not to move. The two lines 'move(0)' and 'move(-0)' will not change the current location of the chicken. Next, line 30 (in your code post) keeps the chicken constantly moving left. It is no wonder it does not move right. Finally, if your fire is not going down, it is because of something in, or absent from, your Fire class.
You need to login to post a reply.