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

2013/4/29

I don't understand how to properly use the counter class

1
2
3
danpost danpost

2013/4/29

#
What you can do, since the Counter object holds the scores to begin with, is add another field to it to hold the highest score (so far).
// declare it as follows
private int highest;

// add these helper methods
private void setHighest()
{
    // 'value' may not be the right name for the variable
    if (value > highest) highest = value;
}

public int getHighest()
{
    return highest;
}
Then, any place the 'value' field is changed, call 'setHighest'. That means in 'add', in 'subtract', in 'setValue', wherever. When a new world is created, you can either pass the counter object to it or set the 'new' counter object with the highest by doing this:
PlayWorld pw = new PlayWorld();
((Counter) newCounter = (Counter)pw.getObjects(Counter.class).get(0);
newCounter.setValue(counter.getHighest());
newCounter.setHighest();
newCounter.setValue(0);
Solringolt Solringolt

2013/4/30

#
Menu menu = new Menu();  
           // menu.setCounter((Counter)getWorld().getObjects(Counter.class).get(0));
           ((Counter) newCounter = (Counter)menu.getObjects(Counter.class).get(0));  
            newCounter.setValue(counter.getHighest());  
            newCounter.setHighest();  
            newCounter.setValue(0);
            
            PlayUniverse playUniverse = (PlayUniverse) getWorld();
            Hero player = playUniverse.getplayer();
            
            getWorld().removeObject(player);
            
            Greenfoot.delay(80);
            
            Greenfoot.setWorld(menu);
I have that when the player dies but I got a compilation error: ((Counter) newCounter = (Counter)menu.getObjects(Counter.class).get(0)); not a statement
Solringolt Solringolt

2013/4/30

#
little up ^^
danpost danpost

2013/4/30

#
I must have gotten parenthesis happy. Start that line with 'Counter newCounter = ...'.
Solringolt Solringolt

2013/4/30

#
newCounter.setValue(counter.getHighest()); cannot find symbol - variable counter I got this error at compilation...
danpost danpost

2013/4/30

#
Ok, you are probably still in an actor class. Try:
newCounter.setValue(((Counter)getWorld().getObjects(Counter.class).get(0)).getHighest());
Solringolt Solringolt

2013/4/30

#
Now it compils but when I die I get this error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 for the line Counter newCounter = (Counter)menu.getObjects(Counter.class).get(0); I thought it was maybe somthing like in this case but it probably not.
danpost danpost

2013/4/30

#
Please show the code both where you detect that your actor dies and wherever execution goes after that until you create the new world and work with the counter.
Solringolt Solringolt

2013/5/1

#
That's what happens when I die:
if (value <= 0)
        {
           Menu menu = new Menu();  
           Counter newCounter = (Counter)menu.getObjects(Counter.class).get(0);  
            
           newCounter.setValue(((Counter)getWorld().getObjects(Counter.class).get(0)).getHighest()); ;  
           newCounter.setHighest();  
           newCounter.setValue(0);
           
           PlayUniverse playUniverse = (PlayUniverse) getWorld();
           Hero player = playUniverse.getplayer();
           
           getWorld().removeObject(player);
           
           Greenfoot.delay(80);
           
           Greenfoot.setWorld(menu);
        }
And that is my world that's set up:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Menu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Menu extends WorldOption
{

    /**
     * Constructor for objects of class Menu.
     * 
     */
    
    private Counter counter;
    
    public Menu()
    {    
        addObject(new Bouton("Jouer","Monde"),600, 450);
        addObject(new Bouton("Son","Option"),1100, 800);
            
    }
    public void setCounter(Counter counterObj)  
    {  
        counter = counterObj;
        addObject(counter,125, 755); 
    } 
}
I hope I gave you the right informations.
Solringolt Solringolt

2013/5/1

#
Little up, I show a first view of my project tomorrow that's why I hurry
danpost danpost

2013/5/1

#
For the code when you die, change it to the following:
if (value <= 0)
{
    Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0); // gets the counter
    counter.setHighest(); // adjusts current high, if beaten
    counter.setValue(getHighest()); // sets value to high score
    Menu menu = new Menu();
    menu.setCounter = counter;
    PlayUniverse playUniverse = (PlayUniverse)getWorld();
    Hero player = playUniverse.getplayer();
    getWorld().removeObject(player);
    Greenfoot.delay(80);
    Greenfoot.setWorld(menu);
}
Solringolt Solringolt

2013/5/1

#
Sorry it doens't work i got that for the line 5 of your code: cannot find symbol - method getHighest() I tried to change it to this:
if (value <= 0)  
        {  
            Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0); // gets the counter  
            
            counter.setHighest(); // adjusts current high, if beaten  
            counter.setValue(counter.getHighest()); // sets value to high score  
            
            Menu menu = new Menu();  
            menu.setCounter(counter);  //this line was also an error if first one solved
            PlayUniverse playUniverse = (PlayUniverse)getWorld();  
            Hero player = playUniverse.getplayer();  
            getWorld().removeObject(player);  
            Greenfoot.delay(80);  
            Greenfoot.setWorld(menu);  
        }
But the highest score isn't shown only the last done.
danpost danpost

2013/5/1

#
When we introduced 'highest' into the Counter class, we also introduced two helper methods ('getHighest' and 'setHighest'). They should both be in your Counter class. Change the method declaration to 'public' for the 'setHighest' method.
Solringolt Solringolt

2013/5/1

#
public void setHighest()  
    {  
        if (value > highest) highest = value;  
    }
    public int getHighest()  
    {  
        return highest;  
    }  
They were already on public..
danpost danpost

2013/5/1

#
I do not see how you would be getting that message. Obviously, if you did not have a Counter object, it would give a NullPointerException. So, maybe we should look at the Counter class code.
There are more replies on the next page.
1
2
3