Hey, I'm trying to make collision detection on my platformer game. I've added the following, and it works, except for whenever I land on the ground after a fall I will fall anywhere from 0-20ish pixels deep into the platform I am landing on. Does anyone see an issue with this code? GroundOfBackVariety is my class for my platform.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class GroundBack here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Mover
{
private int vSpeed = 0;
private int jumpSpeed = -30;
private int gravity = 1;
private int animationTimer = 0;
private boolean onGround;
public Player()
{
this.velocity = 4;
}
/**
* Act - do whatever the GroundBack wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.ii
*/
public void act()
{
checkKeys();
animation();
checkFalling();
groundCollision();
}
private void animation()
{
animationTimer++;
if(animationTimer == 10)
{
setImage("French Guy Walking1.png");
}
if(animationTimer == 20)
{
setImage("French Guy Walking2.png");
animationTimer = 0;
}
if(getOneObjectAtOffset(0, getImage().getHeight()+10, GroundOfBackVariety.class) == null)
{
setImage("French Guy Falling.png");
animationTimer = 0;
}
}
private void checkKeys()
{
if(Greenfoot.isKeyDown("d"))
{
moveRight();
}
if(Greenfoot.isKeyDown("a"))
{
moveLeft();
}
if(Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY() + jumpSpeed - gravity);
}
}
public void groundCollision()
{
if(getOneObjectAtOffset( 0, getImage().getWidth()/2 + 5, GroundOfBackVariety.class) != null)
{
onGround = true;
}
if(getOneObjectAtOffset( 0, getImage().getWidth()/2 + 5, GroundOfBackVariety.class) == null)
{
onGround = false;
}
if(onGround == false)
{
fall();
}
if(onGround == true)
{
setLocation(getX(), getY());
vSpeed = 0;
}
}
public void fall()
{
setLocation(getX(),getY() + vSpeed);
vSpeed = vSpeed + gravity;
}
private void checkFalling()
{
if (onGround == false)
{
fall();
}
}
}

