For some unknown reason to me, the main player ("Willa" class) does not "walk" directly on top of the Platform objects. Instead, it looks like she is walking through the platform (note: not referring to stacked platforms). Sometimes she does walk directly on top of the platform if I make her jump, this doesn't always fix the problem though. Not sure how to fix this glitch. Any help would be appreciated. Please let me know if I need to post the other code.
Thank you!
public class AnimatedActor extends Actor
{
private int frame = 0; //current frame
private String name = "willa"; //base name for image
private String extension = ".gif"; //file extension
private int speed = 10; //speed of animation
private int speedCounter = 0; //time to next image change
public void animate(int first, int last) //first frame e.g. 0, last frame e.g. 3
{
if (speedCounter >= speed) //Only animate when counter has reached speed
{
speedCounter = 0; //reset counter
if (frame < first || frame >= last) //if the frame is outside the first/last range
{
frame = first; //set to first frame
}
else
{
frame++; //otherwise add 1 to the frame number
}
setImage(name + frame + extension); //display the next image
}
else
{
speedCounter++; //if counter not up to speed add 1 to counter
}
}
}public class Willa extends AnimatedActor
{
private int vSpeed = 0;
private int acceleration = 2;
private int jumpHeight = -12; // how high to jump
/**
* Act - do whatever the Willa wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
control();
checkFalling();
}
private void control()
{
if (Greenfoot.isKeyDown("right"))
{
animate (4,5);
}
if (Greenfoot.isKeyDown("left"))
{
animate (2,3);
}
if (Greenfoot.isKeyDown("up")&& (onGround() == true))
{
vSpeed = jumpHeight;
fall();
}
}
private void fall()
{
setLocation(getX(),getY()+ vSpeed);
vSpeed = vSpeed + acceleration;
}
private boolean onGround()
{
Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
return under != null;
}
private void checkFalling()
{
if (onGround() == false)
{
fall();
}
}
}
