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

2011/10/24

Health Points

DMCGames DMCGames

2011/10/24

#
I'm a beginner coder and I need help with hp or Health Points.
danpost danpost

2011/10/24

#
Add an integer instance variable to each Actor class that has health points. How to: Inside the class, but outside any methods within the class, add a line of code declaring the instance variable
public class ClassName extends Actor
{
    private integer healthPoints = 100; // The '100' can be whatever that Actor starts with

    public ClassName()  // Constructor
    {
    }

    public void act()
    {  // etc.
Each Actor within the class 'ClassName' will contain an instance variable to track its health points.
DMCGames DMCGames

2011/10/25

#
So, to decrease you use something like if (some kind of action) { healthpoints - 10; } or something more complicated?
danpost danpost

2011/10/25

#
If your variable name is 'healthpoints' (all lower case), then you would use the following: healthpoints -= 10; (or) healthpoints = healthpoints - 10; The first syntax with the minus/equal combination means to subtract what is on the right from the value of the variable on the left and set the variable on the left to that new value. Your 'if' format looks good otherwise. By the way, you may want to perform changes on one actor from another's class. The tutorials (I believe section 5) has a bit about accessing one Actor class from another.
DMCGames DMCGames

2011/10/25

#
Thank you it help me a lot.
You need to login to post a reply.