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

2014/5/14

Star Wars Game

BeYouself BeYouself

2014/5/14

#
In my Game, I want to decrease the actor's health as time goes by. How do i do this? Could anyone post the code?
danpost danpost

2014/5/14

#
Introduce an instance field 'private int health = s*50;' where s is the lifespan of the actor in seconds. In the act method, subtract one from its value and check for zero (death).
GRIFFIN GRIFFIN

2014/5/14

#
Here is a basic example of what you would want:
int health = 100;
int time = 0;
public void decreaseHealth(){
    time ++;
    if(time >= 10 /*You can change this*/ && health > 0){
             health = health - 1;
             time = 0;
         }
}
danpost danpost

2014/5/15

#
danpost wrote...
Introduce an instance field 'private int health = s*50;' where s is the lifespan of the actor in seconds. In the act method, subtract one from its value and check for zero (death).
An example of this follows for an actor that lives for about 10 seconds:
private int health= 500;

public void act()
{
    exist();
}

private void exist()
{
    health--;
    if (health == 0) getWorld().removeObject(this);
}
You need to login to post a reply.