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/11/30

#
also do you know how to make an actor randomly jump? cause i might start a new thread about it
danpost danpost

2014/11/30

#
Basically (in pseudo-code):
if (onGround() && Greenfoot.getRandomNumber(100)<JUMP_PERCENT_CHANCE) jump();
would be the condition required to jump; and (again, in pseudo-code):
verticalSpeed += JUMP_STRENGTH;
where the strength is a negative value, would cause the initial jumping and
verticalSpeed--;
setLocation(getX(), getY()+verticalSpeed);
would apply gravity and move the actor vertically the proper distance each act cycle. Collision check should be done after any movement. Horizontal movement should be done separately.
cloud41910 cloud41910

2014/11/30

#
In the Random number part should i make a certain value for my actor to jump? and this is my method for it so far
   public void RandomJump ()
    {
        if(isOnGround && Greenfoot.getRandomNumber(100)) jump();
    }
     public void jump()
    {
        fspeed = -10;
        fall();
    }
    public void fall()
    {
        setLocation ( getX(), getY() + fspeed);
        fspeed = fspeed + acceleration;
    }
danpost danpost

2014/11/30

#
Each condition within an 'if' clause must return a boolean value. 'Greenfoot.getRandomNumber(100)' returns an int value and cannot be used by itself as a condition. The int value can be tested for a specific value or range to return a boolean, however (which is why I had '<JUMP_PERCENT_CHANCE' with it, which should be a number between 1 and 100. When this value is compared to the value of the returned random number, it will be evaluated to be a true or false statement, which will be a limiting factor as to when your actor will jump. At very low values, your actor will occasionally jump; at high values, it will jump much more often.
cloud41910 cloud41910

2014/11/30

#
so if i use the Greenfoot.getRandomNumber method am i going to need a specific number between 1 to 100 to make it jump? also regarding the healthbar where can i edit the value of my healthbar?
danpost danpost

2014/11/30

#
You need something to compare the returned value of 'Greenfoot.getRandomNumber' to. This is not to make it jump; but, to regulate the amount of jumping -- so it does not jump instantly every time it hits the ground. You can edit the value of the healthBar anywhere in the class you have it in. The methods 'add', 'subtract' and 'setValue' all can change its value (for example: 'healthBar.setValue(100);' would restore the health to full). You can open the editor on the Bar class and in the upper right you will see a drop-down box that says 'Source code'; change it to 'Documentation' and look over the class.
cloud41910 cloud41910

2014/12/1

#
How can i set the location of my health bar at the top of my actor from the start? before pressing run the healthbar is at the middle and after i press run the healthbar is at the top of my actor
cloud41910 cloud41910

2014/12/1

#
Also how can i limit the subtraction of my healthbar when i touch my enemy here is my code so far
Actor heartless = getOneIntersectingObject(Heartless.class);
        {
            if(heartless != null)
            healthBar.subtract(10);
        }
cloud41910 cloud41910

2014/12/1

#
I tried to put a gameover screen on this method and i'm this error
java.lang.NullPointerException
	at K.hit(K.java:132)
	at K.act(K.java:35)
	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/1

#
this is my code for it
        if(healthBar.getValue() == 0)  
        {    
        getWorld().removeObject(healthBar);  
        getWorld().removeObject(this);
        World world = getWorld();
        NewGameOver newgameover = new NewGameOver();
        world.addObject(newgameover, world.getWidth()/2,world.getHeight()/2);
        }  
    } 
danpost danpost

2014/12/1

#
Line 5 tries to get the world the actor is in when you have already removed it from the world on line 4. For limiting the decreasing healthbar, you need a boolean instance field to track when you are touching a Heartless object or not. Use its value as a regulator in decreasing the health when touching one. Reset its value when it is set and you no longer are touching one.
cloud41910 cloud41910

2014/12/1

#
how can i reset the value when it''s no longer touching one ?
cloud41910 cloud41910

2014/12/1

#
i made this boolean already
    public boolean touchingenemy()
    {
        Actor heartless = getOneIntersectingObject(Heartless.class);
        if(heartless != null){
            return true;
        }
        return false;
    }
danpost danpost

2014/12/1

#
No. That is a boolean method -- not a boolean instance field.
// with boolean field (outside method)
private boolean onHeartless = false;
// then inside method
if (isTouching(Heartless.class)
{
    if (!onHeartless)
    {
        healthBar.subtract(10);
        onHeartless = true;
    }
}
else onHeartless = false;
This should regulate the damage done to once per contact.
cloud41910 cloud41910

2014/12/1

#
am i going to make a variable of "isTouching" or is it already in the API?
There are more replies on the next page.
1
2
3
4
5
6