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

2016/7/29

Using a time counter to spawn 'medals' in gameOver world

Exelious Exelious

2016/7/29

#
My game could be described as being similar to flappy bird. My timer keeps count throughout the gameplay, and when the player dies the world changes to the gameOver screen. I want to add some code that states if the player has been alive for > than 30 seconds create the actor for the BronzeMedal in the gameOver world. This would also happen for the silver and gold medals, where the player lives for 1 minute and 1 minute 30 seconds respectively. Im not sure which of my classes is necessary so i will add what i think is :) Code for my timer
public class TimerActor extends Actor
{
    private boolean running = false;
    private int millisElapsed = 0;
    private long lastTime = 0;
        
    /**
     * Act - do whatever the TimerActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {            
        long time = System.currentTimeMillis();
        
        if(lastTime != 0) {
            long diff = time - lastTime;
            millisElapsed += diff;
        }
        lastTime = time;
        
        updateImage();
    }    
            
     public TimerActor() {
        updateImage();
    }
    
    public void start() {
        millisElapsed = 0;
        lastTime = 0;
    }
    
    public void updateImage() {
        // Calculate minutes & seconds elapsed
        int millis = millisElapsed % 1000;
        int secs = (millisElapsed / 1000) % 60;
        int mins = millisElapsed / 60000;
        // Convert these into text
        String millisText = String.format("%03d", millis);
        String secsText = String.format("%02d", secs);
        String minsText = "" + mins;
        String text = minsText + ":" + secsText + "." + millisText;
        // Update the image
        GreenfootImage img = new GreenfootImage(text, 25, null, null);
        setImage(img);
    }
}
The actor that removes the player and sets the world to GameOver
public class Jellyfish extends Actor
{
    
    
    //Below is all related to animation.
    static final int CHG_RATE = 20; // adjust to suit
    int chgImgIn = 1; 
    int imgNum = 1;
    String[] images = { "Jellyfish.fw.png", "Jellyfish2.fw.png" };
    
    /**
     * Act - do whatever the Jellyfish wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        eat();
        animation();
        removeTouchingJellyfish();
        if (atWorldEdge() == true)  
        {  
           getWorld().removeObject(this);  
        }  
    }    
    
    public void animation()
    {
        chgImgIn--;
        if (chgImgIn == 0)
        {
          chgImgIn = CHG_RATE; // reset countdown
          imgNum = (imgNum + 1) % 2; // this will alternate {0, 1, 0, 1...}
          setImage(images[imgNum]);
        }   
    }
    
    /**
     * Decided how fast and in what direction the jellyfish move.
     */
    public void move()
    {
        move(-3);
       
    }
    
    public boolean atWorldEdge()  
    { 
     if (getX() == 0) {
            return true;
     } else {
            return false;
     }
    
    }
    
    public void eat()
    {
        TimerActor timeractor = (TimerActor) getWorld().getObjects(TimerActor.class).get(0); 
               
        Actor shark;
        shark = getOneObjectAtOffset(0, 0, Shark.class);
        if (shark != null)
        {
            World world;
            world = getWorld();
            world.removeObject(shark);
           
             getWorld().removeObject(timeractor);
            World newWorld = new GameOver();
            Greenfoot.setWorld(newWorld);  
            GreenfootImage timerImage = timeractor.getImage();
            int x = 357-timerImage.getWidth()/2;
            int y = 233-timerImage.getHeight()/2;
            newWorld.getBackground().drawImage(timerImage, x, y);
        }
    }
    
    public void removeTouchingJellyfish()
    {
        Actor jellyfish;
        jellyfish = getOneObjectAtOffset(0, 0, Jellyfish.class);
        World world;
        world = getWorld();
        world.removeObject(jellyfish);
    }
}
danpost danpost

2016/7/29

#
First, you need to be able to access the value of the 'millisElapsed' int field in the TimerActor class from the Jellyfish class. Add a "getter" method to the TimerActor class for this purpose. Then, from within the Jellyfish class, you can use the following to determine which medal to show:
int place = (int)(Math.max(1, 4-timerActor.getMillisElapsed()/30000));
World newWorld = new GameOver(place);
Finally, in the GameOver class, add the int parameter for 'place' in the constructor and the following code within it:
Actor medal = null;
switch (place)
{
    case 1; medal = new GoldMedal(); break;
    case 2: medal = new SilverMedal(); break;
    case 3: medal = new BronzeMedal(); break;
}
if (medal != null) addObject(medal, << x, y >>); // wherever
Exelious Exelious

2016/7/30

#
Hello Danpost, thanks for the help. But im having a few issues. By a Getter method am i safe to assure you mean something like this?:
 public int millisElapsed()
{
   return millisElapsed;
}
Also where do i place the code in my Jellyfish class which determines which medal to show?
danpost danpost

