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

2015/6/18

Score doesn't work it counts all the time

whitestag whitestag

2015/6/18

#
I wanted to make a score that counts everytime it touches the Object Essen but it counts all the time could somebody please explain me where my fault is first i was setting up the class counter with the method Punkterzielen to add up the score by one
public class Counter extends Actor
{
    int counter= 1;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + counter, 40, Color.PINK,Color.WHITE));
    }    
    
    public void Punkterzielen(){
     counter++;
}
}
then I was setting it into my World class Welt and made the method getCounter so I could later use it later
Counter counter= new Counter();
 Einhorn unicorn = new Einhorn();

    /**
     * Constructor for objects of class Welt.
     * 
     */
    public Welt()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 376, 1, false);
        
        
        
        
        //Einhorn in Welt setzen
        addObject(unicorn, 100, getHeight()/2); 
       
       
        //Score in Welt setzen
        addObject(counter, 700,50);
       
        
        
        
    }
    public Counter getCounter()
{ 
    return counter;
}
and then I created in the class Einhorn the method essen which should make that if the Einhorn touches the Essen it disappears and it will add a point to the score
public void essen()
{
     if(isTouching(Essen.class)==true);
     {World myWorld =getWorld();
     Welt welt= (Welt)myWorld;
     Counter counter= welt.getCounter();
     counter.Punkterzielen();
     myWorld.removeObject(getOneIntersectingObject(Essen.class));}
     
    
    }
I hope somebody can explain me why the counter just get up without the Einhorn touching the Essen
miningmanna miningmanna

2015/6/18

#
is the function essen inside the act method of Einhorn?
whitestag whitestag

2015/6/18

#
yes the removing of the function essen works just the counting when it does it doesn't work
danpost danpost

2015/6/18

#
The fault does not appear to be in the code given. Please post the remainder of your Welt and Einhorn class codes.
whitestag whitestag

2015/6/18

#
the whole world code is
public class Welt extends World
{
double zaehlerr= 1;
double k =30;
double zaehler= 1;
double o= 50;
double nummer=1;
double i= 100;

 Counter counter= new Counter();
 Actor unicorn = new Einhorn();

    /**
     * Constructor for objects of class Welt.
     * 
     */
    public Welt()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 376, 1, false);
        
        
        
        
        //Einhorn in Welt setzen
        addObject(unicorn, 100, getHeight()/2); 
       
       
        //Score in Welt setzen
        addObject(counter, 700,50);
       
        
        
        
    }
    public Counter getCounter()
{ 
    return counter;
}

    public void act(){
zaehlerr++;

        if(zaehlerr == k){
            //Schloss erstellen
            
            Burg Burg = new Burg();
            
            GreenfootImage image= Burg.getImage();
            
            //Bildbereich
            addObject(Burg, getWidth(), Greenfoot.getRandomNumber(116)+200);
            
            zaehlerr= 0;
            
            // Abstand zwischen den Schlössern
            
            k = Greenfoot.getRandomNumber(100)+50;
           
           
            
}

  
       zaehler++;
        if(zaehler == o){
            //Wolke erstellen
            
            Cloud Cloud = new Cloud();
            
            GreenfootImage image= Cloud.getImage();
            
            addObject(Cloud, getWidth(), Greenfoot.getRandomNumber(115)+35);
            
            zaehler=0 ;
            o= Greenfoot.getRandomNumber(100)+60;
}
 nummer++;
        if(nummer == i){
            //Essen erstellen
            
            Essen Essen = new Essen();
            
            GreenfootImage image= Essen.getImage();
            
            addObject(Essen, getWidth(), Greenfoot.getRandomNumber(250)+50);
            
            nummer=0 ;
            i= (i- Greenfoot.getRandomNumber(50)+100);
}
}

}
and whole Einhorn class code is :
public class Einhorn extends Actor
{
   double dy = 2;
   double Schwerkraft = 1.5;
   double fliegen= -10;
    /**
     * Act - do whatever the Einhorn wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    public void act() {
    //Einhorn verliert Höhe
      setLocation( getX() ,(int)( getY() + dy)); 
      dy= dy+ Schwerkraft ;
      
     bewegung();
     Gameover();
     essen();
     }
     
     
     /**
      * Bewegen- sowohl das nach oben pushen 
      * als auch nach vorne
      * und hinten bewegen
      */
     public void bewegung(){
         
   //Einhorn fliegen lassen wenn Pfeiltaste gedrückt
        if(Greenfoot.isKeyDown("up")== true){
            dy = fliegen;
        }
    //nach vorne und hinten
    int x = getX();//nimmt x-Koordinate auf
    int y = getY();//nimmt y-Koordinate auf
        if(Greenfoot.isKeyDown("left")==true) {
            setLocation(x - 4, y );
        }
        
        if(Greenfoot.isKeyDown("right")==true) {
            setLocation(x + 4, y );
        }
        
       
    }
      
      /**
       * Gameover- ab wann haben wir das Spiel verloren
       * -wenn wir unter den unteren Rand fallen
       *- wenn Einhorn zu weit nach oben fliegt 
       * -wenn wir ein Schloss berühren
       * -wenn wir eine Wolke berühren
       */
      
  public void Gameover(){ 
       
  //Game Over Anzeige wenn Einhorn unteren Rand berührt
        if( getY() > getWorld().getHeight()) {
           GameOveranzeigen();
        }
        
//Game Over Anzeuge wenn Einhorn zu weit nach oben fliegt
        if(  getY() < -50){
            GameOveranzeigen();
        }
        
 //Game Over Anzeige wenn Einhorn  Hindernis berührt
        if(getOneObjectAtOffset(50,0,Hindernis.class) !=null || getOneObjectAtOffset(0,50,Hindernis.class) !=null || getOneObjectAtOffset(-50,0,Hindernis.class) !=null|| getOneObjectAtOffset(0, -50, Hindernis.class) !=null){
           GameOveranzeigen();
        }
    }
    
    
    /**Definition der Methode GameOveranzeigen
     * 
     */
    public void GameOveranzeigen()
    {
        Game_Over Game_Over = new Game_Over();
        getWorld().addObject(Game_Over, getWorld().getWidth()/2, getWorld().getHeight()/2);
        
        Greenfoot.stop();
    
}
/**
 * Wenn das Einhorn das Essen berührt verschwindet es
 */
public void essen()
{
     if(isTouching(Essen.class)==true);
     {World myWorld =getWorld();
     Welt welt= (Welt)myWorld;
     Counter counter= welt.getCounter();
     counter.Punkterzielen();
     myWorld.removeObject(getOneIntersectingObject(Essen.class));}
     
    
    }
  }
danpost danpost

2015/6/18

#
I still do not see anything that would cause your score counter to count all the time (I presume you mean that it continuously increments during gameplay). Is there any other code that deals with the score counter object? (check all your classes)
whitestag whitestag

2015/6/18

#
yes thats what I meant. No, I only got two more classes one is the Game Over class but there isn't anything in it and the other one is the Object that is disappearing and should be counted but in there is just the function to move it
public class Essen extends Actor
{ int Geschwindigkeit=-10;
    /**
     * Act - do whatever the Essen wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
 setLocation( getX() + Geschwindigkeit, getY());
 
}
}
danpost danpost

2015/6/18

#
I found it. In your Einhorn class at line 90 above (the 'if' line for 'isTouching' Essen objects), you have a semi-colon at the end. This is equivalent of having a no-op statement there which is then considered your entire 'if' block code. So the code within the braces is processed without regard to the 'if' condition. Remove that semi-colon.
whitestag whitestag

2015/6/18

#
it worked now thank you so much :)
You need to login to post a reply.