I want an object to move up and down the whole time (touch ground -> go up, touch ceiling -> go down) but its only doing it 4 times and then the object gets stuck into the ground and wont move up somehow :/
would be nice if someone could help! :D
import greenfoot.*;
public class upDownObject extends Actor
{
private int down = 5; //down-moving speed
private int up = -3; //up-moving speed
private int falling = 1;
public void act()
{
checkIfTouching(); //check if he is touching the ground cause if he does he should move up
checkIfDown(); //check if he can move down
checkeIfUp(); //check if he can move up
}
public void moveDown()
{
setLocation(getX(), getY() +down); // +5
}
public void moveUp()
{
setLocation(getX(), getY() +up); // +-3
}
public void checkIfDown() //check if he can move down
{
if (falling == 1)
{
moveDown();
}
}
public void checkIfUp() //check if he can move up
{if (falling == 0)
{
moveUp();
}
public void checkIfTouching()
{
if(isTouching(ground.class))
{
falling--; // if he's touching the ground he will go to checkIfUp-method and move up
}
if(isTouching(ceiling.class))
{
falling++; //if he's touching the ceiling he will go to checkIfDown-method and move down
}
}
}

