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

2015/4/7

Getting a HealthBar to work

RagingAsian RagingAsian

2015/4/7

#
I almost have the health bar down, but need help to set my greep hp value to be set as the health value of the health bar. So far, I have made the creating method of the health bar in my world class and have referenced it through all my greep classes. Now, I need to set the health bar value to match the hp of the greep. (srry if I don't make any sense). Health bar Class:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class HealthBar here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class HealthBar extends Actor   
{
    int health=
    int width=80;
    int height=15;
    int healthPerPixel=(int)width/health;
     
    public HealthBar()
    {       
        update();
    }
     
    public void act()
    {
        update();
    }
     
    public void update()
    {
        setImage(new GreenfootImage(width+2,height+2));
        GreenfootImage hb=getImage();
        hb.setColor(Color.BLACK);
        hb.drawRect(0,0,width+1,height+1);
        hb.setColor(Color.GREEN);
        hb.fillRect(1,1,health*healthPerPixel,height);
    }
     
    public void loseHealth()
    {
//         health=health-Towers();
    }
}
Stage 1 World Class:
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
126
127
128
129
130
131
132
133
134
135
136
137
138
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Stage1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Stage1 extends World
{
    int[]WayX;
    int[]WayY;
    int index=0;
    private int counter=0;
    private static final int max=1000000;
    private int spawned=0;
    private static int level = 1;
    private Counter lives;
    HealthBar healthbar;
    /**
     * Constructor for objects of class Stage1.
     *
     */
    public Stage1()
    {   
        // Create a new world with 600x425 cells with a cell size of 2x2 pixels.
        super(600, 425, 2, false);
        setBackground("Stage1f.png");
         
        prepare();
    }
     
    public int getLevel()
    {
        return level;
    }
     
    public HealthBar getHealthBar()
    {  
         
        return healthbar;
    }
     
    public void act()
    {
        counter++;
        
        if((counter>=50)&&(spawned<=max))
        {           
            spawn();
            spawned++;
            waypoints();
            counter=0;
        }
    }
     
    public void spawn()
    {           
        int random= (int)((Math.random()*3) + 1);
        if(random==1)
        {       
            Small small = new Small(level,lives,healthbar);           
            addObject(small,0,108);
            addObject(new HealthBar(),small.getX(),small.getY());
        }
        if(random==2)
        {
            Medium medium = new Medium(level,lives, healthbar);
            addObject(medium,0,108);
            addObject(new HealthBar(),medium.getX(),medium.getY());
        }
        if(random==3)
        {
            Heavy heavy = new Heavy(level,lives,healthbar);
            addObject(heavy,0,108);
            addObject(new HealthBar(),heavy.getX(),heavy.getY());
        }
    }
     
    public void waypoints()
    {
         WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,620};
         WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
         index++;
    }
    
    public void prepare()
    {
        Counter lives = new Counter();
        addObject(lives, 123, 416);
        lives.setLocation(120, 415);
        this.lives = lives;
         
        Counter2 money = new Counter2();
        addObject(money, 240, 415);
        money.setLocation(237, 414);
 
        NormalButton nbutton = new NormalButton(money);
        addObject(nbutton, 15, 416);
        nbutton.setLocation(12, 414);
 
        IceButton ibutton = new IceButton(money);
        addObject(ibutton, 26, 409);
        ibutton.setLocation(49, 413);
        ibutton.setLocation(51, 413);
        ibutton.setLocation(48, 413);
 
        FireButton fbutton = new FireButton(money);
        addObject(fbutton, 31, 415);
        fbutton.setLocation(30, 414);
        fbutton.setLocation(31, 414);
        fbutton.setLocation(31, 414);
 
        SniperButton sbutton = new SniperButton(money);
        addObject(sbutton, 70, 415);
        sbutton.setLocation(66, 414);
        sbutton.setLocation(69, 414);
 
        BombButton bbutton = new BombButton(money);
        addObject(bbutton, 89, 417);
        bbutton.setLocation(85, 414);
 
        //All these objects are just text objects.
        Money moneytext = new Money();
        addObject(moneytext, 195, 409);
        moneytext.setLocation(193, 414);
 
        Lives livestext = new Lives();
        addObject(livestext, 424, 395);
        livestext.setLocation(112, 415);      
 
        lives.setLocation(145, 414);
        lives.setLocation(149, 414);
        livestext.setLocation(117, 414);
        lives.setLocation(151, 414);
        money.setLocation(237, 413);
    }
}
Greeps super class:
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Creepers here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Greeps extends Actor
{  
    public int type;
    boolean touchingBullet = false;
     
    int[]WayX;
    int[]WayY;
    int index=0;
     
    double distance=150;
     
    public int hp;
    public int speed;
    private int level;
    public Counter liveCounter;
    public Greeps(int hp,int speed, int lv,Counter lives)
    {
        level = lv;
        this.hp=hp;
        this.speed=speed;
        liveCounter=lives;
        setRotation(90);
        if(type==1) setImage("Enemy-small.png");
        if(type==2) setImage("Enemy-medium.png");
        if(type==3) setImage("Enemy-heavy.png");
        waypoints();       
    }
     
    public void act()
    {
        movement();
        gameOver();
    }                       
     
    /**
     * Sets the points where the Greeps will turn
     *
     */
    public void waypoints()
    {
 
        if(level== 1)
        {
           WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,620};
           WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
         
        }
        if (level == 2){
           WayX= new int[] {478,478,272,272,599};
           WayY= new int[]{171,380,380,19,19};
        }
        if (level == 3){
           WayX=new int[] {355,246,246,463,463,134,134,572,572,27,27};
           WayY=new int[] {248,248,115,115,314,314,49,49,381,381,10};
        }
        if (level == 4){
           WayX= new int[]{319,319,56,56,431,431,525,525,599};
           WayY= new int[]{376,21,21,288,288,21,21,375,375};
        }
    }
     
    /**
     * Sets the speed of the Greeps and determins when to move onto the next waypoint
     *
     */
    public void movement()
    {       
        move(1);
        int theDistance = (int)(Math.hypot(WayX[index] - getX(), WayY[index] - getY()));  
        if (theDistance < 1  )
        {
            if(index<WayX.length-1)
            {
                index++;
            } else
            {
                getWorld().removeObject(this);
                return;
            }
             
        }
        turnTowards(WayX[index], WayY[index]);
        
    }  
     
    public void decreaseHealth()
    {
        Actor b1 = getOneIntersectingObject(Bullets.class);
        Bullets b2 = (Bullets)b1;
        if(isTouching(Bullets.class))
        {
                   World world =getWorld();                  
                   if(b2.bulletType==1)
                   {
                       hp=hp-20;
                       world.removeObject(b1);
                       if (hp <= 0) world.removeObject(this);
                   }
                   if(b2.bulletType==2)
                   {
                       hp=hp-10;
                       world.removeObject(b1);
                       if (hp <= 0) world.removeObject(this);
                   }
                   if(b2.bulletType==3)
                   {
                       hp=hp-10;
                       world.removeObject(b1);
                       if (hp <= 0) world.removeObject(this);
                   }
                   if(b2.bulletType==4)
                   {
                       hp=hp-45;
                       world.removeObject(b1);
                       if (hp <= 0) world.removeObject(this);
                   }
                   if(b2.bulletType==5)
                   {
                       hp=hp-35;
                       world.removeObject(b1);
                       if (hp <= 0) world.removeObject(this);
                   }
        }
        return;       
    }
     
    public int loseALife()
    {      
         
        if((level==1)&&(index==13))
        {
            liveCounter.add(-1);
            getWorld().removeObject(this); 
               
        }
        if((level==2)&&(index>=5))
        {
            liveCounter.add(-1);  
            getWorld().removeObject(this);                                  
        }
        if((level==3)&&(index>=11))
        {
            liveCounter.add(-1); 
            getWorld().removeObject(this);               
        }
        if((level==4)&&(index>=9))
        {
            liveCounter.add(-1);    
            getWorld().removeObject(this);                                
        }          
        return index;
    }
     
     
     
    public void gameOver()
    {
        if (liveCounter.getLives()<=0)
        {
            Greenfoot.setWorld(new GameOver());
            getWorld().addObject(new MainMenuButton(),300,225);
        }
    }
 }
