Im trying to make a platformer (a recreation of meet the robinsons) and I have a few obstacles like crates what you can use to jump on top of. But after I did the code the box radius seems to be a bit too big and when I jump on the box and if I move again I fall right through the box.
Code for main character:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class RobinsonStill here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RobinsonStill extends Actor
{
public int vSpeed = 0;
public int gravity = 2;
public boolean grounded = true;
public boolean jumping;
public int jumpStregth = 20;
public int speed = 5;
/**
* Act - do whatever the RobinsonStill wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkFall();
checkKeys();
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("Up") && grounded == true)
{
jump();
}
if(Greenfoot.isKeyDown("right"))
{
setLocation(getX() + speed, getY());
}
if(Greenfoot.isKeyDown("left"))
{
setLocation(getX() - speed, getY());
}
}
public void setLocation(int x, int y)
{
int oldX = getX();
int oldY = getY();
super.setLocation(x, y);
if(!getIntersectingObjects(Crate.class).isEmpty())
{
super.setLocation(oldX, oldY);
}
}
public RobinsonStill()
{
GreenfootImage myImage = getImage();
int myNewHeight = (int)myImage.getHeight()/6;
int myNewWidth = (int)myImage.getWidth()/6;
myImage.scale(myNewWidth, myNewHeight);
}
public void checkFall()
{
if(onGround()){
vSpeed = 0;
grounded=true;
}else{
grounded=false;
fall();
}
}
public void fall()
{
setLocation(getX(), getY() + vSpeed);
vSpeed = vSpeed + gravity;
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int lookForGround = spriteHeight/2;
Actor ground = getOneObjectAtOffset(0,lookForGround,Floor.class);
return ground !=null;
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() + groundHeight + getImage().getHeight();
setLocation(getX(), newY);
jumping = false;
}
public void jump()
{
vSpeed = -jumpStregth;
fall();
}
public void checkObstacles()
{
Actor crate = getOneIntersectingObject(Crate.class);
if(crate!=null)
{
}
}
}
