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

2014/4/13

Problem subtracting health from health bar

Neymar.Jr Neymar.Jr

2014/4/13

#
This is my code of my health bar and I;m trying to make it decrease when every time an enemy bullet collides with my spaceship. Im not sure should I include my bullet code or player code, so if its necessary tell me and I'll upload it.
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * The StatBar class let's you display any stat with a little amount of code required.
 *
 * @author Zamoht
 * @version January 2014
 */
public class StatBar extends Actor
{
    public static enum Style {RECT, ROUND, SMOOTH};
    private int max;
    private double value;
    private int target;
    private double temp;
    private double speed;
    private GreenfootImage frame;
    private GreenfootImage fullBar;
    private Style barStyle;
    private int fontSize;
    private int letterWidth;
    private int stringWidth;
    private Color color;
    /**
     * Updates the stat bar every act. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (target > value)
        {
            value+=speed*((double)max/1000.0);
            if (target < value)
            {
                value = target;
            }
        }
        else if (target < value)
        {
            value-=speed*((double)max/1000.0);
            if (target > value)
            {
                value = target;
            }
        }
 
        if (value != temp)
        {
            updateImage();
        }
    }   
 
    /**
     * Creates a stat bar using the given color, width, height, style, maximum value and current value.
     *
     * @param color the color of the stat bar. Transparent colors should be avoided.
     * @param width the width of the stat bar.
     * @param height the height of the stat bar.
     * @param style the style of the edges.
     * @param max the maximum value of the shown stat.
     * @param current the current value of the shown stat.
     */
    public StatBar(Color color, int width, int height, Style style, int max, int current)
    {
        barStyle = style;
        int edge = getEdge(width, height);
        if (current > max) current = max;
        frame = new GreenfootImage(width, height);
        frame.setColor(Color.gray);
        frame.fillOval(0, 0, edge, height);
        frame.fillRect(edge/2, 1, width-edge, height);
        frame.fillOval(width-edge, 0, edge, height);
 
        fullBar = new GreenfootImage(width, height);
        setColor(color, edge, false);
 
        setMax(max, true);
        this.value = current;
        target = (int)value;
        speed = 5;
 
        updateImage();
    }
 
    /**
     * Changes the color of the stat bar. Should not be used in the updateColor() method due to
     * bad performance.
     *
     * @param color a new color
     */
    public void setColor(Color color)
    {
        setColor(color, true);
    }
 
    private void setColor(Color color, int edge, boolean update)
    {
        if (fullBar == null || color.equals(this.color))
        {
            return;
        }
        this.color = color;
        int width = fullBar.getWidth();
        int height = fullBar.getHeight();
        fullBar.clear();
        fullBar.setColor(color);
        fullBar.fillOval(0, 0, edge, height);
        fullBar.fillRect(edge/2, 1, width-edge, height);
        fullBar.fillOval(width-edge, 0, edge, height);
        if (update)updateImage();
    }
     
    /**
     * Changes the color of the stat bar. Use this in the updateColor() method with the update parameter
     * set to false.
     *
     * @param color a new color.
     * @param update a boolean which should only be true if an instant update is needed.
     */
    public void setColor(Color color, boolean update)
    {
        int width;
        int height;
        if (fullBar != null)
        {
            width = fullBar.getWidth();
            height = fullBar.getHeight();
        }
        else
        {
            return;
        }
        setColor(color, getEdge(width, height), update);
    }
     
    /**
     * updateColor is called everytime the stat bar is updated. It should be implemented in
     * a subclass for changing the color of the stat bar.
     *
     */
    public void updateColor()
    {
        //Used by subclasses to update the color of the bar
    }
 
    /**
     * Adds a given amount to the value target of the stat bar.
     *
     * @param change the amount which the target should be changed. This value can be negative when
     * subtraction is needed instead of addition.
     */
    public void add(int change)
    {
        target += change;
        if (target > max) target = max;
        if (target < 0) target = 0;
        updateImage();
    }
 
    /**
     * Sets the target which the stat bar's current value will change to over time.
     *
     * @param target the value target
     */
    public void setTarget(int target)
    {
        this.target = target;
    }
 
    /**
     * Instantly changes the value and updates the bar with this new value.
     *
     * @param value a new value.
     */
    public void setValue(int value)
    {
        this.value = value;
        updateImage();
    }
 
    /**
     * Sets the maximum value the shown stat can reach. This does not fix the font size.
     *
     * @param max the maximum value.
     */
    public void setMax(int max)
    {
        setMax(max, false);
    }
 
