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

2018/5/8

Timer

1
2
3
Heet_Patel Heet_Patel

2018/5/8

#
Why does it say 'invalid method declaration; return type required' in line 17.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level_quatre here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level_quatre extends World
{
    HealthBar healthbar = new HealthBar();
    private int time;
    /**
     * Constructor for objects of class Level_quatre.
     * 
     */
    public Play()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1);
        time = 6000;
        point = 0;
        prepare();
    }
    
    private void act()
    {
        time--;
        showTime();
    }
    
    private void showTime()
    {
       showText("Time: " + time/100, 50, 50); 
    }    

    public HealthBar getHealthBar()
    {
        return healthbar;
    }    
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(healthbar, 500, 40);
        SharkSpawner shark_spawner = new SharkSpawner();
        addObject(shark_spawner,745,479);
        shark_spawner.setLocation(927,629);
        Fishes fishes = new Fishes();
        addObject(fishes,810,159);
        house house2 = new house();
        addObject(house2,68,67);
        house2.setLocation(26,27);
    }
}
Heet_Patel Heet_Patel

2018/5/8

#
The public Play() method doesn't seem to work. Pls could some one help me.
danpost danpost

2018/5/8

#
Heet_Patel wrote...
The public Play() method doesn't seem to work. Pls could some one help me.
A constructor must be named the same as the class. The compiler does not recognize Play as a constructor because it is not named appropriately. It therefore presumes it to be a method, but missing a return type -- causing the compilation error.
Heet_Patel Heet_Patel

2018/5/8

#
What should I add to my code. Like am i suppose to have another piece of code. If so could you pls tel me what to add
Heet_Patel Heet_Patel

2018/5/8

#
change of plans. New code
/** This class creates a countdown timer object.
 * It displays like a digital timer clock.
 * The value to set the timer is given in seconds; however, is converted to approximate act cycles within the class.
 * All timers created from this class will start automatically unless using the 'Timer(int, boolean)' constructor with boolean set to 'false'.
 */
import greenfoot.*;
 
public class Timer extends Actor
{
    private int count; // the counter field
    private int initialCount; // the initial time given before event occurs
    private boolean running;
 
    public Timer()
    {
        this(0, true);
    }
 
    public Timer(int timeBeforeEvent)
    {
        this(timeBeforeEvent, true);
    }
 
    public Timer(int timeBeforeEvent, boolean getsStarted) // int value given in seconds
    {
        setTimer(timeBeforeEvent);
        updateImage();
        running = getsStarted;
    }
 
    public void setTimer(int timeBeforeEvent)
    {
        initialCount = 60 * timeBeforeEvent;
        count = -initialCount;
    }
 
    private void updateImage()
    {
        String prefix = "T - ";
        if (count >= 0) prefix = "T + ";
        int time = count * (int)Math.signum(count);
        time = time / 60;
        int secs = time % 60;
        time = (time - secs) / 60;
        int mins = time % 60;
        int hrs = (time - mins) / 24;
        String h = "00"+hrs;
        while (h.length() > 2) h = h.substring(1);
        String m = "00"+mins;
        while (m.length() > 2) m = m.substring(1);
        String s = "00" + secs;
        while (s.length() > 2) s = s.substring(1);
        String text = prefix + h + "h : " + m + "m : " + s + "s";
        GreenfootImage textImage = new GreenfootImage(text, 20, Color.WHITE, new Color(0, 0, 0, 0));
        GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
        image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
        setImage(image);
    }
 
    public void act()
    {
        if (running)
        {
            count++;
            if ((count + initialCount) % 60 == 0) updateImage();
        }
    }
 
    public int getTime()
    {
        return count / 60;
    }
 
    public void start()
    {
        running = true;
    }
 
