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

2014/9/14

I need help with my scoreboard.

plushyducky plushyducky

2014/9/14

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
import java.awt.Color;
public class Score extends Actor
{
    static int best = 0;
    int score = 0;
 
    public void addedToWorld(World w)
    {   act();
    }
        public void act()
    {   setImage(new GreenfootImage("Score: " + score + "\nBest: " + best, 20, Color.WHITE, Color.BLACK));
        setLocation(Space.width - (getImage().getWidth()/2), getImage().getHeight()/2);
        if(Space.started)
        {
            score++;
            if(best < score) best = score;
        }
    }
}
That is my code. I have a Score class, in which that code has been input. I keep getting a "width is not public in greenfoot.World; cannot be accessed from outside package" error. What must I do in order to fix this?
danpost danpost

2014/9/14

#
'width' is private in greenfoot.World; but, there is a method to get the width value from a World object -- 'getWidth'. It cannot be called on the class, it must be called on a World instance. The Actor class method 'getWorld' returns the world instance the actor is in. So, instead of 'Space.width', use 'getWorld().getWidth()'.
Super_Hippo Super_Hippo

2014/9/14

#
By the way, it is more efficient to have something like a 'update' method. Right now, you create a new image every act. It probably would be enough if you only create a new one when the old one is not up-to-date anymore which only happens when the score changed.
danpost danpost

2014/9/14

#
It appears the 'score' value will soar the moment the game is started (if 'Space.started' refers to what it leads me to believe it does).
plushyducky plushyducky

2014/9/23

#
danpost wrote...
It appears the 'score' value will soar the moment the game is started (if 'Space.started' refers to what it leads me to believe it does).
I apologize for the very late reply, as I have been busy with other things aside from this particular project. Yes, that indeed is what is intended to happen. As soon as the game starts, the score counter must act like a timer of some sorts. I did replace "Space.width" with "getWorld().getWidth()". This time, however, I am having issues with "Space.started". I always get "Can not find variable started," or something along those lines. What must I replace it with? XD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*; 
import java.awt.Color; 
public class Score extends Actor 
    static int best = 0
    int score = 0
   
    public void addedToWorld(World w) 
    {   act(); 
    
        public void act() 
    {   setImage(new GreenfootImage("Score: " + score + "\nBest: " + best, 20, Color.WHITE, Color.BLACK)); 
        setLocation(getWorld().getWidth() - (getImage().getWidth()/2), getImage().getHeight()/2); 
        if(Space.started) 
        
            score++; 
            if(best < score) best = score; 
        
    
}
Super_Hippo Super_Hippo

2014/9/23

#
To use 'Space.started' as you did in line 14, you must have a line like 'public static boolean started;' in your 'Space' class. If the compiler can't find the variable 'started', then you probably don't have such a line.
plushyducky plushyducky

2014/9/29

#
I don't have that line in my Space class. If I were to place it in, where should I insert it without accumulating any further errors?
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
public class Space extends World
{
    /**
     * Constructor for objects of class Space.
     *
     */
    public Space()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        prepare();
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Doge doge = new Doge();
        addObject(doge, 13, 190);
        Hero hero = new Hero();
        addObject(hero, 369, 198);
        Catloaf catloaf = new Catloaf();
        addObject(catloaf, 499, 94);
        Catloaf catloaf2 = new Catloaf();
        addObject(catloaf2, 519, 344);
        catloaf.setLocation(525, 92);
        hero.setLocation(235, 188);
        hero.setLocation(237, 182);
        hero.setLocation(295, 180);
        Catloaf catloaf3 = new Catloaf();
        addObject(catloaf3, 57, 374);
        Catloaf catloaf4 = new Catloaf();
        addObject(catloaf4, 222, 59);
        Catloaf catloaf5 = new Catloaf();
        addObject(catloaf5, 358, 330);
        Catloaf catloaf6 = new Catloaf();
        addObject(catloaf6, 400, 212);
        Catloaf catloaf7 = new Catloaf();
        addObject(catloaf7, 363, 72);
    }
}
Super_Hippo Super_Hippo

2014/9/29

#
You don't have a 'started' boolean, why do you want to check if it is true then? Probably you can just remove lines 14, 15 and 18 in the Score code above. By the way, it is probably useless to place actors somewhere and set their location afterwards (like lines 27 through 30 in the prepare method).
plushyducky plushyducky

2014/9/29

#
I have deleted lines 14, 15, and 18. The only problem I have right now is that the score board would not appear in the world. ( :( )
Super_Hippo Super_Hippo

2014/9/29

#
Where are you trying to add it?
plushyducky plushyducky

2014/9/29

#
Top right corner.
danpost danpost

2014/9/29

#
Super_Hippo wrote...
By the way, it is probably useless to place actors somewhere and set their location afterwards (like lines 27 through 30 in the prepare method).
This is due to using the 'Save the World' feature greenfoot provides. It is possible to add the scoreboard like you added the other actors and re-save the world.
Super_Hippo Super_Hippo

2014/9/29

#
No :D I mean where is the code with which you want to add the scoreboard to the world.
You need to login to post a reply.