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

2019/12/7

Score counter isn't working

annika.g annika.g

2019/12/7

#
So I followed a youtube tutorial on how to create a score counter, the counters there and everything but it won't count anything. If the actor, Pacman, eats a coin the score should go up by one and if he eats a Kirsche (cherrie) the score should go up by 2. But it just doesn't go up. Can somebody help me? Code of the counter1 class:
import greenfoot.*;
import java.awt.Color;
public class Counter1 extends Actor
{
  int score = 0;
  public Counter1()
  {
    setImage(new GreenfootImage("Score: " + score, 24, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
  }
  public void addScore()
  {
       score++;
       updateImage();
  }
  public void updateImage()
  {
        setImage(new GreenfootImage("Score: " + score, 24, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
  }
 
}
code of the actor, who eats the coins/Kirschen:
Actor Kirsche = getOneIntersectingObject (Kirsche.class);    
            if(Kirsche!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Kirsche);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.addScore();
              counter1.updateImage();
            }
            Actor Coin = getOneIntersectingObject (Coin.class);
            if(Coin!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Coin);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.updateImage();
The world code:
public class RoboterWelt extends World
{  
    Counter1 counter1 = new Counter1();
    private static int zellenGroesse = 20;
    private int score = 0;}
public Counter1 getCounter1()
    {
        return counter1;
    }
I didn't put anythinf in the coin or Kirsche class, maybe that's the mistake? I don't know, please help me.
danpost danpost

2019/12/7

#
Remove:
import java.awt.Color;
from any and all classes. Then, you can drop 'greenfoot.' from in front of all 'Color'. Remove:
private int score;
from your RoboterWelt class. Your Counter1 object holds the score value. Show your entire RoboterWelt class for review.
annika.g annika.g

2019/12/7

#
Alright, I did that but it still doesn't work. I just checked and the score counter increases its score when Pacman hits a ghost for the first time. The other times he hits a ghost it doesn't go up. Anyway, heres the code for the RoboterWelt:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die einzigen aktiven Akteure in der Roboterwelt sind die Roboter.
 * Die Welt besteht aus 26 * 34 Feldern.
 */

public class RoboterWelt extends World
{  
    Counter1 counter1 = new Counter1();
    private static int zellenGroesse = 20;
    
    HealthBar healthbar = new HealthBar();


    /**
     * Erschaffe eine Welt mit 26 * 34 Zellen.
     */
    
    public RoboterWelt()
    {
        super(26, 34, zellenGroesse);
        GreenfootImage bg = new GreenfootImage ("Black_from_a_camera.jpg");
       
        
        setPaintOrder(Schraube.class, Akku.class,  Roboter.class, Wand.class);
        Greenfoot.setSpeed(35);   
        prepare();
    }
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
    public Counter1 getCounter1()
    {
        return counter1;
    }
    public void act()
    {if(healthbar.health<0)
                    {
                        
                        Greenfoot.setWorld(new GameOver()); 
                    }}
                    
    private void prepare()
    {   addObject (healthbar, 21, 32);
        addObejct (counter1, 2, 32);
danpost danpost

2019/12/7

#
annika.g wrote...
the score counter increases its score when Pacman hits a ghost for the first time. The other times he hits a ghost it doesn't go up.
Please show Pacman codes.
annika.g annika.g

2019/12/7

#
int score;
     public void KirscheAufnehmen()
    {
            Kirsche aktKirsche = (Kirsche)this.getOneObjectAtOffset(0, 0, Kirsche.class);
            if(aktKirsche != null)
            {
                this.getWorld().removeObject(aktKirsche);
                score ++;
                score++;
            }
        }
    public void CoinAufnehmen()
    {
            Coin aktCoin = (Coin)this.getOneObjectAtOffset(0, 0, Coin.class);
            if(aktCoin != null)
            {
                this.getWorld().removeObject(aktCoin);
                score ++;
            }    
public boolean KirscheAufFeld()
    {
        if (this.getOneObjectAtOffset(0, 0, Kirsche.class)!= null)
        {
           return true;     
        } 
        else
        {
            return false;   
        }
    }
     public boolean CoinAufFeld()
    {
        if (this.getOneObjectAtOffset(0, 0, Coin.class)!= null)
        
        {
           return true;     
        } 
        else
        {
            return false;   
        }
    }
     public boolean BlauerGeistAufFeld ()
    {
        if (this.getOneObjectAtOffset(0, 0, BlauerGeist.class)!= null)
        {
           return true;     
        } 
        else
        {
            return false;   
        }
    }
  [code]public void act() 
    {
        health=3;
        
            if(Greenfoot.isKeyDown("up"))
            {setRotation(Direction.UP);
                bewegen();
            }
            else   
            if(Greenfoot.isKeyDown("down"))
            {setRotation(Direction.DOWN);
                bewegen();
            }
            else  
            if(Greenfoot.isKeyDown("left"))
            {setRotation(Direction.LEFT);
                bewegen();
            }
            else 
            if(Greenfoot.isKeyDown("right"))
            {setRotation(Direction.RIGHT);
                bewegen();
            }
            if (KirscheAufFeld())
                {
                    KirscheAufnehmen();
                   
                }
           
            Actor Kirsche = getOneIntersectingObject (Kirsche.class);    
            if(Kirsche!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Kirsche);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.addScore();
              counter1.updateImage();
            }
            Actor Coin = getOneIntersectingObject (Coin.class);
            if(Coin!=null)
            {
              World myWorld = getWorld();
              myWorld.removeObject(Coin);
              RoboterWelt roboterwelt = (RoboterWelt) myWorld;
              Counter1 counter1 = roboterwelt.getCounter1();
              counter1.addScore();
              counter1.updateImage();
            }
    }
        public void hitBlauerGeist()
        {
                Actor BlauerGeist = getOneIntersectingObject(BlauerGeist.class);
            if (BlauerGeist!=null)
            {
                World myWorld = getWorld();
                RoboterWelt roboterwelt =  (RoboterWelt)myWorld;
                HealthBar healthbar = roboterwelt.getHealthBar();
                if (touchingBlauerGeist == true)
                {
                    healthbar.loseHealth();
                    touchingBlauerGeist = true;
                    
                    if(healthbar.health<0)
                    {
                        myWorld.removeObject(this);
                        Greenfoot.setWorld(new GameOver()); 
                    }
                }
               
            
        }
        else 
        { touchingBlauerGeist = false;
           
        }
       
danpost danpost

2019/12/7

#
I do not see anywhere where the score is upped when hitting a ghost. Also, I do not see from where the hitBlauerGeist method is being called. Q: in what class is the 19-line code set just given above? and why is there a score field there when you already have one in the Counter1 class?
You need to login to post a reply.