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

2014/11/28

How to apply HealthBar

1
2
3
4
5
6
cloud41910 cloud41910

2014/12/1

#
also if i keep contact with the enemy my actor is removed even though it has done a little bit damage to my actor could you check if there's something with my actor and enemy actor here's the code
cloud41910 cloud41910

2014/12/1

#
Here's my maincharacter
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class K here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class K extends ShooterActors
{
    /**
     * Act - do whatever the K wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int vspeed = 0;
    private int acceleration = 2;
    private int speed =5;
    private int fspeed = 0;
    private boolean onHeartless = false;
    boolean CanJump;
    KiBlast kiblast = new KiBlast();
    private int points = 0;
    private Bar healthBar;
    public void act()
    {
        movement();
        if(isOnGround()){
            CanJump = true;
        } else{
            CanJump = false;
            fall();
        }
              if (isTouching(Heartless.class)) 
          {  
         if (!onHeartless)  
         {  
         healthBar.subtract(10);  
         onHeartless = true;  
         }  
          }  
         else onHeartless = false;
        fire();
        shoot();
        healthBar.setLocation(getX(), getY()-30);  
        hit();
    }
    public K()  
    {  
        healthBar = new Bar("", "", 100, 100);  
        healthBar.setShowTextualUnits(false);  
        healthBar.setBarWidth(30);  
        healthBar.setBarHeight(3);  
        healthBar.setMaximumValue(70);
        healthBar.setValue(70);
        healthBar.setBreakValue(10); 
    }  
    public boolean isOnGround()
    {
        Clone Window = (Clone) getWorld();
        if(getY() + (getImage().getHeight() / 2) >= (Window.getHeight() -1)) {
            return true;
        }
        return false;
    }
    public void jump()
    {
        fspeed = -10;
        fall();
    }
    public void fall()
    {
        setLocation ( getX(), getY() + fspeed);
        fspeed = fspeed + acceleration;
    }
    public void movement()
    {      
    if(Greenfoot.isKeyDown("left"))
    {
        
        MoveLeft();
    }    
    if(Greenfoot.isKeyDown("right"))
    {
        
        MoveRight();
    }
    if(Greenfoot.isKeyDown("up"))
    {
     jump();
    }
    }
    public void MoveLeft()
    {
        setLocation (getX() - speed, getY());
    }
    public void MoveRight()
    {   
        setLocation (getX() + speed, getY());
    }
    public void fire()
    {
    if(Greenfoot.isKeyDown("x"))
    {
        World world = getWorld();
        world.addObject(kiblast, 0, 0);
        kiblast.setLocation(getX(),getY());
    }
    }
    public void shoot()
    {
         
        {  
         if ("c".equals(Greenfoot.getKey()))  
          
        getWorld().addObject(new KiBlast(), getX(), getY());  
          points = points+5;
       }  
    }
     protected void addedToWorld(World world)  
    {  
        world.addObject(healthBar, getX()-18, getY()-373);  
    }  
        public void hit()  
    {  
        Actor heartlessblast = getOneIntersectingObject(HeartlessBlast.class);  
        if(heartlessblast != null)  
        {  
            healthBar.subtract(10);  
            getWorld().removeObject(heartlessblast);
        }  
        Actor heartless = getOneIntersectingObject(Heartless.class);
        {
            if(heartless != null)
            healthBar.subtract(10);
        }
        if(healthBar.getValue() == 0)  
        {    
          World world = getWorld();
        NewGameOver newgameover = new NewGameOver();
        world.addObject(newgameover, world.getWidth()/2,world.getHeight()/2);
        getWorld().removeObject(healthBar);  
        getWorld().removeObject(this);
        }  
    } 
}
cloud41910 cloud41910

2014/12/1

#
Here's the enemy code
cloud41910 cloud41910

2014/12/1

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

/**
 * Write a description of class Heartless here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    /**
     * Act - do whatever the Heartless wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public class Heartless extends Actor
{
    HeartlessBlast heartlessblast = new HeartlessBlast();
    private int shotDelay = 0;
    private int JumpChance = 0;
    private int vspeed = 0;
    private int magicnumber = 3;
    private int acceleration = 0;
    boolean isOnGround;
    public void act() 
    {
        fap();
        shoot();
    }
    public void annihilate()
    {
        Actor K = getOneObjectAtOffset(0,0,K.class);
     getWorld().addObject(heartlessblast, getX(), getY());
    
    if(K != null){
       World world = getWorld();
        NewGameOver newgameover = new NewGameOver();
        world.addObject(newgameover, world.getWidth()/2,world.getHeight()/2);
        world.removeObject(K);
        
    }
    }
      public void fap()
    {
         getWorld().addObject(heartlessblast, getX(), getY());  
         annihilate();
    }
    
    public void shoot()
    {
        if(shotDelay >= 100)
        {
            getWorld().addObject(new HeartlessBlast(), getX(), getY());
            shotDelay = 0;
        }
        shotDelay++;
    }
       public void jump()
    {
        vspeed = -10;
        fall();
    }
    public void fall()
    {
        setLocation ( getX(), getY() + vspeed);
        vspeed = vspeed + acceleration;
    }
  
}
danpost danpost

2014/12/1

#
Well, you have what appears to be the corrected code in the act method. But, you also have something twice in the hit method. That would cause problems.
cloud41910 cloud41910

2014/12/1

#
The other one is if a bullet hits me should i make a seperate method for it?
danpost danpost

2014/12/1

#
You should not need a separate method for the bullet; but, you should not have Heartless collision checking twice in you class (once in the act method and again in the hit method). I suggest removing the Heartless check in the act method and replacing the check in the hit method with the code removed.
cloud41910 cloud41910

2014/12/1

#
it works but when my actor touches the enemy it's been removed even tho the hp isn't empty yet
danpost danpost

2014/12/1

#
It looks like you have code in your Heartless class where if it is touching the player when it fires a Heartlessblast, annihilate causes game over.
cloud41910 cloud41910

2014/12/2

#
can i also have the code for the random jumping i don't quite get it
danpost danpost

2014/12/2

#
Where is the class code? Show what you have attempted? Explain what it is doing as opposed to what it should be doing. To start off, you will need to assign some values (other than zero) to the acceleration and JumpChance fields.
cloud41910 cloud41910

2014/12/2

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

/**
 * Write a description of class Heartless here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    /**
     * Act - do whatever the Heartless wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public class Heartless extends Actor
{
    HeartlessBlast heartlessblast = new HeartlessBlast();
    private int shotDelay = 0;
    private int JumpChance = 0;
    private int vspeed = 0;
    private int acceleration = 0;
    boolean CanJump;
    boolean OnGround;
    public void act() 
    {
           if(isOnGround()){
            CanJump = true;
        } else{
            CanJump = false;
            fall();
        }
        fap();
        shoot();
        randomjump();
    }
    public void annihilate()
    {
        Actor K = getOneObjectAtOffset(0,0,K.class);
     getWorld().addObject(heartlessblast, getX(), getY());
    
    if(K != null){
       World world = getWorld();
        NewGameOver newgameover = new NewGameOver();
        world.addObject(newgameover, world.getWidth()/2,world.getHeight()/2);
        world.removeObject(K);
        
    }
    
    }
      public void fap()
    {
         getWorld().addObject(heartlessblast, getX(), getY());  
    }
    
    public void shoot()
    {
        if(shotDelay >= 100)
        {
            getWorld().addObject(new HeartlessBlast(), getX(), getY());
            shotDelay = 0;
        }
        shotDelay++;
    }
   public boolean isOnGround()
    {
        Clone Window = (Clone) getWorld();
        if(getY() + (getImage().getHeight() / 2) >= (Window.getHeight() -1)) {
            return true;
        }
        return false;
    }
   public void randomjump()
   {
       if(OnGround && Greenfoot.getRandomNumber(100) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed = -10;  
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
}
cloud41910 cloud41910

2014/12/2

#
but it's not jumping
danpost danpost

2014/12/2

#
Line 72 restricts jumping until the 'OnGround' field is set to 'true'. I do not see anywhere where that could possibly happen in this class as it is. That, and you are over-complicating things by adding redundancies ('CanJump', 'OnGround', 'isOnGround()'). You really only need one of them. 'OnGround' is synonymous with 'CanJump'; and 'isOnGround()' would return a more current state than 'OnGround' might contain.
cloud41910 cloud41910

2014/12/2

#
I tried it like this now it jumps but it only jumps little by little until it goes to the top it doesn't jump like how my main character jumps and it doesn't fall as well
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heartless here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    /**
     * Act - do whatever the Heartless wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public class Heartless extends Actor
{
    HeartlessBlast heartlessblast = new HeartlessBlast();
    private int shotDelay = 0;
    private int JumpChance = 0;
    private int vspeed = 0;
    private int acceleration = 0;
    public void act() 
    {
        fap();
        shoot();
        randomjump();
    }
    public void annihilate()
    {
        Actor K = getOneObjectAtOffset(0,0,K.class);
     getWorld().addObject(heartlessblast, getX(), getY());
    
    if(K != null){
       World world = getWorld();
        NewGameOver newgameover = new NewGameOver();
        world.addObject(newgameover, world.getWidth()/2,world.getHeight()/2);
        world.removeObject(K);
        
    }
    
    }
      public void fap()
    {
         getWorld().addObject(heartlessblast, getX(), getY());  
    }
    
    public void shoot()
    {
        if(shotDelay >= 100)
        {
            getWorld().addObject(new HeartlessBlast(), getX(), getY());
            shotDelay = 0;
        }
        shotDelay++;
    }
   public boolean isOnGround()
    {
        Clone Window = (Clone) getWorld();
        if(getY() + (getImage().getHeight() / 2) >= (Window.getHeight() -1)) {
            return true;
        }
        return false;
    }
   public void randomjump()
   {
       if( Greenfoot.getRandomNumber(100) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed = -10;  
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
}
There are more replies on the next page.
1
2
3
4
5
6