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
Solringolt Solringolt

2013/4/29

#
Hi everyone, I went a bit further with my project. I have some problems using the counter. When an ennemy is killed I use that to increase the counter
((Counter)getWorld().getObjects(Counter.class).get(0)).add(pointWin);
I don't really know how it work i just copy/pasted it :S This is in my playing world and works but I wanted to show on the menu the highscore. Is there a way to stock the score when the player loose and show it on the menu? If it can help you here is my project.
danpost danpost

2013/4/29

#
Is the menu another world or an actor in the same world?
Solringolt Solringolt

2013/4/29

#
It's another world, parent to the playingWorld
danpost danpost

2013/4/29

#
What you are saying, then, is the menu is another instance of the class that is the super-class of 'playingWorld' (as any instance of a class is an instance of the class that that class extends). What I am saying then, is that one should not extend the other, but both should extend 'World'. You can add an instance field to hold the Counter object in the menu world and a method to set its value.
// add instance field
private Counter counter;
// add method to set value of counter
public void setCounter(Counter counterObj)
{
    counter = counterObj;
}
// and use the following to get its value
int counterValue = counter.getValue();

// create the new menu like this
// (I will call it 'Menu' class -- you fix to your name)
Menu menu = new Menu();
menu.setCounter((Counter)getWorld().getObjects(Counter.class).get(0));
Greenfoot.setWorld(menu);
Remove 'getWorld().' if changing worlds from your playingWorld class.
Solringolt Solringolt

2013/4/29

#
I maybe didnt't explain it right. I have in World one class WorldOptions with two subclass: menu and playingUniverse (where I do my addObject(new Counter....) ) Counter being a subclass of Actor.
danpost danpost

2013/4/29

#
OK, so is 'World' not good enough to have as a parent class to both your worlds? Do the two world types have anything in common that would necessitate (or be good consideration for) them being subclassed under a seperate subclass of world? At any rate, the code above should suffice.
Solringolt Solringolt

2013/4/29

#
Ok thx, I will try to use these instance fields (didn't knew them) I'm using that worldOptions to stock variables like GreenfootSound and access them from both worlds.
danpost danpost

2013/4/29

#
Problem with the way you were doing it, is that each world instance will have a seperate value for each field given in the super-class (not necessarily the same value). That is, unless you are using 'static' content.
Solringolt Solringolt

2013/4/29

#
Yes I use static content for that. I'm sorry but I don't understand where I have to put all the stuff you gave me... I put the Instance and the method in my Menu World and I create the Menu when the player dies but I didn't use the : int counterValue = counter.getValue(); Like that i get an error when the player dies and the menu is supposed to be shown.
danpost danpost

2013/4/29

#
What error are you getting and what is the code that is causing the error?
Solringolt Solringolt

2013/4/29

#
Ok I no more have an error but noting happens on the menu. When I kill an enemy
public void hit(int damage) 
    {
        lifeLeft = lifeLeft - damage;
        if(lifeLeft <= 0)
        {
           ((Counter)getWorld().getObjects(Counter.class).get(0)).add(pointWin);
           getWorld().removeObject(this);
        }
    }
When the Hero is killed:
public void colisionCheck()
    {
        Actor hero = getOneIntersectingObject(Hero.class);
        if (hero != null)
        {
          
            
            Menu menu = new Menu();  
            menu.setCounter((Counter)getWorld().getObjects(Counter.class).get(0));  
            Greenfoot.setWorld(menu);
            
            getWorld().removeObject(this);
           
        }
    } 
My Menu looks like that:
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("Aide","PanneauAide"),700, 600);
        addObject(new Bouton("Son","Option"),1100, 800);
        addObject(new Counter(),100, 800);     
    }
    public void setCounter(Counter counterObj)  
    {  
        counter = counterObj;  
    } 
}
And the PlayUniverse like that:
if (currentLevel == 0) 
       {
           addObject( new Hero(),100,380);
           addObject(new Counter("Score:"),130, 755);
           if (checkForNextLevel() == true)
           {
               currentLevel = 1;
           }
           addObject(new Informer("level1.png"),600,400);
       }
danpost danpost

2013/4/29

#
Remove line 24 of the Menu class and make it the second line in the 'setCounter' method.
Solringolt Solringolt

2013/4/29

#
Is it normal that the counter is reset to 0 as soon as I get killed? I now would like to keep on the menu the best score the player did in one run.
danpost danpost

2013/4/29

#
I forgot to tell you, when you move that line from 24 to the 'setCounter' method, to change 'new Counter()' to 'counter'.
Solringolt Solringolt

2013/4/29

#
Thanks it works! Now comes the difficult part I think, how can I keep on the menu only the best score? Is there a way to compare the older score with the new one?
There are more replies on the next page.
1
2
3