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

2018/1/9

Counters not working

Kordus Kordus

2018/1/9

#
Hi. So im trying to have two counters in my game im making for college and I have the actors all set up with code. One is called counter for the score counter which should increase everytime the player hits a enemy ship. The other is a health bar which should go down everytime the player ship is shot but its not working. I either get told the variable doesnt exist or I cannot use "add" from a static context. Help would be appreciated
Kordus Kordus

2018/1/9

#
Counter Code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * A simple counter with graphical representation as an actor on screen. * * @author mik * @version 1.0 */ public class Counter extends Actor { private static final Color transparent = new Color(0,0,0,0); private GreenfootImage background; private int value; private int target; /** * Create a new counter, initialised to 0. */ public Counter() { background = getImage(); // get image from class value = 0; target = 0; updateImage(); } /** * Animate the display to count up (or down) to the current target value. */ public void act() { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); } } /** * Add a new score to the current counter value. */ public void add(int score) { target += score; } /** * Return the current counter value. */ public int getValue() { return value; } /** * Set a new counter value. */ public void setValue(int newValue) { target = newValue; value = newValue; updateImage(); } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage("" + value, 22, Color.YELLOW, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); } }
Kordus Kordus

2018/1/9

#
Is the same for the health bar
Kordus Kordus

2018/1/9

#
When I add the counter line at the bottom of this next bit of code i get an error that i cannot do it from a static context: public void killfighterclass() { if (canSee(fighterclass.class) ) { eat(fighterclass.class); getWorld().removeObject(this); Counter.add(5);
Kordus Kordus

2018/1/12

#
Could really use some help
danpost danpost

2018/1/12

#
'Counter' is a class name -- it does not refer to a particular Counter object. You need a reference to the particular Counter object whose value needs adjusting there. Insufficient code given to help in this respect.
You need to login to post a reply.