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

2016/4/24

health

Walker Walker

2016/4/24

#
i would like a health boolean for my soul (i am making an undertale fight) so heres the code so far
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.collision.CollisionChecker;

/**
 * Write a description of class Soul here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Soul extends Actor
{
    
   
    
    /**
     * Act - do whatever the Soul wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeyPress();
        
            if ( touchingObjectBlast() )
            {
                 //i want it to remove health
        }
    }   
    
    public boolean touchingObjectBlast() 
    {  
     return (getOneIntersectingObject(Blast.class) != null);
     

    }

  
    
  public void checkKeyPress()
   {
     if (Greenfoot.isKeyDown("up"))
    {
        setLocation(getX(), getY()-2);
    }
    if (Greenfoot.isKeyDown("down"))
    {
        setLocation(getX(), getY()+2);
     }
    if (Greenfoot.isKeyDown("left"))
    {
        setLocation(getX()-2, getY());
    }
    if (Greenfoot.isKeyDown("right"))
    {
        setLocation(getX()+2, getY());
    }
   }
}
danpost danpost

2016/4/24

#
A boolean value can be either true or false. As a field, they are usually used to determine which of two different states the actor is in -- examples being 'onGround' (as opposed to off the ground); 'paused' (as opposed to active); 'movingLeft' (as opposed to moving right -- as a directional); 'carryingTomato' (as opposed to not carrying a tomato). So, a boolean for health is what-- has health as opposed no not having health (or dead)?
valdes valdes

2016/4/25

#
I recomend you add an integer field, not boolean. After the declaration of your class, put private int health = some_number; this number will be the initial health of your Actor. In the if (), decrease the value of health. Example health--; Then you will have to add another if in the act() method to check if the Actor is dead and end the game. if (health <= 0)
You need to login to post a reply.