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

2016/4/12

using labels and counter

khoalabear khoalabear

2016/4/12

#
I'm trying to put in a counter using this video I get this error:
1
2
3
4
5
6
7
public void prepare()
    {
        User_fish user_fish = new User_fish();
        addObject(user_fish,770,405);
        Label label = new Label("Score: 0");
        addObject (label, 830, 40);
    }
constructor User_fish in class User_fish cannot be applied to given types; required:Label found: no arguments reason: actual and formal argument lists differ in length the rest of my code involving this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Label extends Actor
{
    public Label(String text)
    {
        GreenfootImage img = new GreenfootImage (text.length()*20,50);
        img.drawString(text, 2, 20);
        setImage(img);
    }
     
    public void setText(String text)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString (text, 2, 20);
    }
}
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
public class User_fish extends Actor
{
    private int movementspeed = 6;
    private GreenfootImage[] images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
    private int size = 50;
    private Label myLabel;
    private int count = 1;
 
   public User_fish(Label label)
    {
        updateImages();
        setImage(images[1]);
        myLabel = label;
    }
     
   public void act()
    {
        checkKeys();
        eat();
    }   
public void eat()
    {
        AI f = (AI) getOneIntersectingObject(AI.class);
        if (f != null)
        {
            if (size > f.size)
            {
                getWorld().removeObject(f);
                size++;
                updateImages();
                count++;
                myLabel.setText("Score: " + count);
            }
            else if (size < f.size) //idk how you want to handle same size
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Gameover_world());
            }
        }
    }
Super_Hippo Super_Hippo

2016/4/12

#
The only constructor in your User_fish class needs a Label as a parameter (line 9). If you want to pass the label you create in your prepare method to pass it to the User_fish object, use this:
1
2
3
4
5
6
7
public void prepare()
{
    Label label = new Label("Score: 0");
    addObject(label, 830, 40);
    User_fish user_fish = new User_fish(label);
    addObject(user_fish, 770, 405);
}
danpost danpost

2016/4/12

#
Apparently, each User_fish object created (which may just be one per run of the scenario) is supposed to have its own label -- and that label needs to be passed to the User_fish object when it I created. However, you are trying to create a User_fish object without giving it its label. *User_fish class line 9: shows a Label object must be supplied when creating an object of the class. *'prepare' method line 3: tries to create a User_fish object without a label. In the 'prepare' method, create the label first, then create the user-fish passing the label to it.
khoalabear khoalabear

2016/4/12

#
Ahh I see. Thanks for the quick reply. Just a graphical problem. When it counts up from 0 to 1, the label seems to get blurred. Any reason why?
danpost danpost

2016/4/12

#
khoalabear wrote...
Just a graphical problem. When it counts up from 0 to 1, the label seems to get blurred. Any reason why?
I cannot say why the image is blurred in such a way. I, for one thing, prefer not to use the 'drawString' method as the image is already set in size and if the new string drawn on it is larger, it is possible to lose the last part of the drawn string off the edge of the image. I prefer to create a text image (using the GreenfootImage constructor that requires the text to be drawn) and then use the 'drawImage' method if needed (the size of the two images can be compared to ensure the text image will fit properly). For a label or counter, just setting the created text image to the actor will usually work by itself. You may want to check out my Value Display Tutorial scenario for my thoughts on displaying values (like counters, labels, etc.).
You need to login to post a reply.