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/18

#
davmac wrote...
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.
Tnx the health bar does update but nothing else stops. What do I do now.
Heet_Patel Heet_Patel

2018/5/18

#
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;
    int counter = 0;
    /**
     * 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);
                    if (counter > 0) {
                        counter = counter - 1;
                        if (counter == 0)  {
                            Greenfoot.stop();
                        }
                    }
                }
            }    
        }    

        else { 
            touchingShark_2 = false;
        }
    }     
} 

This is the new code
davmac davmac

2018/5/21

#
If you want everything else to stop but one actor (the health bar) to continue, you need to have all the other actors check some global state to determine whether they should act or not. For example, add a boolean variable into the world, read it from each actor - except the health bar - and act only if it is true. Set it to false when you want all the actors other than the health bar to stop.
You need to login to post a reply.
1
2
3