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

2021/3/26

improvement for this code?

PetrusderEchte PetrusderEchte

2021/3/26

#
Hello! I have this Code for moveing and jumping:
import greenfoot.*;  
public class Jumper1 extends Actor
{
    private final int GRAVITY = 1; 
    private int velocity; 
int speed = 1;
    public Jumper1() { 
        velocity = 5; 
    }    
    public void act() 
    {
mover();
        fall(); 
        if (Greenfoot.isKeyDown("space") && getY() > getWorld().getHeight() -50) jump();
    }    
    public void fall() { 
        setLocation(getX(), getY() + velocity); 
        if (getY() > getWorld().getHeight() -50) velocity = 5; 
        else velocity += GRAVITY;
    }
    public void jump() {
          
        velocity = -20;  
    }
public void mover() {
       if (Greenfoot.isKeyDown("a"))
       setLocation(getX() - speed, getY());
       if (Greenfoot.isKeyDown("d"))
       setLocation(getX() + speed, getY());
}
}
        
But it is not perfect yet. For exemple, if you keep pressing space than you dont fall down. I have no clue how to solve this. If you any tips than pls help me. Thanks a lot with the best greetings!
danpost danpost

2021/3/26

#
Please verify my presumptions: (1) world cell size is 1; (2) actor created is "new Jumper1()" (and not a instance of a class that extends Jumper1).
PetrusderEchte PetrusderEchte

2021/3/28

#
Hello! Thanks for your help, but i am not good in programming and i dont know what you mean.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die einzigen aktiven Akteure in der Roboterwelt sind die Roboter.
 * Die Welt besteht aus 14 * 10 Feldern.
 */

public class SpielWelt extends World
{
    private static int zellenGroesse = 30;

    /**
     * Erschaffe eine Welt mit 14 * 10 Zellen.
     */
    public SpielWelt()
    {
        super(24, 12, zellenGroesse);
        setBackground("images/spielhintergrund.jpg");
        

     
    }
}      


   
this is my world. The code of the jumper class do you already know. this is all i have
danpost danpost

2021/3/28

#
Remove line 10. Change line 17 to:
super(720, 360, 1);
and add the following at line 19:
addObject(new Jumper1(), 300, 300);
You need to login to post a reply.