2016/7/30

#
Exelious wrote...
Hello Danpost, thanks for the help. But im having a few issues. By a Getter method am i safe to assure you mean something like this?: < Code Omitted >
The name of the method (you gave as 'millisElapsed()') should be 'getMillisElapsed()', which is more informative as to what the method does. It is also the name I used when calling the method in the first line of code I gave above.
Also where do i place the code in my Jellyfish class which determines which medal to show?
It should replace line 70 above.
Exelious Exelious

2016/7/30

#
Thank you for all the help, everything works except for two errors i am receiving which are both in the Jellyfish class. One when calling upon the getMillisElapsed code which says "Cannot find symbol getMillisElapsed()" and one in the next line for new GameOver(place); which says "constructor GameOver in class GameOver cannot be applied to given types: required: no arguments, found: int, reason: actual and formal argument lists differ in length."
int place = (int)(Math.max(1, 4-timerActor.getMillisElapsed()/30000));
World newWorld = new GameOver(place);
Here is my code again. (i changed the names of Medal to Coin just to match my file names)
public class Jellyfish extends Actor
{
    
    
    //Below is all related to animation.
    static final int CHG_RATE = 20; // adjust to suit
    int chgImgIn = 1; 
    int imgNum = 1;
    String[] images = { "Jellyfish.fw.png", "Jellyfish2.fw.png" };
    
    /**
     * Act - do whatever the Jellyfish wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        eat();
        animation();
        removeTouchingJellyfish();
        if (atWorldEdge() == true)  
        {  
           getWorld().removeObject(this);  
        }        
    }    
    
    public void animation()
    {
        chgImgIn--;
        if (chgImgIn == 0)
        {
          chgImgIn = CHG_RATE; // reset countdown
          imgNum = (imgNum + 1) % 2; // this will alternate {0, 1, 0, 1...}
          setImage(images[imgNum]);
        }   
    }
    
    /**
     * Decided how fast and in what direction the jellyfish move.
     */
    public void move()
    {
        move(-3);
       
    }
    
    public boolean atWorldEdge()  
    { 
     if (getX() == 0) {
            return true;
     } else {
            return false;
     }
    
    }
    
    public void eat()
    {   TimerActor timeractor = (TimerActor) getWorld().getObjects(TimerActor.class).get(0); 
               
        Actor shark;
        shark = getOneObjectAtOffset(0, 0, Shark.class);
        
        
        
        if (shark != null)
        {
            World world;
            world = getWorld();
            world.removeObject(shark);
           
            getWorld().removeObject(timeractor);
            int place = (int)(Math.max(1, 4-timeractor.getMillisElapsed()/30000));
            World newWorld = new GameOver(place);
            Greenfoot.setWorld(newWorld);  
            GreenfootImage timerImage = timeractor.getImage();
            int x = 357-timerImage.getWidth()/2;
            int y = 233-timerImage.getHeight()/2;
            newWorld.getBackground().drawImage(timerImage, x, y);
            
        }
    }
    
    public void removeTouchingJellyfish()
    {
        Actor jellyfish;
        jellyfish = getOneObjectAtOffset(0, 0, Jellyfish.class);
        World world;
        world = getWorld();
        world.removeObject(jellyfish);
    }
}
public class TimerActor extends Actor
{
    private boolean running = false;
    private int millisElapsed = 0;
    private long lastTime = 0;
        
    /**
     * Act - do whatever the TimerActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {            
        long time = System.currentTimeMillis();
        
        if(lastTime != 0) {
            long diff = time - lastTime;
            millisElapsed += diff;
        }
        lastTime = time;
        
        updateImage();
        GetMillisElapsed();
        
    }    
            
     public TimerActor() {
        updateImage();
    }
    
    public void start() {
        millisElapsed = 0;
        lastTime = 0;
    }
    
    public void updateImage() {
        // Calculate minutes & seconds elapsed
        int millis = millisElapsed % 1000;
        int secs = (millisElapsed / 1000) % 60;
        int mins = millisElapsed / 60000;
        // Convert these into text
        String millisText = String.format("%03d", millis);
        String secsText = String.format("%02d", secs);
        String minsText = "" + mins;
        String text = minsText + ":" + secsText + "." + millisText;
        // Update the image
        GreenfootImage img = new GreenfootImage(text, 25, null, null);
        setImage(img);
    }
    
    public int GetMillisElapsed()
{
   return millisElapsed;
}
    
}
danpost danpost

2016/7/31

#
Line 50 in the TimerActor class should be:
public int getMillisElapsed()
(names in java are case-sensitive). In your GameOver class, you should change:
public GameOver()
to
public GameOver(int place)
allowing the 'place' value to be brought into the constructor.
Exelious Exelious

2016/7/31

#
Works perfectly, thanks a bunch! :)
You need to login to post a reply.