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

2017/10/19

My Aliens wont move at all (space invaders)

Aristoclea Aristoclea

2017/10/19

#
I got everything else for the game but for some reason the Aliens won't move at all. Is my code completely wrong?
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
public class Alien extends Actor
{
  
    protected int delayCounter;   //Counts cycles.
    protected int counter;             //Another counter variable.
    public int modulus;                // make aliens move.
    public boolean first;              //first move cycle then true.
    public boolean Left;               // Alien movement to the left = true.
     
    /**
     * Allows the Alien to be destroyed by shooter bullet.
     */
    public void act()
    {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        if(bullet != null)
        {
            getWorld().removeObject(this);
        }
        delayCounter++;
         
    }   
     
    /**
     * Allows the Alien to shoot
     */
    public void shoot ()
    {
        getWorld().addObject (new AlienBullet(), getX(), getY());
    }
        
                 
    /**
     * Where each veriable is set.
     */
    public Alien()
    {
        delayCounter = 0;
        Left = true;
        modulus = 20;
        counter = 0;
        first = true;
      
    }
     
     
     
     
    /**
    * This method tells the Alien how to move
    */
    public void move()
    {
       counter++;  
       if (counter < (20 * modulus) && first) //Go left if counter is less than 400
                                        
       {
           moveLeft();
       }
       else if (counter < (35 * modulus) && !Left && !first ) //Go right if counter
                                                                //is less than 750 and
                                                               
                                                              
       {
            moveRight();
       }
       else if (counter < (35 * modulus) && Left && !first ) //Go left if counter is
                                                               //less than 700
                                                              
                                                              
                                                                
       {
            moveLeft();
       }
         
       if ( counter == (35 * modulus) ) //Tells the aliens to move down
                                         
       {
            moveDown();
            counter = 0;
            Left = !Left;
       }
       else if (counter == (20 * modulus) && first) //Tells the aliens to move down
                                                    //once it hits the left edge of te world
                                                  
       {
           moveDown();
           first = false;
           counter = 0;
           Left = !Left;
       }
    }
     
    /**
     * This Tells the Aliens to move left
     */
    public void moveLeft()
    {
        if ( (delayCounter % modulus) == 0)
        {
            setLocation(getX()-10, getY());
        }
    }
     
    /**
     * this tells the aliens how to go right
     */
    public void moveRight()
    {
       if ( (delayCounter % modulus) == 0)
        {
            setLocation(getX()+10, getY());
        }
    }
     
    /**
     * This shows the aliens how to go down
     */
   public void moveDown()
   {
         setLocation(getX(), getY() + 10);
   }
     
   
}
danpost danpost

2017/10/19

#
Just because you have a 'move' method does not mean your actor will move. The method must be called from somewhere to be executed.
You need to login to post a reply.