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

2017/3/24

count a timer while an special actor is in the game

1
2
Fifilein Fifilein

2017/3/24

#
Hi! I need your help. I want my timer to count up when the GameOverclass is empty and the Grapeclass is not empty. The part with the GameOverclass works, but not the part with the Grapeclass. I tried this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class frogfly here.
 * 
 * @author (Fifilein) 
 * @version (2.4)
 */
public class frogfly extends World
{
 Counter counter = new Counter();
 private int timer;
 private Actor timerDisplay; 
 Grape g = new Grape();
    /**
     * Constructor for objects of class frogfly.
     * 
     */
    public frogfly()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);   
        timer = 1;
        timerDisplay = new SimpleActor();
        addObject(timerDisplay, 220, 20); // change location as desired
        updateTimerDisplay(); //Button endles = new Buttony(300, 100, 0);
        //
        //    addObject(new Grape(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
        //}  
        int x = Greenfoot.getRandomNumber(10);
        for (int i = 2; i<=x; i++){
           addObject(new Grape(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
        }
        
        
        
            addObject(new Lobster(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
        
        
         addObject(new Hippo(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));  
            
        
           addObject(counter, 100, 40); 
        
        
    }
     public void act()
    {
   
         //if (g != null)
        if (timer > 0 && getObjects(GameOver.class).isEmpty() && g != null)
    { timer++; if (timer%60 == 0) { 
         updateTimerDisplay(); }
    } 
}
private void updateTimerDisplay()
    {timerDisplay.setImage(new GreenfootImage("Time remaining: "+(timer/60), 24, Color.RED, new Color(0, 0, 0, 0))); // adjust text as desired
    }
     public  Counter getCounter()
   {
      return counter; 
    }
    
}
Can you help me please? Thx, Fifilein
danpost danpost

2017/3/24

#
In line 51, you have 'g != null'. This asks if the field 'g' references a Grape object or not. Obviously, it will due to line 14, which assigns it one to reference. But, that is not really what you want to ask there. You want to ask if the Grape object is in the world or not. There are two ways of doing that -- (1) using 'getObjects(Grape.class)' and seeing if the list returned is empty or not; and (2) using 'g.getWorld()' and checking if the referenced 'g' object currently belongs to a world. The second way is probably not the desired way as (a) a Grape object could be in the world, but it is not the one referenced by the 'g' field and (b) the world may that the referenced 'g' object is in may not be the world you are asking about (although it probably will be). At any rate, the first way will definitely check 'this' world and detect any Grape object in it. So, instead of 'g != null', use '! getObjects(Grape.class).isEmpty()'. I think I would also remove line 23 and remove 'timer > 0 &&' from line 51.
Fifilein Fifilein

2017/3/24

#
Thank you! I have another question, when the Grapeclass is empty I want to apear a screen which tell you how many grapes in how many seconds you collected. Can you give me an idea how to do this? Thx
danpost danpost

2017/3/24

#
Fifilein wrote...
when the Grapeclass is empty I want to apear a screen which tell you how many grapes in how many seconds you collected. Can you give me an idea how to do this? Thx
Create an actor to display the info and add it into the world (as long as one is not already in the world).
Fifilein Fifilein

2017/3/25

#
danpost wrote...
create an actor to display the info and add it into the world (as long as one is not already in the world).
. Thanks! Is there a method to add an object and make it invisible and if another class is empty to make it visible?
danpost danpost

2017/3/25

#
Fifilein wrote...
Is there a method to add an object and make it invisible and if another class is empty to make it visible?
It is possible -- yes. But, why would you want to do that when you can just add the actor into the world at the time you would otherwise be making it visible.
Fifilein Fifilein

2017/3/25

#
danpost wrote...
Fifilein wrote...
Is there a method to add an object and make it invisible and if another class is empty to make it visible?
It is possible -- yes. But, why would you want to do that when you can just add the actor into the world at the time you would otherwise be making it visible.
I want to Show the time and score you reached.When I add info the timer starts Count up from 0 and the score is 0.My thought was I can make a duplicate timer and counterdisplay and make the second invisible and when the grapeclass is empty to make it visible.
Fifilein Fifilein

2017/3/26

#
Or do you know a way when you have a timer and it Counts your time.And when you add later the same timer once again, the same timer is shown?
Super_Hippo Super_Hippo

2017/3/26

#
You don't have to add the timer to the world if you don't want to show it (line 25). Add it when you want to add it. You also don't need to change the image of it until it is shown.
Fifilein Fifilein

2017/3/26

#
Super_Hippo wrote...
You don't have to add the timer to the world if you don't want to show it (line 25). Add it when you want to add it. You also don't need to change the image of it until it is shown.
Okay.And how do you think can solve my problem?At the end of my game I want to show a YouWindisplay which tells you you how many grapes in how many seconds you collected.
danpost danpost

2017/3/26

#
Remove line 14 -- there is no need to reference a single grape (especially one that is never added into the world). Then, add 2 to the 'x' in line 31 to ensure at least 2 grapes will be added into the world. Finally, in the act method, add an 'if' statement/block that checks for no grapes in the world and adds the game over object into the world (it can also move/modify the counter and the timerDisplay).
Fifilein Fifilein

2017/3/26

#
danpost wrote...
Remove line 14 -- there is no need to reference a single grape (especially one that is never added into the world). Then, add 2 to the 'x' in line 31 to ensure at least 2 grapes will be added into the world. Finally, in the act method, add an 'if' statement/block that checks for no grapes in the world and adds the game over object into the world (it can also move/modify the counter and the timerDisplay).
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class frogfly here.
 * 
 * @author (Fifilein) 
 * @version (2.4)
 */
public class frogfly extends World
{
 Counter counter = new Counter();
 private int timer;
 private int AnzahlGrapes;
 private Actor timerDisplay; 
 private Actor winDisplay; 
  GreenfootImage iimmage = new GreenfootImage("frogflyhintergrund.png");
 GreenfootImage iimmaage = new GreenfootImage("Gewonnen.png");
 GreenfootImage iimmaagge = new GreenfootImage("Verlorenhintergrund.png");
//public Lobster startknopa;
    /**
     * Constructor for objects of class frogfly.
     * 
     */
    public frogfly()
    {         
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);   
        setBackground(iimmage);
        timerDisplay = new SimpleActor();
        addObject(timerDisplay, 220, 20);
         winDisplay = new SimpleActor2();
        addObject( winDisplay, 320, 20);// change location as desired
        updateTimerDisplay(); 
        updateWinDisplay();
        
        int x = Greenfoot.getRandomNumber(10);
        for (int i = 2; i<=x; i++){
           addObject(new Grape(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
            addObject(new Grape(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
        }
        AnzahlGrapes = x;
        
        
            addObject(new Lobster(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
        
        
         addObject(new Hippo(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));  
            
        
           addObject(counter, 100, 40); 
        
        
    }
     public void act()
    {
  
  if(getObjects(Grape.class).isEmpty())
        {
         setBackground(iimmaage);
          
        } 
        if(getObjects(Hippo.class).isEmpty())
        {
         setBackground(iimmaagge);
       addObject(new GameOver, getWidth()/2,  getHeight()/2);
     
        }  
        if ( getObjects(GameOver.class).isEmpty() && ! getObjects(Grape.class).isEmpty())
    { timer++; if (timer%60 == 0) { 
         updateTimerDisplay();
         updateWinDisplay();}
    } 
}
private void updateTimerDisplay()
    {timerDisplay.setImage(new GreenfootImage("Time: "+(timer/60), 24, Color.RED, new Color(0, 0, 0, 0))); // adjust text as desired
    }
    private void updateWinDisplay()
    {winDisplay.setImage(new GreenfootImage("Du hast: " +(AnzahlGrapes), 24, Color.RED, new Color(0, 0, 0, 0))); // adjust text as desired
    }
     public  Counter getCounter()
   {
      return counter; 
    }
    
}
I am not sure,whether I had do all thinhgs right so here is my Code:
Super_Hippo Super_Hippo

2017/3/26

#
Move lines 30 and 32 to both 66 and 70. Is it wanted that you only call these update methods when winning and not when losing? (I am not sure why you have two SimpleActor classes.)
Fifilein Fifilein

2017/3/27

#
Yes , for my GameOver I have an Image and not These update methods.
danpost danpost

2017/3/27

#
Lines 36 through 41, where you add the grapes into the world looks a bit off. For one thing, if the random number generated for 'x' is less than 2, no grapes will be added into the world and (presumably) the game will end the moment it starts. Try replacing those lines with this:
AnzahlGrapes = 2 + Greenfoot.getRandomNumber(11); // min is 2, max = 12
for (int i = 0; i<AnzahlGrapes; i++)
{
    addObject(new Grape(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(600));
}
There is really no need for the 'updateWinDisplay' method. The win display is only shown at the end and its image only need be set once, at that time. I do like the fact that you set it up properly as if the displayed text was variable; but, that is not the case with this displayed text.
There are more replies on the next page.
1
2