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

2021/2/28

climbing a ladder

Genota Genota

2021/2/28

#
Hello! I am trying to create a scenario, where my actor is able to climb a ladder. However it doesn´t seem to work right, because when I press "up" my Actor starts to bounce just a bit on and off the Ground underneath. I am thinking, that it might be something in relation to my "JumpHeight". Here is my code for the Actor:
    public void jump()
    {
    if(Greenfoot.isKeyDown("space")&& (onGround()==true))
    {
            vSpeed = jumpHeight;
            fall();
    }
        else if(isTouching(Ladder.class))
    {
      if(Greenfoot.isKeyDown("up"))
      {
      int ypos = getY();
            
      ypos = ypos - 5;
      setLocation(getX(), ypos);
      }
    }
 
   }
Its just the public void jump and of course I added it in my act method and got no syntax errors
danpost danpost

2021/2/28

#
Touching ladder should be considered on ground.
Genota Genota

2021/2/28

#
Nothing changed... The same problem appeared. Or Maybe I did something wrong. Here is my raw Actor code:
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int vSpeed = 0;
    private int accel = 1;
    private int speed = 5;
    private int jumpHeight= -20;
    private boolean jumping = false;
    private int attacking = -1;
    private int animationSpeed = 2;
    private int shotDelay;
    boolean touchingEnemy1 = false;
    private GreenfootSound music = new GreenfootSound("background_music.mp3");
    private GreenfootSound sound = new GreenfootSound("GameOver_sound.mp3");
    private GreenfootSound sound3 = new GreenfootSound("Coin_1.mp3");
    
    private GreenfootImage run1 = new GreenfootImage("Glumanda1right1.png");
    private GreenfootImage run2 = new GreenfootImage("Glumanda2right1.png");
    private GreenfootImage run3 = new GreenfootImage("Glumanda3right1.png");
    private GreenfootImage run4 = new GreenfootImage("Glumanda1right2.png");
    private GreenfootImage run5 = new GreenfootImage("Glumanda2right2.png");
    private GreenfootImage run6 = new GreenfootImage("Glumanda3right2.png");
    private GreenfootImage run7 = new GreenfootImage("Glumanda1left1.png");
    private GreenfootImage run8 = new GreenfootImage("Glumanda2left1.png");
    private GreenfootImage run9 = new GreenfootImage("Glumanda3left1.png");
    private GreenfootImage run10 = new GreenfootImage("Glumanda1left2.png");
    private GreenfootImage run11 = new GreenfootImage("Glumanda2left2.png");
    private GreenfootImage run12 = new GreenfootImage("Glumanda3left2.png");
    private GreenfootImage run13 = new GreenfootImage("Glumandamouth1.png");
    private GreenfootImage run14 = new GreenfootImage("Glumandamouth2.png");
    private GreenfootImage run15 = new GreenfootImage("Glumandamouth3.png");
    private GreenfootImage run16 = new GreenfootImage("Glumandamouth4.png");
    private GreenfootImage run17 = new GreenfootImage("Glumandamouth1left.png");
    private GreenfootImage run18 = new GreenfootImage("Glumandamouth2left.png");
    private GreenfootImage run19 = new GreenfootImage("Glumandamouth3left.png");
    private GreenfootImage run20 = new GreenfootImage("Glumandamouth4left.png");
    private GreenfootSound sound1 = new GreenfootSound("Bump_sound.mp3");
    private GreenfootSound sound2 = new GreenfootSound("fire_sound.mp3");
    private int frame = 3;
    private int animationCounter = 0;
    GifImage myGif = new GifImage ("Gif_PS.gif");
    GifImage myGif2 = new GifImage ("Gif_PSleft.gif");
    private int moveToGround;
    private int direction = 1;
    private int coinsCollected =0;
    
    public void act() 
    {
        checkFalling();
        fall();
        jump();
        moveAround();
        animationCounter ++;
        fireProjectile();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        hitEnemy();
        music();
        if (collectItem(CoinUpAndDown.class)) 
        coinsCollected++;
        if (collectItem(Coin.class)) 
        coinsCollected++;
    }  
    public void moveAround()
    {
      if(Greenfoot.isKeyDown("d"))
      
      {
          setLocation(getX() + speed, getY());
          direction = 1;
          if(animationCounter % 8 == 0)
           animateRight();
      }
      if(Greenfoot.isKeyDown("a"))
       {
         setLocation(getX() - speed, getY());
         direction = -1;
         if(animationCounter % 8 == 0)
         animateLeft();
      }
    }
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
    }
    public void checkFalling()
    {
        if (!isTouching(Ground.class))
        {
            vSpeed++;
        }
        else 
            vSpeed = 0;
        
    }
    public void jump()
    {
    if(Greenfoot.isKeyDown("space")&& (onGround()==true))
    {
            vSpeed = jumpHeight;
            fall();
    }
    }
    public boolean onGround()
    {
    int spriteHeight = getImage().getHeight();
    int yDistance = (int) (spriteHeight / 2 + 5);
    Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
    if(ground == null)
    {
         return false;   
    }
    else
    {
         moveToGround(ground);
         return true;
    }
Maybe you cant tell me in a more specific way what I have to do
danpost danpost

2021/2/28

#
At line 118, insert the following:
if (isTouching(Ladder.class)) return true;
You need to login to post a reply.