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

2017/2/13

Score Counter

1
2
Nooooob Nooooob

2017/2/13

#
Tried to make a score counter but it doesn't work. World class:
Counter1 counter1 = new Counter1();
public Counter1 getCounter1()
{
   return counter1;
}
Actor:
 if(isTouching(a.class))
       {
          counter1.addScore(1);
       }
Counter:
int score = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.BLUE, Color.RED));
    }    
    public void addScore()
    {
        score++;
    }
Can anyone help?
Super_Hippo Super_Hippo

2017/2/13

#
1. Are you adding counter1 (the object) to the world? 2. In your actor class, you have to call the method you made in your world on the world in which the actor lives to return the counter1. 3. (optional) Do not set a new image to the counter every act cycle. Just set a new one when it is needed, so when the score changed.
valdes valdes

2017/2/13

#
You can import class Counter (<ctrl> + <i> or Edit menu + Import class ... option). Here is an example of how it is used: ShootingElephant you can check the code in the world class and the bullet class.
Nooooob Nooooob

2017/2/13

#
Thx 4 replying you 2 :) That's where I'm stuck at: @Super_Hippo 1. Yes.
addObject(counter1, 40, 10);
2. Sorry, I don't get it. The world class I posted, is the world the actor lives in. 3. How can you do that only when the score changes? @valdes I'll check it out.
Super_Hippo Super_Hippo

2017/2/13

#
1. Well, how is counterx counter1? 2. Yes, I know that. You have a getter method there. You have to call that getter method to return the counter1. Right now, you are trying to use counter1 in your actor as it would be a field in your actor class. 3. Put line 3 into an 'update' method and call the update method in line 4 and after line 8. Since it is only only line, you could instead copy line 4 to after line 8.
Nooooob Nooooob

2017/2/13

#
1. Typo. :=) 2.
Super_Hippo wrote...
Right now, you are trying to use counter1 in your actor as it would be a field in your actor class. You have to call that getter method to return the counter1
All right, I think I understand now what you mean but I don't know how to put that into a code. 3.
Super_Hippo wrote...
Since it is only only line, you could instead copy line 4 to after line 8.
Like that?
private void act()
{
score++;
setImage(new GreenfootImage("Score: " + score, 24, Color.BLUE, Color.RED));
}
Super_Hippo Super_Hippo

2017/2/13

#
2. - get the world in which the actor is - cast the world object to an object of type <your world name> - call the getCounter1 method on that object 3. No, with that, you add a point every act cycle and change the image every act cycle. So you have to do like the exact opposite.
Nooooob Nooooob

2017/2/13

#
2.
getWorld.MyWorld.getCounter1;
//I know it's wrong. I've read it over and over again but I can't seem to get it right.
3. Did you mean that with opposite?
int score = 0;
    public void addScore()
    {
        score++;
setImage(new GreenfootImage("Score: " + score, 24, Color.BLUE, Color.RED));
    }
or is this right?
int score = 0;
    public void addScore()
    {
        score++;
        updateImage();
    } 
    public void updateImage()
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
    }
Super_Hippo Super_Hippo

2017/2/13

#
3. (yes, 3 first) Well yes, this is correct. I actually failed a little bit there. You can remove the act method completely as you did. You can also add a constructor, so the image is also shown before "Run" is clicked:
public Counter1()
{
    update(); //if you do it without the update method, use the setImage line here instead
}
2. When calling the updateImage method, you used
updateImage();
This is the correct way of calling a method. So you also need the parenthesis there.
World w = getWorld(); //get a reference to the current world
MyWorld m = (MyWorld) w; //cast it to Myworld
Counter1 c = m.getCounter1(); //get the counter1 from the world
c.addScore(); //add score to the counter
or just
((Counter1)(MyWorld)getWorld()).getCounter1()).addScore();
Nooooob Nooooob

2017/2/13

#
If I got the counter right:
int score = 0;
  public Counter1()
  {
    setImage(new GreenfootImage("Score: " + score, 24, Color.BLUE, Color.BLACK));
  }
  public void addScore()
  {
       score++;
       updateImage();
       World w = getWorld();
       MyWorld m = (MyWorld) w;
       Counter1 c = m.getCounter1();
       c.addScore();
  }
  public void updateImage()
  {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
  }
I can see the counter but it doesn't get more. I guess the actor is still wrong:
if(isTouching(a.class))
{
(Counter1)((MyWorld)getWorld())getCounter1()).addScore(1);  //it says it's not a statement
}
danpost danpost

2017/2/13

#
Did you look at the line closely and make sure it is written out properly?
Super_Hippo Super_Hippo

2017/2/13

#
Recopy it. I clicked on "Post" too quickly and edited it a minute later. Btw, you can't use 'addScore(1)'. And remove what you added to the addScore method or you will create an infinite loop.
Nooooob Nooooob

2017/2/13

#
Super_Hippo wrote...
remove what you added to the addScore method or you will create an infinite loop.
 int score = 0;
  public Counter1()
  {
    setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
  }
  public void addScore()
  {
       score++;
       updateImage();
  }
  public void updateImage()
  {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
  }
Super_Hippo wrote...
Recopy it.
if(isTouching(a.class))
{
((Counter1)(MyWorld)getWorld()).getCounter1()).addScore();
}
danpost wrote...
Did you look at the line closely and make sure it is written out properly?
I think so.
Super_Hippo Super_Hippo

2017/2/13

#
There is a ( missing before MyWorld I guess. You can use 'updateImage()' in line 4 instead of the setImage duplicate.
danpost danpost

2017/2/13

#
Super_Hippo wrote...
There is a ( missing before MyWorld I guess.
Yep. The compiler cannot find a match to that particular parenthesis.
There are more replies on the next page.
1
2