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

2013/10/31

Life system isn't working

sihaco sihaco

2013/10/31

#
I have a class Tumbler and a subclass of Tumbler called Life. The Tumbler has a public field called lifes which is initially equal to 3. When the Tubmler hits an object of the class Car, the field lifes should decreased with one (lifes = lifes - 1). In the class Life (subclass of Tumbler) I have code to remove one object of Life (which is a heart symbol). The problem is, instead of doing lifes = lifes - 1 (so the first time, it should be 3 - 1 = 2) lifes gets a value of -185. This is the code for the method when the Tumbler hits a Car:
public void stopWhenCollideWithCar()
    {
        if(isTouching(Car.class)){
        lifes = lifes -1;
        Greenfoot.playSound("explosion.wav");
        }
    }
And this is the code for Life:
public void removeHeart()
    {
        if(lifes == 2){
            getWorld().removeObject(this);
        }
        if(lifes == 1){
            getWorld().removeObject(this);
        }
    }
Both methods are called from the act method. What's the problem? It should simply just do life - 1, instead of making the value of life equal to -100 +.
You need to login to post a reply.