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

2013/6/15

Need help with live counter?

dangamecreator dangamecreator

2013/6/15

#
I am making a platform based game, but I don't know how to do a live counter. Can anyone help me?
What kind of counter, one that keeps track of time, or keeps track of frames?
dangamecreator dangamecreator

2013/6/15

#
One that when it reaches zero it's game over, (when lives reach zero)
Alright, (sorry for the late responses) The way I would create this is an int that has a specific number when first initialized. This number would be the number of frames to game over (if you do some simple math and maybe use the FPS class on the site you could figure out how much time that would be). Then, every time act() is called, subtract it by one. Then when it equals zero, create a game over. Here's how it would look in code:
//instance variables
private int timer = 6000; //running at 60 fps that should be 100 seconds
...
//constructor, other methods
...
public void act()
{
    if (timer > 0)
        timer--;    //Counts down
    else
        gameOver();  //calls a game over method when it's 0 or less
...
}
...
danpost danpost

2013/6/15

#
@FlyingRabidUnicornPig, I believe that dangamecreator is looking for a Counter, not a Timer; more specifically, a Counter that keeps track of the number of lives remaining for the main character of the scenario. A counter is a counter, whether it counts frames, seconds, lives, or health. Greenfoot contains a Counter class you can import into your scenarios and you can create multiple counters of any type from the same class. When you do make multiple counters, you need a way to be able to tell which one is which; this is usually done by either adding instance Counter fields (or an array of Counter objects) to the world class or by giving each one an identification (a field in the Counter class that contains a unique value, used to determine what that particular counter is used for). Having a class that can perform generally is much better than creating multiple classes, one for each object you want to create individually and specifically. The method to work the Counter are in the Counter class, but the commands to run those method go in your other classes. Any checking of the value of the counters should also be done in those other classes. In other words, if you need to check to see if the value is zero (or less) to stop the game, it should be done immediately after calling the method that changes the counters value:
// using  this instance field in the world class (aka WorldName)
private Counter livesCounter = new Counter("Lives", 3);
// and this method in the world class
public Counter getLivesCounter()
{
    return livesCounter;
}
// in your player class, when life lost
Counter lives = ((WorldName)getWorld()).getLivesCounter();
lives.add(-1);
if (lives.getValue() == 0) gameOver();
dangamecreator dangamecreator

2013/6/15

#
Thanks danpost :), and FlyingRabidUnicorn for helping :D
derp2000 derp2000

2013/6/16

#
// using this instance field in the world class (aka WorldName) private Counter livesCounter = new Counter("Lives", 3); // and this method in the world class public Counter getLivesCounter() { return livesCounter; } // in your player class, when life lost Counter lives = ((WorldName)getWorld()).getLivesCounter(); lives.add(-1); if (lives.getValue() == 0) gameOver(); } I've put this in my healthcounter class but it says that at the lives.add(-1) an indentifier is expected? what does it mean
derp2000 derp2000

2013/6/16

#
i'mean i have putted the parts in the right classes but still get the problem with lives.add(-1) <indentifier> expected
danpost danpost

2013/6/16

#
@derp2000, none of the code you just re-posted goes in the healthcounter class. The field and the 'get' method go in your sub-class of World; and the rest go in some other class (not the healthcounter class - usually the class that creates your main actor or player). The error message you are getting often indicates that your bracketing is not correct (I did not supply the last closing curly bracket that you show at the end of the re-post ).
derp2000 derp2000

2013/6/16

#
I mean I didn't put it in my health counter class... BuT I'have putted it al where it belongs (or what is standing as notifications) But some how it didn't work???? // using this instance field in the world class (aka WorldName) private Counter livesCounter = new Counter("Lives", 3); // and this method in the world class public Counter getLivesCounter() { return livesCounter; } // in your player class, when life lost Counter lives = ((WorldName)getWorld()).getLivesCounter(); lives.add(-1); if (lives.getValue() == 0) gameOver();
derp2000 derp2000

2013/6/16

#
what could be the problem?
danpost danpost

2013/6/16

#
Please show the code for your player class (use the 'code' tag below the 'Post a reply' box and insert your program code in the pop-up window provided).
derp2000 derp2000

2013/6/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //Imports Color Class from the java library.
/**
 * Write a description of class Poppetje here.
 * 
 * @author Harmen 
 * @version (a version number or a date)
 */
public class Poppetje extends Actor
{
    /**
     * Act - do whatever the Poppetje wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
            }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
            }            
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-5, getY());
            }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+5, getY());
            }
    }    
    
     public void wonGame()
    {
        if ( getY() < 15 )
        {
            getWorld().removeObjects(getWorld().getObjects(Car1.class));
            getWorld().removeObjects(getWorld().getObjects(Car2.class));
            getWorld().removeObjects(getWorld().getObjects(Leraar.class));
            getWorld().removeObjects(getWorld().getObjects(Leraar2.class));
            GreenfootImage bg = getWorld().getBackground();
            bg.setColor(Color.blue);
            bg.drawString("u heeft gewonnen!", 300, 400);
            Greenfoot.stop();
        }
  
    }
    
     public void eat()
    {
        Actor Car1;
        Car1 = getOneObjectAtOffset(0, 0,Car1.class);
        if (Car1 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Car1);
        }
    }
   
    if(getX() == 17 && getY()==15)        
    
    {        
        ((background)getWorld()).board();       
        Greenfoot.stop();        
    }        
    else if(getX() == 18 && getY()==15)        
    {        
       ((background)getWorld()).board();       
       Greenfoot.stop();        
    }     


}
danpost danpost

2013/6/16

#
Please only use one discussion thread for each issue. I answered this problem in the other discussion (as well as above, indirectly). Also, you should not continue others discussion with your own problems (start a new discussion and, if needed, refer to the other discussion).
You need to login to post a reply.