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

2018/5/8

Timer

1
2
3
davmac davmac

2018/5/14

#
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.
Even if you don't mean it to, this sounds a lot like: I can't be bothered trying to think through this for myself. I haven't tried anything. I want someone to write the code for me. Programming involves understanding certain key concepts, not just writing code. If you're having trouble with the concepts then you should ask about the concepts, not ask for someone to just give you the code. The first way you learn something and you make progress; the second way, you'll just be back again asking for more code the very next time you want to do anything.
Heet_Patel Heet_Patel

2018/5/14

#
Okay lets rephrase that then.
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);
                    Greenfoot.delay(5);
                    Greenfoot.stop();
                }    
            }    

        } else { 
            touchingShark_2 = false;
        }
    }     
} 

I used this code after a while and this stilldoesn't work. I want to delay the greenfoot.stop method so i added a delay method and this still won't work.
davmac davmac

2018/5/14

#
The Greenfoot.stop() method stops the entire scenario. The Greenfoot.delay() method delays the entire scenario. To do some action after something but still keep everything running, one way is to have a counter variable in an actor or in the world which you decrease (or increase) every time act() is called until it reaches 0 (or some value), and when it has reached the target value you perform the action. Roughly speaking:
if (counter > 0) {
    counter = counter - 1;
    if (counter == 0) {
        Greenfoot.stop();
    }
}
In the example above I've used "counter > 0" as a condition to indicate that the countdown is running, so to start it you just set counter to some value that's greater than 0.
Heet_Patel Heet_Patel

2018/5/15

#
I don't understand. I dont have counter, I have a health bar. Is there something that delays only the greenfoot.stop() method
davmac davmac

2018/5/15

#
Is there something that delays only the greenfoot.stop() method
You cannot just call another method before calling Greenfoot.stop() in order to delay the stop. You need to arrange for Greenfoot.stop() to be called after a delay, without holding up everything else. The code I gave you does that.
I dont have counter, I have a health bar.
You would need to add the counter variable in the appropriate class (eg to Fishes). Its type should be int - a whole number.
Heet_Patel Heet_Patel

2018/5/15

#
?? What do you men. So r u trying to say that I should add the previous code you gave me instead of the Greenfoot.delay(); method and it should work.
Heet_Patel Heet_Patel

2018/5/15

#
And what do you mean by the counter variable
davmac davmac

2018/5/15

#
Heet_Patel wrote...
And what do you mean by the counter variable
I mean the counter variable I referred to earlier, that you would need to add to your code:
davmac wrote...
To do some action after something but still keep everything running, one way is to have a counter variable in an actor or in the world which you decrease (or increase) every time act() is called
Heet_Patel wrote...
?? What do you men. So r u trying to say that I should add the previous code you gave me instead of the Greenfoot.delay(); method and it should work.
Yes - that code, in an appropriate place (as I said before - it needs to run every time act() is called) - and a declaration for the counter variable, and then instead of calling Greenfoot.stop() directly you would set the counter to your chosen value. But don't just try to copy the code - look at it, try to understand what it does, and how it achieves what you need. If you have any questions about that, ask about that. Just asking for code every time isn't going to get you far.
Heet_Patel Heet_Patel

2018/5/16

#
I mean how do you create a counter variable. I want the game to stop once the actor has lost all the health I want the game to stop. But I want the code to make sure it happens about a few seconds after so the health bar can update and then the timer can stop.
danpost danpost

2018/5/16

#
Heet_Patel wrote...
I want the code to make sure it happens about a few seconds after so the health bar can update and then the timer can stop.
If you have your health bar update when its value is changed (instead of "after"), then you can just stop the scenario immediately.
davmac davmac

2018/5/17

#
I mean how do you create a counter variable.
The same way you create any variable - you write a declaration for it (a type and name, and optionally an initialising value) in your class. You already have a variable in your class:
    boolean touchingShark_2 = false;
Its type is boolean and its name is touchingShark_2. As I said above, the counter variable should be of type int and while you could name it counter you can also use any other name you like. To be explicit:
    int counter = 0;
Heet_Patel Heet_Patel

2018/5/17

#
boolean touchingShark_2 = false;
So do I add this before the act method and after that write
int counter = 0;
or do I this code in the act method?
Heet_Patel Heet_Patel

2018/5/17

#
danpost wrote...
Heet_Patel wrote...
I want the code to make sure it happens about a few seconds after so the health bar can update and then the timer can stop.
If you have your health bar update when its value is changed (instead of "after"), then you can just stop the scenario immediately.
I meant that the greenfoot.stop() method is delayed by a few seconds so that the health bar can update
danpost danpost

2018/5/17

#
Heet_Patel wrote...
I meant that the greenfoot.stop() method is delayed by a few seconds so that the health bar can update
I understood that. I was just giving an alternate way to accomplish what you were trying to do (without having to use a delay).
davmac davmac

2018/5/18

#
Heet_Patel wrote...
boolean touchingShark_2 = false;
So do I add this before the act method and
It's already in your code.
... after that write
int counter = 0;
Yes.
or do I this code in the act method?
No. If you did, it would be a local variable and would not retain its value between act cycles.
There are more replies on the next page.
1
2
3