Hi everyone, recently I've gotten pretty into Greenfoot, for a school project, and I've got my basic movement done. However, when I jump on a platform it's fine however on the edge on platforms the character sprite slips off
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Character1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Character1 extends Actor
{
private int vSpeed = -20;
private int Speed = 4;
private boolean jump;
private int acceleration = 1;
/**
* Act - do whatever the Character1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveplayer();
checkFall();
checkAbove();
checkRightWall();
checkLeftWall();
}
public void checkFall()
{
if(onGround()) {
vSpeed = 0;
}
else {
fall();
}
}
public void moveplayer()
{
if(Greenfoot.isKeyDown("left")) {
setLocation(getX()-Speed, getY());
}
if(Greenfoot.isKeyDown("right")) {
setLocation(getX()+Speed, getY());
}
if(Greenfoot.isKeyDown("space")) {
jumpcheck();
}
}
public void jump()
{
vSpeed = -20;
jump = true;
fall();
}
public void jumpcheck()
{
if(Greenfoot.isKeyDown("space") && jump == false){
jump();
}
}
public void fall()
{
setLocation(getX(), getY() + vSpeed);
if (vSpeed <9)
{
vSpeed = vSpeed + acceleration;
}
jump = true;
}
public boolean onGround()
{
int characterheight = getImage().getHeight();
int findground = (int)(characterheight/2);
Actor ground = getOneObjectAtOffset(0, findground, Platform.class);
if (ground == null)
{
jump = true;
return false;
}
else
{
repositioncharacter(ground);
return true;
}
}
public void repositioncharacter (Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
jump = false;
}
public boolean checkAbove()
{
int characterheight = getImage().getHeight();
int findground = (int)(characterheight/-2);
Actor above = getOneObjectAtOffset(0, findground, Platform.class);
if (above != null)
{
vSpeed = 1;
bopHead(above);
return true;
}
else
{
return false;
}
}
public void bopHead (Actor above)
{
int aboveHeight = above.getImage().getHeight();
int newY = above.getY() +(aboveHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public boolean checkRightWall()
{
int spriteWidth = getImage().getWidth();
int width = (int)(spriteWidth/2);
Actor rightWall = getOneObjectAtOffset(width, 0, Platform.class);
if (rightWall == null)
{
return false;
}
else
{
stopMovingRight(rightWall);
return true;
}
}
public void stopMovingRight(Actor rightWall)
{
int wallWidth = rightWall.getImage().getWidth();
int newX = rightWall.getX() - ((wallWidth + getImage().getWidth())/2);
setLocation(newX, getY());
}
public boolean checkLeftWall()
{
int spriteWidth = getImage().getWidth();
int width = (int)(spriteWidth/-2);
Actor LeftWall = getOneObjectAtOffset(width, 0, Platform.class);
if (LeftWall == null)
{
return false;
}
else
{
stopMovingLeft(LeftWall);
return true;
}
}
public void stopMovingLeft(Actor LeftWall)
{
int wallWidth = LeftWall.getImage().getWidth();
int newX = LeftWall.getX() + ((wallWidth + getImage().getWidth())/2);
setLocation(newX, getY());
}
}