Here is my current code for my actor, he is currently moving left and right and jumping. When touching he doesn't move through the ground. The last thing I want him to do is to be able to climb a ladder. I know what I will need to make a class ladder and have that as an object, but does anyone know any simple code for making him climb smoothly when pressing "up".
Thanks
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Spaceman here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Spaceman extends Actor
{
/**
* Act - do whatever the Spaceman wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int speed = 1;
private int vspeed = 0;
private int acceleration = 1; //How quickly the falls down
private Boolean onFloor;
private int jumpStrength = -10; //Limits the jump height
private final int DURATION = 30; //Stops the spamming of spacebar
private int spambuster;
private GreenfootImage rR = new GreenfootImage ("spaceman3.gif");
private GreenfootImage lL = new GreenfootImage ("spaceman4.gif");
public void act()
{
go();
if(isTouching(null)){ checkFall() ;}else {fall();};
}
public void go()
{
{
if(Greenfoot.isKeyDown("right"))
{
setImage(rR);
setLocation(getX() + speed, getY());
//if(isTouching(null)){ checkFall() ;}else {fall();}
}
if(Greenfoot.isKeyDown("left"))
{
setImage (lL);
setLocation(getX() - speed, getY());
// if(isTouching(null)){ checkFall() ; }else {fall();}
}
if (spambuster != 0)
spambuster--;
if(Greenfoot.isKeyDown("space") && spambuster == 0)
{
jump();
spambuster= DURATION;
// if(isTouching(null)){ checkFall() ; }else {fall();}
{
}
}
}
}
public void jump()
{
if (Greenfoot.isKeyDown("space") && onGround())
{
vspeed = jumpStrength;
fall();
}
//vspeed = -3;
//fall();
}
public boolean onGround()
{
Object under = getOneIntersectingObject(Ground.class);
return under != null;
//Actor under = getOneObjectAtOffset (0,getImage().getHeight()/ 2,Platform.class);
//return under != null;
}
public void fall()
{
setLocation ( getX(), getY() + vspeed);
vspeed = vspeed + acceleration;
//vspeed += 1;
}
public void checkFall()
{
if (onGround()) {
vspeed = 0;
}
else {
fall();
}
}
}