Small SubClass that extends Greeps:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Medium here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Small extends Greeps
{
    private int level;
 
    public Small(int level,Counter lives, HealthBar healthbar)
    {
        super(25, 3, level,lives);
        liveCounter=lives;
        super.waypoints();
        type=1;
    }
    /**
     * Act - do whatever the Medium wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         
       movement();
       super.decreaseHealth();
       super.loseALife();
    }
}
There are two other subclasses that extend Greeps that are exactly the same as the Small subclass
RagingAsian RagingAsian

2015/4/8

#
Never mind, i figured it out. Although, I am getting an error in the HealthBar class from int healthPrePixel. The error is java.lang.ArithmeticException: / by zero at HealthBar.<init>(HealthBar.java:15) at Stage1.spawn(Stage1.java:64) at Stage1.act(Stage1.java:50) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2015/4/8

#
What is your line 11 in the HealthBar class? The previous HealthBar code posted shows an unfinished line.
RagingAsian RagingAsian

2015/4/8

#
Sorry bout that. Here's the HealthBar class code again with the changes I made. HealthBar Class:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class HealthBar here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class HealthBar extends Actor   
{
    boolean inPosition = false;
    Greeps greep;
    Towers tower;
    int health=greep.hp;
    int width=80;
    int height=15;
    int healthPerPixel=(int)width/health;
    private Actor enemy;
     
    public HealthBar(Actor enemy)
    {       
        this.enemy=enemy;
        update();
    }
     
    public void act()
    {
        update();
    }
         
    public void update()
    {
        setImage(new GreenfootImage(width+2,height+2));
        GreenfootImage hb=getImage();
        hb.setColor(Color.BLACK);
        hb.drawRect(0,0,width+1,height+1);
        hb.setColor(Color.GREEN);
        hb.fillRect(1,1,health*healthPerPixel,height);
    }
     
    public void loseHealth()
    {
        health = greep.hp;       
//         health=health-tower.dmg;
    }
     
    public void followGreep()
    {
        if (enemy.getWorld()!=null)
        {
            int x=enemy.getX();           
            int y=enemy.getY();
            turnTowards(x,y);
            move(2);
             
        }else{ getWorld().removeObject(this);}
    }
}
At first, Line 11 said
1
int health;
and had a given a value in the loseHealth() method with this code:
1
health=greep.hp;
danpost danpost

2015/4/8

#
What you had at first
1
int health;
is better than what you now have for line 14:
1
int health = greep.hp;
'greep' will not yet be assigned any value when line 14 is executed (it would still be a null reference). I mean, you cannot get an 'hp' value from an absent greep object. Actually, I do not see any code anywhere that assigns an object to the 'greep' field declared on line 12.
RagingAsian RagingAsian

2015/4/8

#
Would this suffice?
1
Greeps greep=new Greeps(hp,speed,lv,lives)
Also, would it be best to create the health bar object in the world class, or the Small, Medium, and Heavy subclasses, all three have different hp values? I currently have them being made in the world class.
danpost danpost

2015/4/8

#
Let me ask you a few questions, here. (1) How many of each subclass of Greeps will you be creating? (2) Are each Greeps object (regardless of subclass created from) to have a healthbar and a lives counter and a money counter? I ask these questions because you currently have only ONE healthbar and ONE lives counter and ONE money counter created in the world. One of each is needed for each Greeps object if they are to have their own values for these states.
danpost danpost

2015/4/9

#
danpost wrote...
Let me ask you a few questions, here. (1) How many of each subclass of Greeps will you be creating? (2) Are each Greeps object (regardless of subclass created from) to have a healthbar and a lives counter and a money counter? I ask these questions because you currently have only ONE healthbar and ONE lives counter and ONE money counter created in the world. One of each is needed for each Greeps object if they are to have their own values for these states.
Sorry -- I was not thinking clear (or did not see the whole picture). You do have only one lives and one money counter -- but, you are creating a healthbar for each greep spawned. The problem is that you are passing all the greeps the same non-existent healthbar (null) that is held in the 'healthbar' field (nothing is ever set to the field) and then adding different healthbars into the world (supposedly for each greep). Remove the 'healthbar' field from the world class and then within the 'spawn' method, create the healthbar objects first, add them into the world and then create the greep object and add it into the world passing the healthbar just created.
RagingAsian RagingAsian

2015/4/9

#
Would this work? Spawn method of Stage1:
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
public void spawn()
    {   
        HealthBar healthbar=new HealthBar();
        int random= (int)((Math.random()*3) + 1);
        int x;
        int y;
        if(random==1)
        {       
            Small small = new Small(level,lives,healthbar);
            x=small.getX();
            y=small.getY();
            addObject(healthbar,0,106);
            healthbar.setLocation(x,y-4);
            addObject(small,0,108);
        }
        if(random==2)
        {
            Medium medium = new Medium(level,lives, healthbar);
            x=medium.getX();
            y=medium.getY();
            addObject(healthbar,0,106);
            healthbar.setLocation(x,y-4);
            addObject(medium,0,108);
        }
        if(random==3)
        {
            Heavy heavy = new Heavy(level,lives,healthbar);
            x=heavy.getX();
            y=heavy.getY();
            addObject(healthbar,0,106);
            healthbar.setLocation(x,y-4);
            addObject(heavy,0,108);
        }
    }
Also, I still getting the same error, but happens before anything gets created in the java.lang.ArithmeticException: / by zero at HealthBar.<init>(HealthBar.java:15) at Stage4.spawn(Stage4.java:71) at Stage4.act(Stage4.java:53) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
RagingAsian RagingAsian

2015/4/9

#
Ok I got my healthbar object to be created. I just need to get the health bar objects to hover above the Greeps where ever they go.
danpost danpost

2015/4/11

#
I do not know exactly what changes have been made to the Greeps class at this point; but, I felt I should mention this: The following is the parameter list that you show in your Greeps class constructor: int hp,int speed, int lv,Counter lives int hp: I can understand this one; the subclasses can supply the initial health values though it; int speed: I do not think that this value should come in from outside the class; better might be to use a static int field for the default speed value of new greeps and set the instance speed field for the greep to its value when created (it can still be changed from outside the class before creating new greeps); int lv: this value should be stored in your world class and made accessible to other classes from there. Counter lives: I am unsure if your greeps has lives or not; I would think that once their health is depleted, they are removed permanently from the world; I do not know of any games where an actor other than the main character has more than one life. To summarize, it appears that the only parameter you may need is the one for initial health points. The external healthbar codes should be totally contained within the Greeps class (this includes the creation, addition to world, changing value of, moving and removing of the healthbar); so, there is no need for a parameter for that, as well. If you want, I can show you the basic code to contain the healthbar within an actor class; or even upload a simple sample demo.
RagingAsian RagingAsian

2015/4/13

#
danpost wrote...
Counter lives: I am unsure if your greeps has lives or not; I would think that once their health is depleted, they are removed permanently from the world; I do not know of any games where an actor other than the main character has more than one life.
This is to decrease the lives counter by one every time a Greep has reached the end of the path. I also changed up my coding to what you said to do. Does this look better?
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Creepers here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Greeps extends Actor
{  
    public int type;
    boolean isPlaced;
     
    int[]WayX;
    int[]WayY;
    int index=0;
     
    double distance=150;
    public int hp;
    private int level;
    public Counter liveCounter;
    HealthBar healthbar=new HealthBar(hp);
    public Greeps(int hp, int lv,Counter lives)
    {  
        level = lv;
        this.hp=hp;      
        liveCounter=lives;
        if(type==1) setImage("Enemy-small.png");
        if(type==2) setImage("Enemy-medium.png");
        if(type==3) setImage("Enemy-heavy.png");
        waypoints();       
    }
 
     
    public void act()
    {
        createHealthBar();
        setHealthBarLocation();
        movement();
        decreaseHealth();
        loseALife();      
        gameOver();
    
     
    /**
     * Sets the points where the Greeps will turn
     *
     */
    public void waypoints()
    {
 
        if(level== 1)
        {
           WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,599,600};
           WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140,140};
         
        }
        if (level == 2){
           WayX= new int[] {478,478,272,272,599,600};
           WayY= new int[]{171,380,380,19,19,19};
        }
        if (level == 3){
           WayX=new int[] {355,246,246,463,463,134,134,572,572,27,27,27};
           WayY=new int[] {248,248,115,115,314,314,49,49,381,381,10, 0};
        }
        if (level == 4){
           WayX= new int[]{319,319,56,56,431,431,525,525,599,600};
           WayY= new int[]{376,21,21,288,288,21,21,375,375,375};
        }
    }
     
    public void atEndOfPath()
    {
        if(getX()<0)
        {
            getWorld().removeObject(this);
            getWorld().removeObject(healthbar);
            return;
        }
        if(getX()>600)
        {
            getWorld().removeObject(this);
            getWorld().removeObject(healthbar);
            return;
        }
        if(getY()>425)
        {
            getWorld().removeObject(this);
            getWorld().removeObject(healthbar);
            return;
        }
        if(getY()<0)
        {
            getWorld().removeObject(this);
            getWorld().removeObject(healthbar);
            return;
        }
    }
     
    /**
     * Sets the speed of the Greeps and determins when to move onto the next waypoint
     *
     */
    public void movement()
    {       
        move(1);
        int theDistance = (int)(Math.hypot(WayX[index] - getX(), WayY[index] - getY()));  
        if (theDistance < 1  )
        {
            if(index<WayX.length-1)
            {
                index++;
            } else
            {
                getWorld().removeObject(this);
                return;
            }
             
        }
        turnTowards(WayX[index], WayY[index]);
        
    }  
     
    public void decreaseHealth()
    {
        Actor b1 = getOneIntersectingObject(Bullets.class);
        Bullets b2 = (Bullets)b1;
        if(isTouching(Bullets.class))
        {
                   World world =getWorld();                  
                   if(b2.bulletType==1)
                   {
                       hp=hp-20;
                       world.removeObject(b1);
                       healthbar.update();
                       if (hp <= 0){ world.removeObject(this); world.removeObject(healthbar);}
                   }
                   if(b2.bulletType==2)
                   {
                       hp=hp-10;
                       world.removeObject(b1);
                       healthbar.update();
                       if (hp <= 0){ world.removeObject(this); world.removeObject(healthbar);}
                   }
                   if(b2.bulletType==3)
                   {
                       hp=hp-10;
                       world.removeObject(b1);
                       healthbar.update();
                       if (hp <= 0){ world.removeObject(this); world.removeObject(healthbar);}
                   }
                   if(b2.bulletType==4)
                   {
                       hp=hp-45;
                       world.removeObject(b1);
                       healthbar.update();
                       if (hp <= 0){ world.removeObject(this); world.removeObject(healthbar);}
                   }
                   if(b2.bulletType==5)
                   {
                       hp=hp-35;
                       world.removeObject(b1);
                       healthbar.update();
                       if (hp <= 0){ world.removeObject(this); world.removeObject(healthbar);}
                   }
        }else{
            atEndOfPath();
        }
        return;       
    }
     
    public int loseALife()
    {      
         
        if((level==1)&&(index==13))
        {
            liveCounter.add(-1);
            getWorld().removeObject(this); 
               
        }
        if((level==2)&&(index==5))
        {
            liveCounter.add(-1);  
            getWorld().removeObject(this);                                  
        }
        if((level==3)&&(index==11))
        {
            liveCounter.add(-1); 
            getWorld().removeObject(this);               
        }
        if((level==4)&&(index==9))
        {
            liveCounter.add(-1);    
            getWorld().removeObject(this);                                
        }          
        return index;
    }
     
    public void createHealthBar()
    {
        World world = getWorld();
        world.addObject(healthbar,getX(),getY()-5);
    }
     
    public void setHealthBarLocation()
    {
        healthbar.setLocation(getX(),getY()-10);
    }
          
    public void gameOver()
    {
        if (liveCounter.getLives()<=0)
        {
            Greenfoot.setWorld(new GameOver());
            getWorld().addObject(new MainMenuButton(),300,225);
            Greenfoot.start();
        }
    }
 }