    public void stop()
    {
        running = false;
    }
}
How do i make it that when i die the counter stops. I have tried putting Greenfoot.stop(); but this doesn't get rid of the entire health bar but it stops everything. This is where the Greenfoot.stop();
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fishes here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fishes extends chaar
{
    boolean touchingShark_2 = false;
    /**
     * Act - do whatever the Fishes wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(4);
        if (Greenfoot.isKeyDown("Left"))
        {
            turn(-3);
        }
        if (Greenfoot.isKeyDown("Right"))
        {
            turn(3);
        }
        Actor Shark_2 = getOneIntersectingObject(Shark_2.class);
        if(Shark_2 != null)
        {
            World myWorld = getWorld();
            Level_quatre level_4 = (Level_quatre)myWorld;
            HealthBar healthbar = level_4.getHealthBar();
            if(touchingShark_2 == false)
            {
                healthbar.loseHealth();
                touchingShark_2 = true;
                if(healthbar.health <=0)
                {
                    Gameover_1 gameover = new Gameover_1();
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                    myWorld.removeObject(this);
                }    
            }    
            
        } else { 
            touchingShark_2 = false;
        }
    }     
} 

If you need the world code here it is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level_quatre here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level_quatre extends World
{
    HealthBar healthbar = new HealthBar();
    /**
     * Constructor for objects of class Level_quatre.
     * 
     */
    public Level_quatre()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1);
        prepare();
    }
    
    public HealthBar getHealthBar()
    {
        return healthbar;
    }    
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(healthbar, 500, 40);
        SharkSpawner shark_spawner = new SharkSpawner();
        addObject(shark_spawner,745,479);
        shark_spawner.setLocation(927,629);
        Fishes fishes = new Fishes();
        addObject(fishes,810,159);
        house house2 = new house();
        addObject(house2,68,67);
        house2.setLocation(26,27);
        Timer timer = new Timer();
        addObject(timer,352,290);
        timer.setLocation(187,40);
    }
}
Heet_Patel Heet_Patel

2018/5/8

#
Is it possible to delay the greenfoot method for stopping for one second.
Heet_Patel Heet_Patel

2018/5/8

#
This is a youtube video about timers but contains no info about the kind of timers i need
danpost danpost

2018/5/9

#
Heet_Patel wrote...
How do i make it that when i die the counter stops. I have tried putting Greenfoot.stop(); but this doesn't get rid of the entire health bar but it stops everything.
The Timer class contains a stop method -- use it (when you die).
Heet_Patel Heet_Patel

2018/5/9

#
How exactly does that work. Like it says public void stop and then running = false; so how do i change it into when i die it stops. whats the code
danpost danpost

2018/5/9

#
Heet_Patel wrote...
How exactly does that work. Like it says public void stop and then running = false; so how do i change it into when i die it stops. whats the code
How do you think it works? It is always the same when you tell an object to do something: (pseudo-code) object.doSomething(); Sometimes the object is missing, but then it is implicitly given: (pseudo-code) doSomething(); which is equivalent to: (pseudo-code) this.doSomething(); Just like a command -- subject followed by verb.
Heet_Patel Heet_Patel

2018/5/10

#
No but how do i get it to stop when gameover pops up on the screen
danpost danpost

2018/5/10

#
Heet_Patel wrote...
No but how do i get it to stop when gameover pops up on the screen
Where do you think you would put the code and what code do you think you would use?
Heet_Patel Heet_Patel

2018/5/11

#
That doesn't help Danpost. I actually need help. The riddles won't help me. I am a bit dumb so only getting straight to the point will help me.
danpost danpost

2018/5/11

#
Heet_Patel wrote...
The riddles won't help me.
I think it quite straight-forward:
Where do you think you would put the code and what code do you think you would use?
Just asking for thoughts -- not on solving a riddle (coding is not cryptic, just rigid).
Heet_Patel Heet_Patel

2018/5/14

#
I am not quite sure with what is what. Kind of bad at this kind of stuff.
There are more replies on the next page.
1
2
3