I'm having difficulty in making my frog lose a life if it touches any of the cars. Greenfoot says that a non-static method cannot be referenced from a static method. The remove when touching is placed in each individuals cars act method.
This is also the code of the frog class.
public class Cars extends Actor
{
/**
* Act - do whatever the Cars wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
public void removeWhenTouching()
{
if(isTouching(Frog.class))
{
removeTouching(Frog.class);
if(Frog.getLives() == 1)
{
Greenfoot.stop();
}else{
Frog.loseLife();
}
}
}
}
public class Frog extends Actor
{
private String[] move = {"frogup1.png","frogup2.png","frogup3.png","frogup4.png","frogup5.png","frogup6.png"};
private int frameCounterMove = 0;
private int reload;
int lives = 3;
/**
* Act - do whatever the Frog wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
if (isTouching(Car3.class))
{
loseLife();
}
if (isTouching(Car4.class))
{
loseLife();
}
if (isTouching(Car2.class))
{
loseLife();
}
if (isTouching(Car1.class))
{
loseLife();
}
}
public void move()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-5);
setRotation(360);
animateOnMove();
}
else if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-5, getY());
setRotation(270);
animateOnMove();
}
else if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+5, getY());
setRotation(90);
animateOnMove();
}
else if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+5);
setRotation(180);
animateOnMove();
}
}
public void animateOnMove()
{
reload++;
if(reload==3)
{
setImage(move[frameCounterMove]);
frameCounterMove++;
if(frameCounterMove >= move.length)
frameCounterMove=0;
reload = 0;
}
}
public int getLives()
{
return lives;
}
public void loseLife()
{
lives = lives - 1;
}
}
