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

#
I see thx, that's fine now but the counter still doesn't go up.
danpost danpost

2017/2/13

#
Ignore this. May edit in a moment.
Nooooob Nooooob

2017/2/13

#
danpost wrote...
Ignore this. May edit in a moment.
Ok.
danpost danpost

2017/2/13

#
Nooooob wrote...
@Super_Hippo 1. Yes.
addObject(counter1, 40, 10);
Can you show the rest of the method this line is in, please? BTW, I cannot edit it if you reply to it.
Nooooob Nooooob

2017/2/13

#
public class MyWorld extends World
{
Counter1 counter1 = new Counter1();
public MyWorld()
{    
super(600, 500, 1); 
prepare();
}
public void prepare()
{
    addObject(counter1, 40, 10); //the counter
    addObject(Actor(), 100, 450); //the actor
}
danpost danpost

2017/2/13

#
Nooooob wrote...
< Code Omitted
Okay. just making sure you are only creating one Counter1 object here.
danpost danpost

2017/2/13

#
You have made a lot of change. Let us get an overview of what you currently have to check out (entire Counter1 class and the Actor subclass).
Nooooob Nooooob

2017/2/13

#
Counter1:
import greenfoot.*;
import java.awt.Color;
public class Counter1 extends Actor
{
  int score = 0;
  public Counter1()
  {
    setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
  }
  public void addScore()
  {
       score++;
  }
  public void updateImage()
  {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
  }
}
Actor:
import greenfoot.*;
public class Actor extends Actor
{
private int speed = 3;
public void act() 
{
move(speed);
 if(Greenfoot.isKeyDown("left"))
      { 
          turn(-2);
      }
        if(Greenfoot.isKeyDown("right"))
      { 
          turn(2);
      }
       if(isTouching(a.class))
       {
           ((Counter1)((MyWorld)getWorld()).getCounter1()).addScore();
removeTouching(a.class);
       }
}
It works fine, just the counter won't go up.
danpost danpost

2017/2/13

#
You are not calling 'updateImage' from the 'addScore' method after incrementing the value of the 'score' field. (seems you removed that line for some unknown reason)
Nooooob Nooooob

2017/2/13

#
danpost wrote...
You are not calling 'updateImage' from the 'addScore' method after incrementing the value of the 'score' field. (seems you removed that line for some unknown reason)
It's working :) THANKS A LOT! @Super_Hippo too!
You need to login to post a reply.
1
2