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

2014/1/13

Stop the scoreboard.

1
2
Jabadu Jabadu

2014/1/13

#
Hello. I' having some troubles with stopping the scoreboard. I set the limit to 100, but scoreboard keep on counting forward. So here is my code:
if (counter.getValue() == 100)
        {
            Greenfoot.stop();
        }
Am i missing something ?
danpost danpost

2014/1/13

#
If you counter ever get increased by more than one in an act cycle, you may skip over 100, so it is never 100 when it check. You could try '>=' instead.
Zamoht Zamoht

2014/1/13

#
if (counter.getValue() >= 100) { Greenfoot.stop(); } This should fix your problem. If not, then I'm pretty sure the problem is elsewhere. @danpost damn you are fast :D
Jabadu Jabadu

2014/1/13

#
I changed and still nothing.. I have this method called in act method:
public void pojejPizza()
    {
        if(canSee(Pizza.class))
        {
            eat(Pizza.class);
            novaPizza();
            counter1.add(10);
        }
        if (counter1.getValue() >= 100)
        {
            Greenfoot.stop();
        }
    }
Is there a problem?
danpost danpost

2014/1/13

#
I do not see any problems within the context of the method itself. Maybe if you show the code for the whole class, then it might be possible to crack this.
Jabadu Jabadu

2014/1/13

#
Ok, here it is, the whole class which is pretty long..
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * @author 
 * @version 
 */
public class Pika1 extends Pikice
{
    private Counter1 counter1;
    private int speed;
    private int ItemZakasnitev;
    private int PizzaZakasnitev;
    private int pospesekZaMalo;

    /**
     * Konstruktor razreda Pika1. Uveden zaradi štetja točk (Counter-ja), 
     * dodanih tudi nekaj ostalih parametrov.
     */
    public Pika1(Counter1 pointCounter)
    {
        counter1 = pointCounter;
        speed = 2;
        ItemZakasnitev = 0;
        PizzaZakasnitev = 0;
        pospesekZaMalo = 0;
    }
   
    /**
     * Act metoda v kateri se izvajajo izbrane metode razreda Pika1, 
     * ko igro zaženemo.
     */
    public void act() 
    {
        move(speed);
        turnPika();
        obRobu(); 
        pospesi1();
        risanjeSledi();
        pojejItem(); 
        preveriItem();
        pojejPizza();
        preveriPizza();
        konecRunde();
        zacetekNoveRunde();
    }
   
    /**
     * Metoda riše sled (slikico razreda "PikaTelo").
     */  
    public void risanjeSledi()
    {
        getWorld().addObject(new Pika1Telo(), getX(), getY());
    }
    
  
    /**
     * Metoda deklarira tipki za usmerjanje glavnega objekta.
     */
    public void turnPika()
    {
          if (Greenfoot.isKeyDown("d"))
        {
            turn(4);
        }
        
        if (Greenfoot.isKeyDown("a"))
        {
            turn(-4);
        }
    }
    
    
    /**
     * Metoda resetira igro če se igralec zaleti v rob igralnega polja. 
     * Ob tem nasprotnemu igralcu pripiše 1 točko.
     */
    public void obRobu()
    {
          if (obRobuSveta())
        {
            Greenfoot.setWorld(new Zid());
        }
    }
    
    /**
     * Metoda nam ob stiku "Pike" z "Item" dodeli pospešek in kliče drugo
     * metodo za namestitev novega "Item".
     */
    public void pojejItem()
    {
        if (canSee(Item.class) )
        {
            eat(Item.class);
            novItem();
            pospesek1();
        }
    }
    
    /**
    * Metoda deklarira čas trajanja pospeška.
    */
    private void pospesek1()
    {
         pospesekZaMalo=150;
    }
    
    /**
    * Metoda nam dodeli pospešek in nam po iztečenem časovnem intervalu
    * vrne hitrost na začetno stanje.
    */ 
    private void pospesi1()
    {         
        if (pospesekZaMalo>0)
        {  
            move(3);
            pospesekZaMalo--;
                    
            if(pospesekZaMalo == 0)
            {
                move(2);
            } 
        }
    }
    
    /**
    * Metoda nam pove zakasnitev pojave novega "Itema".
    */
    private void novItem()
    {
        ItemZakasnitev = 100;        
    }
    
    /**
     * Metoda odšteva dodeljeno zakasnitev in ob izteku le te ustvari
     * nov "Item".
     */
    private void preveriItem()
    {
        if(ItemZakasnitev > 0)
        {
            ItemZakasnitev--;
            
            if(ItemZakasnitev == 0)
            {
                ustvariNovItem();
            }
        }
    }
    
    /**
     * Metoda ustvari nov "Item" na naključnem mestu v igralnem polju.
     */
    private void ustvariNovItem()
    {
        Item newItem;
        newItem = new Item();
        
        World world;
        world = getWorld();
        
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        
        world.addObject(newItem, x, y);
    }
    