    /**
     * Sets the maximum value the shown stat can reach.
     *
     * @param max the maximum value.
     * @param fixFont a boolean which determines wether the font size should be adjusted. If set
     * to true and the new maximum value is too large for the stat bar, the font size will be asjusted.
     */
    public void setMax(int max, boolean fixFont)
    {
        this.max = max;
 
        if (fixFont)
        {
            int width = frame.getWidth();
            int height = frame.getHeight();
            int digits = getDigits(max);
            fontSize = 1;
            GreenfootImage label = new GreenfootImage("0", fontSize, Color.black, null);
            while (label.getHeight() < height-height/5 && label.getWidth()*(digits*2+1) < width-width/5)
            {
                letterWidth = label.getWidth();
                fontSize++;
                label = new GreenfootImage("0", fontSize, Color.black, null);
            }
            fontSize--;
             
            label = new GreenfootImage(""+max+"/"+max, fontSize, Color.black, null);
            stringWidth = label.getWidth()/2;
        }
 
        updateImage();
    }
 
    /**
     * Sets the update speed of the statbar. This should not be changed due to a higher maximum value
     * since the update works with a percentage of the maximum value.
     *
     * @param speed the update speed. Setting this to 1 will change the current value with getMax()/1000
     * every act till the target is reached. Setting this to 2 will change the curren value with getMax()/500 and so on. Remember that the
     * speed is a double.
     */
    public void setSpeed(double speed)
    {
        this.speed = speed;
    }
 
    private void updateImage()
    {
        updateColor();
        GreenfootImage image = new GreenfootImage(frame);
        double ratio = (double)value/(double)max;
        int width = (int)Math.round(ratio*frame.getWidth());
        if (width > 0)
        {
            GreenfootImage part = new GreenfootImage(width, frame.getHeight());
            part.drawImage(fullBar, 0, 0);
            image.drawImage(part, 0, 0);
        }
        GreenfootImage label = new GreenfootImage(""+(int)value+"/"+max, fontSize, Color.black, null);
        int modifier = 0;
        if (label.getHeight()%2 == 0 || (label.getHeight()%2 == 1 && image.getHeight()%2 == 1)) modifier = 1;
        image.drawImage(label,
            image.getWidth()/2 - stringWidth + letterWidth * (getDigits(max) - getDigits((int)value)),
            image.getHeight()/2 - label.getHeight()/2 + modifier);
            //image.getWidth()/2 - letterWidth*(getDigits(max)*2+1)/2 + letterWidth*(getDigits(max)-getDigits((int)value))
        setImage(image);
        temp = value;
    }
 
    private int getDigits(int value)
    {
        int digits = 0;
        do
        {
            digits++;
            value/=10;
        }
        while (value > 0);
        return digits;
    }
 
    private int getEdge(int width, int height)
    {  
        int edge;
        switch (barStyle)
        {
            case RECT:
            edge = 0;
            break;
 
            case ROUND:
            edge = height;
            break;
 
            case SMOOTH:
            edge = height/2;
            break;
 
            default:
            edge = 0;
            break;
        }
        return edge;
    }
     
    /**
     * Returns the maximum value.
     *
     */
    public int getMax()
    {
        return max;
    }
     
    /**
     * Returns the current value.
     *
     */
    public int getValue()
    {
        return (int)value;
    }
}
danpost danpost

2014/4/13

#
Will need to see either the enemy bullet class or the spaceship class -- whichever one has the collision detection code within it (for collision between these two actors).
Neymar.Jr Neymar.Jr

2014/4/13

#
Okay thanks here's my class with the code for the collision
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Spaceship here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player1 extends Mover
{
    private int counter;
     
       private int timeTillCanFire;
       private StatBar statBar;
    private int changer;
       public void fire()
    {
        
         
        {
            if(timeTillCanFire == 0)  { //if can fire now...
                if(Greenfoot.isKeyDown("enter")) {
                    //do whatever you normally do to acually fire.
                    timeTillCanFire = 10; //wait ten frames before you can fire again.
                }
            }
            if(timeTillCanFire != 0) { //if can't fire now...
                timeTillCanFire--;      //wait till can
            }
        }
    }
     
 
    public void act()
    {
         
        
        int speed = 6;
        if(Greenfoot.isKeyDown("up"))
        { setLocation(getX(), getY() - speed);
        }
        if(Greenfoot.isKeyDown("down"))
        { setLocation(getX(), getY() + speed);
        }
        if(Greenfoot.isKeyDown("left"))
        { setLocation(getX() - speed, getY());
        }
        if(Greenfoot.isKeyDown("right"))
        { setLocation(getX() + speed, getY());
        }
         
           if (Greenfoot.isKeyDown("enter"))
        {
            getWorld().addObject(new Bullet(getRotation()), getX(), getY());
        }
         
 
        fire();
        boundary();
         
         
         
        BulletCollision();
 
    }
    public void BulletCollision()
    {
        if (getWorld() != null)
        {
            Enemy_Bullet collision = (Enemy_Bullet) getOneIntersectingObject(Enemy_Bullet.class);
            if (collision != null)
            {
                SpaceWorld space = (SpaceWorld)getWorld(); //gets the space world
 
                  
                space.removeObject(collision);
            }
        }
    }
     
      
     
     
          
         
    public Player1()
    {
        counter = 0;
        setVelocity(4);
         
    }
 
    public void boundary()
    {
        //keeps the player on the screen
        if (getX() < 23)
        {
            setLocation(getX() + getVelocity(), getY());
        }
        if (getX() > 626)
        {
            setLocation(getX() - getVelocity(), getY());
        }
        if (getY() < 19)
        {
            setLocation(getX(), getY() + getVelocity());
        }
        if (getY() > 681)
        {
            setLocation(getX(), getY() - getVelocity());
        }
    }
 
    
     
     
     
        
 }
 
         
   
  
            
