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

2013/9/14

need help

manish123 manish123

2013/9/14

#
hello, i am working on a timer class for my game where after the timer hits zero, the game ends and i display a text however it is not working can someone point out what i am doing wrong? : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class timer here. * * @author (your name) * @version (a version number or a date) */ public class timer extends Actor { public int timer_; /** * Act - do whatever the timer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { countdown(); } public void countdown() { for(int timer_ = 5; timer_ >= 5; timer_-- ) { if(timer_ == 0) { Greenfoot.stop(); GreenfootImage img = new GreenfootImage(4,5); img.drawString("You are out of time",4,5); img.setColor(Color.ORANGE); } } } }
Gevater_Tod4711 Gevater_Tod4711

2013/9/14

#
What exactly do you want this code to do? Until now you just start a for loop and create an image that is never shown. If you want to create a countdown timer you first should never use loops. The loop will count down in only one act and you game will end before it has begon. Then you should create the image you want using another GreenfootImage constructor and also print the image onto the world or somewhere else so that you can see the image. Try it like this:
public int timer_ = 5;//or any other starting value;

public void act() {
    countdown();
}

public void countdown() { 
    timer_--;
    if (timer <= 0) {
        GreenfootImage img = new GreenfootImage("You are out of time", 30, Color.ORANGE, null);//creates a greenfoot image with orange letters on transparent ground with a font size of 30;
        getWorld().getBackground().drawImage(img, (getWorld().getWidth() - img.getWidth())/2, (getWorld().getHeight() - img.getHeight())/2); //print the text image you created onto the middle of the world;
    }
}
You need to login to post a reply.