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

2011/5/31

Jumping/Falling Issues.

kdnkdn12 kdnkdn12

2011/5/31

#
For my AP Computer Science final we are supposed to make a game using Greenfoot. My game involves jumping/falling, but I can't get it to work. I've watched the video, but when I try it like he does my person goes through the floor. If I change it, he either keeps going through it, or he doesn't fall at all. And with jumping, they just go straight up and fall, I can't figure out how to make it to where the player has time to move while jumping. Here is the code I've been using.
import greenfoot.*;  

public class Player2  extends Actor
{
    private int vSpeed = 10;
    private int acceleration = 1;
    private int jumpSpeed = 0;
    private boolean goingLeft = true;
    GreenfootImage gr = getImage();
    private int count = 0;

    
    
    public void act() 
    {
        checkKeys();
        checkFall();
        count++;
    }    
    
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("left"))
        {   if(goingLeft)
            {
            gr.mirrorHorizontally();
            goingLeft=false;
            }
            this.setLocation(getX()-2,getY());
        }
        if(Greenfoot.isKeyDown("right"))
        {   if(!goingLeft)
            {
            gr.mirrorHorizontally();
            goingLeft=true;
            }
            this.setLocation(getX()+2,getY());
        }
        if(Greenfoot.isKeyDown("up"))
        {
            jump();
        }
    }
    
    public void checkFall()
    {
        if(onGround()) {
            vSpeed = 0;
        }
        else {
            fall();
        }
    }
    
     public boolean onGround()
    {
       /*if(getX()<gr.getHeight()-100)
       {
           return true;
        }
       return false;*/
       Actor underG = getOneObjectAtOffset(0,1,Ground.class);
       return underG != null;
    }

    
    public void stopfall()
    {
        
    }
    
    public void fall()
    {if(count%2==0){
        if(!onGround())
        {
        vSpeed= vSpeed + 2;
        this.setLocation(getX(), getY() + vSpeed);
        
        }}
    }
    
    public void jump()
    {
        if(onGround())
        {
        jumpSpeed = -60;
       this.setLocation(getX(), getY() + jumpSpeed);
        }
    }
    
    private boolean verSpeed()
    {
        if(vSpeed>=1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
}
Me and my teacher played around with it for a while, so there are some unused variables in there, but if they help you by all means do whatever.
davmac davmac

2011/6/1

#
There's nothing obviously wrong with this code that would cause the problems you describe. Why don't you upload the scenario (with source) and post a link to it here?
kdnkdn12 kdnkdn12

2011/6/1

#
http://greenfootgallery.org/scenarios/3012 There is my scenario, not sure if it uploaded completely right. If that link doesn't work for some reason, it's called Turn in Homework.
davmac davmac

2011/6/2

#
You didn't upload the source code...
mik mik

2011/6/2

#
Not seeing all the details, I'd say that this line looks suspicious: getOneObjectAtOffset(0,1,Ground.class) You check for ground only one cell below your jumping actor. (That is: one cell below the centre point of your actor!) If your cells are small (i.e. one pixel) and if your actor is large (i.e. more than one pixel) then you will detect ground only once the centre of the actor touches the ground object. You need to use a greater y-offset to check near the feet of your actor (for example: getImage().getHeight()/2 ). Also, even if you detect ground correctly, you can still sink some distance into the ground, because your movement is more than one pixel per step. For example, of you are five pixels above ground (so still falling), and your vertical speed is 12 pixels per act round, then in the next frame your already 7 pixels into the ground. First problem: if your ground is thin (thinner than 7 pixels) you might fall straight through it without detecting it. Second problem: Even if the ground is thick enough, you are sunk in by 7 pixels. Fix: Make your ground thick enough. And if you detect that you hit ground, correct the y coordinate to place yourself on the top of the ground.
FallenPhoenix FallenPhoenix

2012/5/10

#
mik wrote...
Not seeing all the details, I'd say that this line looks suspicious: getOneObjectAtOffset(0,1,Ground.class) You check for ground only one cell below your jumping actor. (That is: one cell below the centre point of your actor!) If your cells are small (i.e. one pixel) and if your actor is large (i.e. more than one pixel) then you will detect ground only once the centre of the actor touches the ground object. You need to use a greater y-offset to check near the feet of your actor (for example: getImage().getHeight()/2 ). Also, even if you detect ground correctly, you can still sink some distance into the ground, because your movement is more than one pixel per step. For example, of you are five pixels above ground (so still falling), and your vertical speed is 12 pixels per act round, then in the next frame your already 7 pixels into the ground. First problem: if your ground is thin (thinner than 7 pixels) you might fall straight through it without detecting it. Second problem: Even if the ground is thick enough, you are sunk in by 7 pixels. Fix: Make your ground thick enough. And if you detect that you hit ground, correct the y coordinate to place yourself on the top of the ground.
I believe that getOneObjectAtOffset(0,1,Ground.class) should be getOneObjectAtOffset(0, 1, Ground.class)
You need to login to post a reply.