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

2014/11/28

How to apply HealthBar

2
3
4
5
6
danpost danpost

2014/12/2

#
Now, you are not restricting the jump at all if not on the ground. Change line 64 to
if (isOnGround() && Greenfoot.getRandomNumber(100) == 3)
The other problem is that your 'fall' method is only being called one time per jump -- when the jump is initiated in the 'jump' method. Also, there is no restriction as to how far you can fall. Your actor will, once falling is fixed, fall half-way into the bottom edge of the world unless regulated. Line 19 still shows no acceleration due to gravity.
cloud41910 cloud41910

2014/12/2

#
It only jumped once and it didn't fall like it's supposed to after jumping am i gonna make a boolean instance field for this?
danpost danpost

2014/12/2

#
Did you add acceleration (change the value of the field to 1 or 2)? Please post the revised code.
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 = 5;
    boolean CanJump;
    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( isOnGround() && Greenfoot.getRandomNumber(100) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed = -10;  
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
}
danpost danpost

2014/12/2

#
Insert the following line before line 59 (before 'return true;' in the 'isOnGround' method:
vSpeed = 0;
Also, change the acceleration value to either 1 or 2 (preferably 1). You could also add the following line at the same place as the previous one:
setLocation(getX(), (getWorld().getHeight()-getImage().getHeight())/2);
And I still do not see where you are calling 'fall' from (other than from the 'jump' method).
danpost danpost

2014/12/2

#
Add a call to 'fall' as the first line in your act method and reverse the order of the other calls in that method:
public void act()
{
    fall();
    randomJump();
    shoot();
    fap();
}
cloud41910 cloud41910

2014/12/2

#
what if i want the height of it's jump to change?
cloud41910 cloud41910

2014/12/2

#
so everytime it jumps there would be a possibility to make its jump higher or lower
danpost danpost

2014/12/2

#
Use a random number plus some to add to 'vSpeed' when jump is initiated. Change the line in the 'jump' method to this::
vSpeed += 8+Greenfoot.getRandomNumber(8);
or something like that.
cloud41910 cloud41910

2014/12/2

#
I tried putting the healthbar to my enemy and i'm getting this error
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:681)
	at greenfoot.Actor.getY(Actor.java:170)
	at Heartless.isOnGround(Heartless.java:59)
	at Heartless.randomjump(Heartless.java:66)
	at Heartless.act(Heartless.java:28)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
cloud41910 cloud41910

2014/12/2

#
here's the code
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 = 1;
    boolean CanJump;
    private Bar healthBar;
    public void act() 
    {
        fall();
        fap();
        shoot();
        hit();
        randomjump();
        healthBar.setLocation(getX(), getY());  
    }

    public Heartless()
        {  
        healthBar = new Bar("", "", 100, 100);  
        healthBar.setShowTextualUnits(false);  
        healthBar.setBarWidth(30);  
        healthBar.setBarHeight(3);  
        healthBar.setMaximumValue(30);
        healthBar.setValue(30);
        healthBar.setBreakValue(10); 
    }  
      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)) {
             vspeed = 0; return true;
        }
        return false;
    }
   public void randomjump()
   {
       if( isOnGround() && Greenfoot.getRandomNumber(100) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed = -10;  
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
      protected void addedToWorld(World world)  
    {  
        world.addObject(healthBar, getX()-0, getY()-0);  
    }  
   public void hit()
   {
         Actor kiblast = getOneIntersectingObject(KiBlast.class);  
        if(kiblast != null)  
        {  
            healthBar.subtract(10);  
            getWorld().removeObject(kiblast);
        }  
           if(healthBar.getValue() == 0)  
        {    
        getWorld().removeObject(healthBar);  
        getWorld().removeObject(this);
        }  
    }
}
danpost danpost

2014/12/3

#
I did mention the order of your calls in the act method above. The 'healthbar.setLocation' line should be up there also -- immediately after 'fall'.
cloud41910 cloud41910

2014/12/3

#
I tried it
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 = 1;
    boolean CanJump;
    private Bar healthBar;
    public void act() 
    {
        healthBar.setLocation(getX(), getY());  
        fall();
        fap();
        shoot();
        hit();
        randomjump();
        
    }

    public Heartless()
        {  
        healthBar = new Bar("", "", 100, 100);  
        healthBar.setShowTextualUnits(false);  
        healthBar.setBarWidth(30);  
        healthBar.setBarHeight(3);  
        healthBar.setMaximumValue(30);
        healthBar.setValue(30);
        healthBar.setBreakValue(10); 
    }  
      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)) {
             vspeed = 0; return true;
        }
        return false;
    }
   public void randomjump()
   {
       if( isOnGround() && Greenfoot.getRandomNumber(100) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed = -10;  
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
      protected void addedToWorld(World world)  
    {  
        world.addObject(healthBar, getX()-0, getY()-0);  
    }  
   public void hit()
   {
         Actor kiblast = getOneIntersectingObject(KiBlast.class);  
        if(kiblast != null)  
        {  
            healthBar.subtract(10);  
            getWorld().removeObject(kiblast);
        }  
           if(healthBar.getValue() == 0)  
        {    
        getWorld().removeObject(healthBar);  
        getWorld().removeObject(this);
        }  
    }
}
cloud41910 cloud41910

2014/12/3

#
It has this error
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:681)
	at greenfoot.Actor.getY(Actor.java:170)
	at Heartless.isOnGround(Heartless.java:59)
	at Heartless.randomjump(Heartless.java:66)
	at Heartless.act(Heartless.java:28)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/12/3

#
Why do you still have 'randomJump' called last? Switch the first two lines of the act method and place the randomJump call between them.
There are more replies on the next page.
2
3
4
5
6