This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ed_Hazza
Ed_Hazza wrote ...

2016/4/22

How to make an Actor climb a ladder in greenfoot?

1
2
Ed_Hazza Ed_Hazza

2016/4/22

#
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();
        }
    }
}
FutureCoder FutureCoder

2016/4/23

#
if(isTouching(ladder.class)) { if(Greenfoot.isKeyDown("up")) { int ypos = getY(); ypos = ypos - 5; setLocation(getX(), ypos); } } This is a simple way to climb a ladder. You can change the speed of climbing the ladder if you change - 5, and add in a counter to change the image, if you want it to look more realistic when climbing the ladder.
Ed_Hazza Ed_Hazza

2016/4/23

#
Thank you very much! the only problem which i am having is that because i have a jump strength, it limits it and the guy just bobs on the ground when i try to climb the ladder, is there a way around this? Thanks !
FutureCoder FutureCoder

2016/4/23

#
Try to put my code after the code you used for jump strength, e.g: public void jump() { if (Greenfoot.isKeyDown("space") && onGround()) { vspeed = jumpStrength; fall(); }else if(isTouching(ladder.class)) { if(Greenfoot.isKeyDown("up")) { int ypos = getY(); ypos = ypos - 5; setLocation(getX(), ypos); } } //vspeed = -3; //fall(); } This might not work, but try it anyway. If it doesn't, tell me and i will try something else
Ed_Hazza Ed_Hazza

2016/4/23

#
I am not to sure what you mean about where to put it after jump strength. After my public void jump? or instead of?
FutureCoder FutureCoder

2016/4/23

#
in the public void jump. Look at the code. Its your code, but when you close if(Greenfoot.isKeyDown("space") && onGround()), carry it on with else if(isTouching(ladder.class)) { if(Greenfoot.isKeyDown("up")) { int ypos = getY(); ypos = ypos - 5; setLocation(getX(), ypos); } } then you close public void jump. The whole thing should look like this public void jump() { if (Greenfoot.isKeyDown("space") && onGround()) { vspeed = jumpStrength; fall(); }else if(isTouching(ladder.class)) { if(Greenfoot.isKeyDown("up")) { int ypos = getY(); ypos = ypos - 5; setLocation(getX(), ypos); } } }
Ed_Hazza Ed_Hazza

2016/4/23

#
He still runs and jumps as normal however does nothing when I reach the ladder and press up. :/
FutureCoder FutureCoder

2016/4/23

#
Stride is a coding language that you can use on Greenfoot, are you using that, or java
Ed_Hazza Ed_Hazza

2016/4/23

#
Stride? I am fairly certain I am using Java. How can I tell?
FutureCoder FutureCoder

2016/4/23

#
Type in on main character if(Greenfoot.isKeyDown("space")) { jump() } If an error message comes up, it's java, if it doesn't it's stride.
FutureCoder FutureCoder

2016/4/23

#
I need to know what you're using
Ed_Hazza Ed_Hazza

2016/4/23

#
It doesnt like that. It needs to be under a public void, and I have already got that
FutureCoder FutureCoder

2016/4/23

#
press scenario, scenario information, then see if it has 'The original version was written in Java; this version has been rewritten in Stride.' at the bottom, then i'll know. Sorry, just figured that's how you tell if its stride of java.
Ed_Hazza Ed_Hazza

2016/4/23

#
Brings up a readme.text. Then it says right at the bottom Moe Version 3.0.0
FutureCoder FutureCoder

2016/4/23

#
its got to be java then, but i don't understand. i tested your code and it doesn't recognise fall(), and a couple other of the things you've used.
There are more replies on the next page.
1
2