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

2017/11/1

how make the healthbar go down

1
2
rinariirin rinariirin

2017/11/1

#
my healthbar don't go down it keep show up the game over when touching the player
rinariirin rinariirin

2017/11/1

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    boolean touchinggator = false;
    private GreenfootImage myImage = getImage();
    private String[] imageNames = {"player_run1.png","player_run2.png","player_run3.png"};
    private int currentImage = 0;
    int xPos;
    int yPos;
    
    public void addedToWorld(World w)
    {
        xPos = getX();
        yPos = getY();
    }

    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-10);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+10);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-10, getY());
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+10, getY());
        }
        animateOnMove();
    }    

    private void animateOnMove()
    {   if(getX()!=xPos|| getY()!=yPos)
        {
            currentImage++;
            if(currentImage >= imageNames.length)
                currentImage=0;
            GreenfootImage newImage = new GreenfootImage(imageNames[currentImage]);
            if(getX()<xPos)
               newImage.mirrorHorizontally();
            setImage(newImage);
            myImage = getImage();
        }
        xPos=getX();
        yPos=getY();
        
         Actor spider;
        spider = getOneObjectAtOffset(0,0,spider.class);
        
         if (spider !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(spider);
            scoreboard scoreboard = (scoreboard) (getWorld().getObjects(scoreboard.class).get(0));
            scoreboard.score = scoreboard.score + 1;
        }
    }
    public void hitgator()
    {
       Actor gator = getOneIntersectingObject(gator.class );
       if (gator !=null)
       {
          World myworld = getWorld();
          Sandstone sandstone = (Sandstone)myworld;
          Healthbar healthbar = sandstone.getHealthbar();
          if (touchinggator ==false)
          {
              healthbar .losehealth();
              touchinggator = true;
              if (healthbar.health <=0)
              {
                game_over1 game_over = new game_over1();
                myworld.addObject(game_over,myworld.getWidth()/2, myworld.getHeight()/2);
                myworld.removeObject(this);
              }
            }      
         else {
          touchinggator = false;
        }
      }
    }
}
rinariirin rinariirin

2017/11/1

#
I need a slow down the time of the healthbar ?
danpost danpost

2017/11/1

#
rinariirin wrote...
I need a slow down the time of the healthbar ?
What are you wanting -- lose health on initial contact or lose health per time contacted?
rinariirin rinariirin

2017/11/2

#
what code should I add in
danpost danpost

2017/11/2

#
rinariirin wrote...
what code should I add in
Try moving line 98 up three lines (cut and paste at line 95).
rinariirin rinariirin

2017/11/2

#
it does nothing happen when I do it
danpost danpost

2017/11/2

#
rinariirin wrote...
it does nothing happen when I do it
Show your revised code so we know what you have done and have now.
rinariirin rinariirin

2017/11/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    boolean touchinggator = false;
    private GreenfootImage myImage = getImage();
    private String[] imageNames = {"player_run1.png","player_run2.png","player_run3.png"};
    private int currentImage = 0;
    int xPos;
    int yPos;
    
    public void addedToWorld(World w)
    {
        xPos = getX();
        yPos = getY();
    }

    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-10);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+10);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-10, getY());
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+10, getY());
        }
        animateOnMove();
    }    

    private void animateOnMove()
    {   if(getX()!=xPos|| getY()!=yPos)
        {
            currentImage++;
            if(currentImage >= imageNames.length)
                currentImage=0;
            GreenfootImage newImage = new GreenfootImage(imageNames[currentImage]);
            if(getX()<xPos)
               newImage.mirrorHorizontally();
            setImage(newImage);
            myImage = getImage();
        }
        xPos=getX();
        yPos=getY();
        
         Actor spider;
        spider = getOneObjectAtOffset(0,0,spider.class);
        
         if (spider !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(spider);
            scoreboard scoreboard = (scoreboard) (getWorld().getObjects(scoreboard.class).get(0));
            scoreboard.score = scoreboard.score + 1;
        }
    }
    public void hitgator()
    {
       Actor gator = getOneIntersectingObject(gator.class );
       if (gator !=null)
       {
          World myworld = getWorld();
          Sandstone sandstone = (Sandstone)myworld;
          Healthbar healthbar = sandstone.getHealthbar();
          if (touchinggator ==false)
          {
              healthbar .losehealth();
              touchinggator = true;
              if (healthbar.health <=0)
              {
                game_over1 game_over = new game_over1();
                myworld.addObject(game_over,myworld.getWidth()/2, myworld.getHeight()/2);
                myworld.removeObject(this);
              }
            }      
       }
       else {
          touchinggator = false;
       }
    }
}
rinariirin rinariirin

2017/11/3

#
or I should put on a timer
danpost danpost

2017/11/3

#
rinariirin wrote...
or I should put on a timer
You should call the 'hitgator' method from the 'act' method.
rinariirin rinariirin

2017/11/3

#
what should I do then
danpost danpost

2017/11/3

#
rinariirin wrote...
what should I do then
Did you do this?
danpost wrote...
call the 'hitgator' method from the 'act' method.
If so and your health bar still does not move, post the code to your World subclass.
rinariirin rinariirin

2017/11/6

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AnimWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sandstone extends World
{
    game_over1 game_over1 = new game_over1();
    Healthbar thisHealthbar; // = new Healthbar ();
    /**
     * Constructor for objects of class AnimWorld.
     * 
     */
    public Sandstone()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }
    

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Player player = new Player();
        addObject(player, 284, 175);
        
        spider spider = new spider();
        addObject(spider,473,234);
        spider spider2 = new spider();
        addObject(spider2,383,99);
        spider spider3 = new spider();
        addObject(spider3,250,363);
        spider spider4 = new spider();
        addObject(spider4,103,102);
        scoreboard scoreboard = new scoreboard();
        addObject(scoreboard,64,42);
        gator gator = new gator();
        addObject(gator,45,260);
        
        thisHealthbar = new Healthbar();
        addObject(thisHealthbar,400,20);
        
    }
    public game_over1 getGameOver1()
    {
        return game_over1;
    }
    public Healthbar getHealthbar()
    {
        return thisHealthbar;
    }
    public void hitgator()
    {
       Actor gator = getOneIntersectingObject(gator.class );
       if (gator !=null)
       {
          World myworld = getWorld();
          Sandstone sandstone = (Sandstone)myworld;
          Healthbar healthbar = sandstone.getHealthbar();
          if (touchinggator ==false)
          {
              healthbar .losehealth();
              touchinggator = true;
              if (healthbar.health <=0)
              {
                game_over1 game_over = new game_over1();
                myworld.addObject(game_over,myworld.getWidth()/2, myworld.getHeight()/2);
                myworld.removeObject(this);
              }
            }      
       }
       else {
          touchinggator = false;
       }
    }
}
rinariirin rinariirin

2017/11/6

#
help please
There are more replies on the next page.
1
2