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

2017/11/23

How to make Health Point for my Main Actor ?

donysamdhila donysamdhila

2017/11/23

#
I have three class right there for make this one. But when i run this project, object "Life" automatically disappears from MyWorld before my main object is touched by enemy 1st, Nyawa.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Nyawa here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Nyawa extends Actor
{
    /**
     * Act - do whatever the Nyawa wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int nyawa=1;
    public void act() 
    {
        // Add your action code here.
    }    
}
2nd, Life.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Life here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Life extends Actor
{
    public void act() 
    {
        if(Nyawa.nyawa == 0);
        {
        World world;
        world = getWorld();
        world.removeObject(this);
        }
    }
}
3rd Enemy3.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy3 extends Actor
{
    /**
     * Act - do whatever the Enemy3 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int direction = 5;
    public void act() 
    {
        // Add your action code here.
        gerak();
        damage();
    }
    public void gerak()
    {
        move(direction);
        if (isAtEdge()) direction = -direction;
    }
    public void damage()
    {
        Actor Alien;
        Alien = getOneObjectAtOffset(0,0,Alien.class);
        if (Alien != null)
        {
            Nyawa.nyawa--;
        }
    }
}
danpost danpost

2017/11/23

#
Line 15 in the Nyawa class will only set the value of 'nyawa' to '1' when the project is compiled (because you have made it a ''static' (or class) field. As such, you need to set its value either during the construction of the world or during the construction of the Nyawa object..
You need to login to post a reply.