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

2012/5/25

Updating Score

BadatCoding BadatCoding

2012/5/25

#
Greetings, I have an enemy class which when an enemy is removed from the world adds credits for the player to spend. I have a subclass which is meant to output the Number of Credits to screen. At the moment it does display "Credits : 10" (10 is the starting number of credits) but once credits are added in the Enemy class, the output does not update. Any ideas of what I'm doing wrong? Thanks, BaC
danpost danpost

2012/5/25

#
Not without seeing what code you are using! Will probably need to see the class that creates the object that displays the credits and any other code that is related to that class (the method where it is created, the method where it is called, etc.). Include the class name and type of each part (example: In the actor 'Enemy' class, I have this method which contains a call to the 'Credit' object's 'addCredits(int)' method: ...). The amount of information you give relates to the amount of help you recieve! It may be something simple and fount in the class of the credit displaying object itself, but I recent had an encounter with code that had an object that was not working (it appeared to be working, yet a value within it was not changing) because the object was created twice (the value was changing in the other one). So, the problem was in the world class constructor, not in an actor class.
BadatCoding BadatCoding

2012/5/25

#
There's a decent amount of code in the Enemy class. I will include more if no problem is found with what is directly relevant, here is the directly relevant code in the Enemy class: I declare the variable
 public int Credits = 150;
I add the credits here
if(E1Health < 0){
             Credits = Credits + 10;
            getWorld().removeObject(this);
Then I have the CreditCounter class which works but doesn't update the number of Credits:
public class CreditCounter extends Enemy
{
    /**
     * Act - do whatever the CreditCounter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public CreditCounter(){
        setImage(new GreenfootImage(200, 30));
        displayCounter();
    }
    public void act() 
    {
        if(Credits > 0){
            updateCounter();
        }
    }    
    public void displayCounter(){
        GreenfootImage counter = getImage();
        counter.clear();
        counter.setColor((Color.WHITE));
        Font font = counter.getFont();
        counter.setFont(font.deriveFont(24.0F));
        counter.drawString("Counter: " + Credits, 1, 18);
    }
    public void updateCounter(){
        GreenfootImage counter = getImage();
        counter.clear();
        counter.drawString("Counter: " + Credits, 1, 18);
    }
}
Hope that makes more sense of it.
davmac davmac

2012/5/25

#
Your "Credits" variable (which should really be called "credits" btw) is an instance variable, so each enemy has their own copy of the variable, and the CreditCounter also has its own copy (which never gets updated). A simple fix in this case would be to declare the variable "static", meaning that it's shared among all instances:
    public static int Credits = 150;  
If you do this, there's no need for CreditCounter to extend Enemy, and it shouldn't - after all, a CreditCounter is not an Enemy! You just access the counter as 'Enemy.CreditCounter' (rather than just 'CreditCounter'). Be aware that using 'static' isn't generally great from a design standpoint; but, using it is the minimal change that will make your code work in this case.
BadatCoding BadatCoding

2012/5/25

#
Thank you davmac!
You need to login to post a reply.