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

2014/1/16

How to make a scorecounter and counting score?

Stormrage Stormrage

2014/1/16

#
"void"is not allowed error for"Time:"
private static final Color TEXT_COLOR = new Color(200, 0, 0);
    private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);
    public ScoreCounter(){
        updateImage();  
    }

    public void act() {
        updateImage();
        //this keeps updating the score counter;
    }

    private void updateImage()
    {
        String text = "Time: " + ((Background) getWorld()).countScore();

        GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR,TRANSPARENT_COLOR);
        setImage(image);
        
    }
public void countPop()
    {
        s.add(20);
    }

    public void countScore()
    {
        s.getValue();
    }
add also null for add score
int value = 0;




    public Score()
    {
    }

    public void add(int score)
    {
        value=value+score;
    }

    public int getScore()
    {
        return value;
    }
}
danpost danpost

2014/1/16

#
I believe it is your 'countScore' call at the end of line 14 that is causing the first problem. You have declared that method to not return any value (or void of a value). And 'void' cannot be converted into a String (you cannot get something from nothing) even if it could be returned. Change the return type and return the value in the 'countScore' method (as the method is right now, it is useless; that is, it gets a value from somewhere, but does nothing with it). 'null for add score' does not say much and little info is given about this. If you are getting any errors that you need help with, copy/paste the entire message here and supply some background as well as any pertinent code. The more info you give, the less time later will be used to fish the info from you.
Stormrage Stormrage

2014/1/16

#
Thank you for your reply I have change it to
private int updateImage()
    {
        String text = "Time: " + ((Background) getWorld()).countScore();

        GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR,TRANSPARENT_COLOR);
        setImage(image);
        return 2;
    }
but it is still the same problem: 'void' type not allowed here.It cannot be used as a variable type
danpost danpost

2014/1/16

#
danpost wrote...
Change the return type and return the value in the 'countScore' method
It is not the 'updateImage' method that needs to return a value; it is the 'countScore' method.
Stormrage Stormrage

2014/1/16

#
Thanks :) It been fixed For the second problem. When a enemy been dead it will return java.lang.NullPointerException at Enemy.act(Enemy.java:34)
isDestroyed = isTouching( ObjectBullet.class);
        
        if(isDestroyed){
            List object=getObjectsInRange( 30,ObjectBullet.class);
            getWorld().removeObjects(object);
            getWorld().removeObject(this);
            Background b=(Background) getWorld(); 
            ((Background) getWorld()).countPop();
        }   
The Enemy.java:34 is ((Background) getWorld()).countPop();
danpost danpost

2014/1/16

#
First off, if you are going to put the world returned by 'getWorld' in a field ( 'b' -- line 7 above), then you might as well do that before the two 'getWorld's on the previous lines; and USE it. Nowhere after assigning 'b' the world are you using it for anything (you are, even after assigning the world to the field, still using 'getWorld' to get the world when you already have it in 'b'). The 'nullPointerException' is due to the fact that 'getWorld' will return 'null' for any line after line 6 (removing the actor from the world). This could have been avoided two ways..(1) by utilization of the 'b' field throughout the 'if' block; or (2) moving the line that removes the actor ( 'this' ) to the end of the 'if' block. I suggest this:
// starting at your line 3 above
if (isDestroyed)
{
    Background b = (Background)getWorld();
    b.removeObjects(getObjectsInRange(30, ObjectBullet.class));
    b.removeObject(this);
    b.countPop();
}
Stormrage Stormrage

2014/1/16

#
Thank:) fixed
You need to login to post a reply.