I am trying to make it so when my play touches a platform, he gets teleported to the top of said platform, while retaining his x coordinate. There is also a gravity aspect to my game. However, when I run the program, I get this error:
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
And when I click on it, it highlights this line of code:
Actor plat = (Platform)getOneIntersectingObject(Platform.class);
import greenfoot.*;
public class Player extends Actor
{
private int accel = 1;
public int vSpeed = 0;
public void act()
{
onGround();
moving();
checkFall();
}
public void moving()
{
if(Greenfoot.isKeyDown("a"))
{
setRotation(180);
move(3);
setRotation(0);
}
if(Greenfoot.isKeyDown("d"))
{
setRotation(0);
move(3);
}
if ("space"==(Greenfoot.getKey()))
{
jump();
}
}
boolean onGround = false;
Actor plat = (Platform)getOneIntersectingObject(Platform.class);
int newY = plat.getY();
public void onGround()
{
if(isTouching(Platform.class))
{
setLocation(getX(), newY);
onGround = true;
}
else
{
onGround = false;
}
}
public void checkFall()
{
if(onGround)
{
vSpeed = 0;
}
else
{
falling();
}
}
public void falling()
{
setLocation(getX(), getY() + vSpeed);
vSpeed = vSpeed + accel;
}
public void jump()
{
if(onGround)
{
vSpeed = -14;
falling();
}
}
}
