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:
And this is the code for Life:
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 +.
public void stopWhenCollideWithCar()
{
if(isTouching(Car.class)){
lifes = lifes -1;
Greenfoot.playSound("explosion.wav");
}
}public void removeHeart()
{
if(lifes == 2){
getWorld().removeObject(this);
}
if(lifes == 1){
getWorld().removeObject(this);
}
}