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

2014/6/27

any one can help me make my hp bar work please

nic555ex3 nic555ex3

2014/6/27

#
hi and one can help me make my hp bar work please i need it for my school project urgent need it work by next monday any one can help me please email me at nic555ex3@gmail.com i will sent u my files
lordhershey lordhershey

2014/6/27

#
Have you started the project yet? I could do it, but it will cost you, YOUR SENSE OF PRIDE OF DOING IT YOURSELF! (cue lightning) is all that is needed is a health bar? What is it about you current set of files that is not working?
IeuanMcCrushems IeuanMcCrushems

2014/6/27

#
I like burgers.
nic555ex3 nic555ex3

2014/6/27

#
i aready started it and i just need the hp bar working
danpost danpost

2014/6/27

#
It would be better if you post, using the 'code' tag below the 'Post a reply' box, your code here. Required would be your hp bar class, the parts in your world class that deal with creating it and adding it into the world (and any other code in the world class that deals with the hp bar) and the relevant code in the class(es) where the value of the hp bar is changed.
nic555ex3 nic555ex3

2014/6/28

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

/**
 * Write a description of class Mushroom here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tojan extends BadGuys
{
    boolean EatPlayer = false; 
    int frame = 0;
    private int animationCount;
    private int vSpeed = 0;
    private int acceleration = 1;
    private int speed = 2;
    
    private int spriteHeight = getImage().getHeight();
    private int spriteWidth = getImage().getWidth();
    private int lookForGroundDistance = (int)spriteHeight/2;
    private int lookForEdge = (int)spriteWidth/2;
    
    GreenfootImage Mush1l = new GreenfootImage("Mush1l.png");
    GreenfootImage Mush2l = new GreenfootImage("Mush2l.png");
    GreenfootImage Mush3l = new GreenfootImage("Mush3l.png");
    GreenfootImage Mush4l = new GreenfootImage("Mush4l.png");
    GreenfootImage Mush5l = new GreenfootImage("Mush5l.png");
    
    GreenfootImage Mush1r = new GreenfootImage("Mush1r.png");
    GreenfootImage Mush2r = new GreenfootImage("Mush2r.png");
    GreenfootImage Mush3r = new GreenfootImage("Mush3r.png");
    GreenfootImage Mush4r = new GreenfootImage("Mush4r.png");
    GreenfootImage Mush5r = new GreenfootImage("Mush5r.png");
    
    
   
    
    /**
     * Act - do whatever the Mush wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      checkFall();
      move();
      checkRightWalls();
      checkLeftWalls();
      checkEdge();
      animationCount ++;
      tryToEatPlayer();
        
        if(speed<0)
        {
            if(animationCount % 4 == 0) // Slows down animation
            animateLeft();
        }
        else
        {
            if(animationCount % 4 == 0) // Slows down animation
            animateRight();
        }
    }   
      public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(Mush1l);
        }
        if(frame == 1)
        {
            setImage(Mush2l);
        }
        if(frame == 2)
        {
            setImage(Mush3l);
        }
        if(frame == 3)
        {
            setImage(Mush4l);
        }
        if(frame == 4)
        {
            setImage(Mush5l);
            frame = 0;
        }
        frame++;
    }
    
        public void animateRight()
    {
        if(frame == 0)
        {
            setImage(Mush1r);
        }
        if(frame == 1)
        {
            setImage(Mush2r);
        }
        if(frame == 2)
        {
            setImage(Mush3r);
        }
        if(frame == 3)
        {
            setImage(Mush4r);
        }
         if(frame == 4)
        {
            setImage(Mush5r);
            frame = 0;
        } 
        frame++;
    }
     
    public void checkEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
        {
            speed *= -1;
        }
    }
    
    public void move()
    {
        Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class);
        
        if(ground == null)
        {
            speed *= -1; // Reverse direction
            lookForEdge *= -1; // Looks for a negative number
        }
        else
        {
            move(speed);
        }
    }
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if(ground == null)
        {
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
      public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);
        Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(rightWall == null)
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }
    
    public void stopByRightWall(Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
        setLocation(newX - 5, getY());
        speed *= -1;
    }
    
        public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/-2);
        Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(leftWall == null)
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }
    
    public void stopByLeftWall(Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
        setLocation(newX + 5, getY());
        speed *= -1;
    }
    
     /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(1, 1, clss);
        return actor != null;        
    }
    
    /**
     * Try to grab an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(1, 1, clss);
        if(actor != null) {
         World myWorld = getWorld();
            Level01 level01 = (Level01)myWorld;
            HealthBar healthbar = level01.getHealthBar();
            if(EatPlayer == false)
            {
                HealthBar.loseHealth();
                eatPlayer = true;
                if(healthbar.health <=0)
            getWorld().removeObject(actor);
        }
    }  
    }
    public void tryToEatPlayer()
    {
        if (canSee(Player.class))
        {
            
        
            eat(Player);
            Greenfoot.setWorld(new gameover());
        }
       }
}
nic555ex3 nic555ex3

2014/6/28

#
that one the class for tojan the badguy that my player hv to kill
nic555ex3 nic555ex3

2014/6/28

#
this is my healthbar class
mport greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class HealthBar extends Actor  
{
    int health = 4;  
    int healthBarWidth = 80;  
    int healthBarHeight = 15;  
    int pixelsPerHealthPoint =(int)healthBarWidth/health;
   
     /**
      * Act - do whatever the HealthBar wants to do. This method is called whenever
      * the 'Act' or 'Run' button gets pressed in the environment.
      */
     public HealthBar()
     {
       update();  
     }    
 
   public void act()
   {
       update();
   }
   public void update()
   {
       setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
       GreenfootImage myImage = getImage();
       myImage.setColor(Color.WHITE);
       myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
       myImage.setColor(Color.RED);
       myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);

    }
    
    public void loseHealth()
    {
        health--;
    }
}
danpost danpost

2014/6/28

#
I first looked at your HealthBar class. You do not need to update its image every act cycle. After the initial creation of the HealthBar object, when you call 'update' to set the initial image of the bar, the only time you need to update the image is when the value of the bar changes (in the 'loseHealth' method after the value is changed). Add the 'update' call there and remove the act method from the class. Then, in your Tojan class, I notice that line 252 is trying to call the 'loseHealth' method on the class, 'HealthBar', and not on an object of the class, 'healthBar' (declared on line 249). Also, on line 253, you refer to a field, 'eatPlayer', which is declared as 'EatPlayer' and the argument for 'eat' on line 265 is missing the '.class' as in that of the 'canSee' of line 261. You should compile and test more often to remove problems like these as you write your code.
You need to login to post a reply.