danpost danpost

2014/4/13

#
At line 75 in the Player1 class, you should be able to use:
1
statBar.add(-1);
to adjust the health. Except, I do not see where you are setting the field to any StatBar object.
Neymar.Jr Neymar.Jr

2014/4/14

#
Yeah I tried it but i keep getting this error every time the enemy bullet touches the spaceship. java.lang.ClassCastException: Player1 cannot be cast to Enemy_Bullet at Enemy_Bullet.BulletCollision(Enemy_Bullet.java:23) at Bullet2.act(Bullet2.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.ClassCastException: Enemy cannot be cast to Enemy_Bullet at Player1.BulletCollision(Player1.java:71) at Player1.act(Player1.java:63) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Player1.BulletCollision(Player1.java:56) at Player1.act(Player1.java:45) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Player1.BulletCollision(Player1.java:56) at Player1.act(Player1.java:45) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
Neymar.Jr Neymar.Jr

2014/4/14

#
And i created a subclass called health bar under statbar. Heres my code for the health bar.
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
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 StatBar
{
     
     
     
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //You can just delete the whole act method if you don't need to add anything
        super.act();
    }   
     
    public HealthBar(int max, int current)
    {
        //Creates a new stat bar with a green color, a width of 140 pixels, a height of 24 pixels,
        //the ROUND bar style, a max value and a current value.
        super(new Color(0, 255, 0), 140, 24, Style.ROUND, max, current);
        //Sets the update speed to 5.
        setSpeed(5);
    }
     
    public HealthBar(int max)
    {
        this(max, max);
    }
     
     
     
     
     
     
     
    public void updateColor()
    {
        if (getValue() <= getMax()/4)
        {
            setColor(Color.red);
        }
        else if (getValue() < getMax()/2)
        {
            setColor(Color.yellow);
        }
        else
        {
            setColor(Color.green);
        }
    }
}
danpost danpost

2014/4/14

#
This error does not have anything to do with the StatBar or HealthBar object. It looks like you have been changing your code a little (particularly line 71 of the Player1 class). However, the latest error you are showing is in the Enemy_Bullet class at line 23 (in the BulletCollision method). That is the code you should be showing at this point.
Neymar.Jr Neymar.Jr

2014/4/14

#
okay here's my enemy_bullet code
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Enemy_Lazer here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public abstract class Enemy_Bullet extends Mover
{
    private int death;
    private int time;
     
    public Enemy_Bullet()
    {
        time = 0;
    }
     
     
     
     
    public void act()
    {
        movement();
        offScreen();
        timeOut();
        extraAction();
    }
    /*
     * removes the actor if it has left the bottom of the screen
     */
    public void offScreen()
    {
         if (getWorld() != null && getY() > getWorld().getHeight() + 10)
        {
            getWorld().removeObject(this);
        }
    }
    public void timeOut()
    {
        if (getWorld() != null)
        {
            time++;
            if (time == 1000)
            {
                getWorld().removeObject(this);
            }
        }
    }
    /*
     * Determines the movement of the bullet
     */
    public void movement()
    {
        moveLeft();
    }
    /*
     * Overwrite this if a bullet needs to do something special
     */
    public void extraAction()
    {
        //overwrite if you want to perform action
    }
     
     
}
danpost danpost

2014/4/14

#
You must have removed the BulletCollision method from this class. Are you still getting errors? If so, produce one, copy/paste the message here and post the troublesome code. If you do not know where the trouble is located, wait for someone here to tell you what code needs posting before changing anything. Otherwise, it will be almost impossible to help.
Neymar.Jr Neymar.Jr

2014/4/14

#
What do u mean removed. I also have a subclass under that called bullet2 and heres my code for that.
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)
 
/**
 * Write a description of class Bullet here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
 
public class Bullet2 extends Enemy_Bullet
{
 
    private int direction, speed;
    private StatBar statBar;
    private int changer;
     
    public static final int damage = 5;
    public Bullet2(int dir)
    {
        direction = dir;
        speed = 5;
 
    }
 
    public void act()
    {
        setRotation(180);
        move(3);
        
 
    }
     
    
 
    
    public static void delay(int time)
    {
        time = 3;
    }
danpost danpost

2014/4/14

#
All I was saying is that I cannot help you with the error you last posted because your code has changed so much that the information given within the message is no longer valid.
Neymar.Jr Neymar.Jr

2014/4/14

#
oh ok
You need to login to post a reply.