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

2017/6/3

PROBLEM-JnR-Player drops through objekts

Jonah Jonah

2017/6/3

#
Hi there, A few friends and me are currently working on a Jump and Run game for school and used existing code for the Player class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Actor
{
    private int speed = 8;
    private int vspeed = 0;
    private int acceleration = 1;
    private int imageheight;
    private int testcount = 0;

    public void act()
    {
        imageheight= getImage().getHeight() / 2;
        checkKeys();
        checkFall();

    }

    private void checkKeys()
    {
        if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("space"))
        {
            jump();
        }

        if(Greenfoot.isKeyDown("a"))
        {
            this.setLocation(getX()- speed,getY());
        }

        if(Greenfoot.isKeyDown("d"))
        {
            this.setLocation(getX()+ speed,getY());
        }
    }

    public boolean onGround()//Methode um zu testen, ob der charakter auf einem objekt steht
    {
        Actor under = getOneObjectAtOffset ( 0, imageheight , Solid.class);
        return under != null;
    }

    public void jump()
    {  
        vspeed = -10;// rückwertung der Fall
        fall();
    }

    public void fall()
    {
        setLocation(getX(),getY()+ vspeed);
        vspeed = vspeed + acceleration;

    }

    public void checkFall()
    {
        if(onGround()){
            vspeed = 0;
        }else{
            //our Idea
            /*if(vspeed>0){
                for(int i =0;i==vspeed || getOneObjectAtOffset ( 0, imageheight+i, Solid.class)!=null;i++){
                    testcount++;
                }
                vspeed=vspeed-testcount;
                testcount=0;
                
            }*/
            fall();
        }

    }
}
The gravity has an acceleration wich counts +1 per "round". It can just test if the player is currently standing on (or in) an Object. So the Problem is that the player most of the time is, because of the steps, in an object and then stopping, but we want it to stop direktly above. So our idea is to test after each "round" each place until the "future position" under the player and then decrease the vertikalspeed (vspeed) to the difference of the current position and the detected-solid-position.
if(vspeed>0){
                for(int i =0;i==vspeed || getOneObjectAtOffset ( 0, imageheight+i, Solid.class)!=null;i++){
                    testcount++;
                }
                vspeed=vspeed-testcount;
                testcount=0;
                
            }
This is the code i wrote, but it doesn't work. Perhaps one of you guys can help me, Sorry for any language or grammar mistakes, Jonah
danpost danpost

2017/6/3

#
Provided there is no excess transparencies in the images of the Solid object and the Player object, you should be able to use:
setLocation(getX(), under.getY()-(getImage().getHeight()+under.getImage().getHeight())/2);
to properly set the player on the solid when found on ground.
Jonah Jonah

2017/6/4

#
I added your code to the onGround() method. Then there is an error:
java.lang.NullPointerException
	at Player.onGround(Player.java:40)
	at Player.checkFall(Player.java:59)
	at Player.act(Player.java:14)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/6/4

#
Jonah wrote...
I added your code to the onGround() method. Then there is an error: < Error Omitted >
Use:
if (under != null) setLocation...
Jonah Jonah

2017/6/25

#
thank you very much. I now have an own idea:
public void checkFall()
    {
        if(onGround()){
            vspeed = 0;
        }else{

            if(vspeed>0){
                //System.out.println(vspeed);

                for(int i =0; i < vspeed+1 ;i++){
                    //System.out.println("vspeed: "+vspeed+"\ti:")
                    if( this.getOneObjectAtOffset ( 0, imageheight+i, Solid.class) instanceof Solid) {
                        vspeed =i;                        
                        break;
                    }
                }
            }

            fall();
        }

    }
it works properly
You need to login to post a reply.