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

2015/9/9

Health not working

hoothoot hoothoot

2015/9/9

#
hello, this is yet another noob programmer trying to figure out what's wrong with my code. If you've seen DBZ, yes this is the same gohan and cell. anyway, i cant get my main character (gohan) to lose health when attacked. i have the following codes that deal with health: for gohan,
    private int health = 9001; //its over 9000-don't know if this works  
    public void setHealth(int points) {
        health += points;  
    }
    public int getHealth() {
        return health;
    }

//and

 //this works
private void hitEnemy() {
        Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
        if (cell != null) {
            cell.setHealth(-10);
        }
    }
Super_Hippo Super_Hippo

2015/9/9

#
This was the second part. (You used 'quote' instead of 'code' in the end, so it wasn't displayed.)
        //this works
        private int health = 9001;
         
        public void setHealth(int points) {
            health += points;  
        }
        public int getHealth() {
            return health;
        }

        //and

        //this doesn't
        private void hitEnemy() {
        Gohan gohan = (Gohan) getOneObjectAtOffset(0, 0, Gohan.class);
        if (gohan != null) {
            gohan.setHealth(-9001);
        }
'Does not work' = he doesn't lose life? Do you call the 'hitEnemy' method from the act-method? How do you know it doesn't work? You use 'gohan.setHealth(-9001);', so he would be at 0 health right after he got the first hit (9001-9001=0). And since Cell will hit Gohan when Gohan hits Cell, Gohan can't win the fight. Just a small thing: You have a method called 'setHealth' but you don't really 'set' the health to a new value. Instead, you add the health. So the method's name should be 'addHealth'.
hoothoot hoothoot

2015/9/9

#
thanks I figured it out on my own but this is exactly what I did.
hoothoot hoothoot

2015/9/9

#
i have a new question maybe you can solve. look on my profile plz
Super_Hippo Super_Hippo

2015/9/10

#
I don't see anything on your profile :? Here is what I see
hoothoot hoothoot

2015/9/10

#
I'm past it anyways, but thanks for the help.
You need to login to post a reply.