But once after I changed it and it compiled, I got this error. java.lang.ArithmeticException: / by zero at HealthBar.<init>(HealthBar.java:20) at Greeps.<init>(Greeps.java:22) at Heavy.<init>(Heavy.java:14) at Stage3.spawn(Stage3.java:81) at Stage3.act(Stage3.java:49) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Danpost I posted my current scenario onto my profile. If possible, could you look over it please.
danpost danpost

2015/4/13

#
The Greeps class code, the HealthBar class code and the 'spawn' method in your Stage1 world class still need a lot of work. If you are going to create healthbars for each greep created in the 'spawn' method, then you need to create the HealthBar object first and pass the object to the Greeps class when called to create the new Greeps object:
1
2
3
4
5
6
7
8
9
10
11
// a simple 'spawn' method
private void spawn()
{
    HealthBar hb = new HealthBar(hp);
    Actor greep = null;
    int random = Greenfoot.getRandomNumber(3);
    if (random == 0) greep = new Small(hb);
    else if (random == 1) greep = new Medium(hb);
    else if (random == 2) greep = new Heavy(hb);
    addObject(greep, 0, 108);
}
Now, to add the healthbar into the world, put the following method in the Greeps class:
1
2
3
4
protected void addedToWorld(World world)
{
    world.addObject(healthbar, getX(), getY()-25);
}
This method will be called automatically by the greenfoot framework when the Greeps object is added into a world. You still need to remove the 'level' field and the 'livesCounter' field from the Greeps class. These are not things that 'belong' to Greeps objects. They should be in the class that controls the game as a whole (a World subclass that supers your Stage? classes, for example). The 'hp' field in your Greeps class would be better placed in the HealthBar class with a getter method to return its value.
RagingAsian RagingAsian

2015/4/13

#
Where do you recommend I put the Lives Counter? In the World Class(es)?
danpost danpost

2015/4/13

#
RagingAsian wrote...
Where do you recommend I put the Lives Counter? In the World Class(es)?
danpost wrote...
You still need to remove the 'level' field and the 'livesCounter' field from the Greeps class. These are not things that 'belong' to Greeps objects. They should be in the class that controls the game as a whole (a World subclass that supers your Stage? classes, for example).
Please, do not make me repeat myself.
You need to login to post a reply.