    public void pojejPizza()
    {
        if(canSee(Pizza.class))
        {
            eat(Pizza.class);
            novaPizza();
            counter1.add(10);
            }
        if (counter1.getValue() >= 100)
        {
            konecIgre();
        }
    
    }
    
    private void novaPizza()
    {
        PizzaZakasnitev = 100;
    }
    
    private void preveriPizza()
    {
        if(PizzaZakasnitev >0)
        {
            PizzaZakasnitev--;
            if(PizzaZakasnitev==0)
            {
                ustvariNovoPizza();
            }
        }
    }
    
    private void ustvariNovoPizza()
    {
        Pizza newPizza;
        newPizza = new Pizza();
        
        World world;
        world = getWorld();
        
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        
        world.addObject(newPizza, x, y);
    }
    /**
     * Metoda nam ob trčenju obeh igralcev igro resetira, in prišteje eno
     * točko igralcu, ki se kasneje zaleti. Ob 10ih zbranih točkah posameznega
     * igralca se kliče nova metoda, ki nam zaustavi celotno igro.
     */
    public void konecRunde()
    {
         if(canSee(PikaTelo.class))
        {
            eat(PikaTelo.class);
            Greenfoot.setWorld(new Zid());
            zacetekNoveRunde();
        }
    }
    
    /**
     * Metoda avtomatsko zažene novo rundo.
     */
    public void zacetekNoveRunde()
    {
        Greenfoot.start();
    }
    
    /**
     * Metoda nam zaključi igro.
     */
    public void konecIgre()
    {
        Greenfoot.stop();
    }
}
danpost danpost

2014/1/14

#
I do not see anything amiss here either. Can I see the code for the active world class.
Jabadu Jabadu

2014/1/14

#
This is in world class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Zid here.
 * 
 * @author 
 * @version 
 */
public class Zid extends World
{
    static final int worldWidth = 1000, worldHeight = 600;
   Pika pika;
   Pika1 pika1;
   Counter counter;
   Counter1 counter1;
   public Item item;

    public Zid()
    {
        super(1000, 600, 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()
    {
        Counter counter = new Counter();
        addObject(counter, 60, 23);
        Pika pika = new Pika(counter);
        addObject(pika, 300, 300);

        Counter1 counter1 = new Counter1();
        addObject(counter1, 153, 22);
        Pika1 pika1 = new Pika1(counter1);
        addObject(pika1, 862, 252);
        
        Item item = new Item();
        addObject(item, 630, 405);
        Item item2 = new Item();
        addObject(item2, 800, 125);
        item.setLocation(541, 547);
        item2.setLocation(540, 64);
        Pizza pizza = new Pizza();
        addObject(pizza, 740, 167);
        Pizza pizza2 = new Pizza();
        addObject(pizza2, 381, 453);
    }
}
danpost danpost

2014/1/14

#
The counters look ok here too. What counter class are you using? Maybe show that code.
Jabadu Jabadu

2014/1/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * @author
 * @version
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private static int value;
    private int target;

    /**
     * Konstruktor.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Spreminja vrednosti točkovnika.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Doda novo številko v točkovnik.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Vrne trenutno vrednost. 
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Nastavi novo vrednost točkovniku.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update-a slikico točkovnika in primerno izpisuje rezultate.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
danpost danpost

2014/1/14

#
I think that was the wrong Counter class, doesn't the Pika1 actor use a Counter1 object? The value field in the Counter class should not be 'static'.
Jabadu Jabadu

2014/1/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author 
 * @version 
 */
public class Counter1 extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Naredi nov točkovnik in ga nastavi na privzete vrednosti 0.
     */
    public Counter1()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Spreminja vrednosti točkovnika.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }    
   
    /**
     * Doda novo številko v točkovnik.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Vrne trenutno vrednost.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Nastavi novo vrednost točkovniku.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     *Update-a slikico točkovnika in primerno izpisuje rezultate.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Oh I didn't saw it, sorry. But actually is the same as first one.. 2 actors, 2 counters.
danpost danpost

2014/1/14

#
I do not understand why you would need two Counter classes -- ?one for each actor? You can create two separate counter instances from the same class.
Counter counter = new Counter();
Counter counter1 = new Counter();
This produces the same result as having two different classes with the same code. That is one of the perks of Object Oriented Programming (OOP). A class is a blueprint and you can create many objects from the same blueprint.
Jabadu Jabadu

2014/1/14

#
Ok, I didnt know it, thanks. But i repair this and delete Counter1, it still don't work as it should..
danpost danpost

2014/1/14

#
Oh well, I did not see anything that would cause the results you are getting. I guess you will have to upload the scenario with the 'Publish source code' checkbox checked, so we can troubleshoot it.
There are more replies on the next page.
1
2