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

2014/4/3

Platformer Collision Help

AlexFeetham AlexFeetham

2014/4/3

#
So, in the platformer game I'm creating the character cant jump on the same platform as it just falls through it, but jumping to other platforms works fine. It seems to be just when the player attempts to jump on the same platform. I've included the code from my player class. Any help is appreciated =)
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 hSpeed = 5;
    private int vSpeed = 10;
    private int acceleration = 1;
    private int jumpStrength = 15;
    private boolean jumping = false;
    public void act() 
    {
        movement();
        collision();
        fall();
        checkFall();
        deathCheck();
    }
    
    private void movement()
    {
       if(Greenfoot.isKeyDown("a"))
         setLocation ( getX() - hSpeed, getY() );

       if(Greenfoot.isKeyDown("d"))
         setLocation ( getX() + hSpeed, getY() );
       
       if(Greenfoot.isKeyDown("w")&& jumping==false)
       {
           jumping = true;
           vSpeed = -jumpStrength;
           fall();
       }
    }
    
    private void checkFall()
    {
        Actor Under = getOneObjectAtOffset (0,20, SmallPlatformA.class);
        Actor over = getOneObjectAtOffset (0,-20,SmallPlatformA.class);
        if(Under != null)
        {
            vSpeed = 0;
            jumping = false;
        }
        else
        {
            fall();
        }
    }
    
    private void fall()
    {
        setLocation ( getX(), getY() + vSpeed); 
        vSpeed = vSpeed + acceleration;
        
    }
    
    private void collision()
    {
        Actor smallPlatformA1 = getOneObjectAtOffset(0,0,SmallPlatformA.class);
        if(smallPlatformA1 != null)
        {
            jumping = false;
        }
        Actor collide = getOneIntersectingObject(SmallPlatformA.class);
    }
    
    private void deathCheck()
    {
        Actor death = getOneIntersectingObject(DeadZoneBottom.class);
        if(death != null)
        {
            Greenfoot.setWorld(new StartScreen());
        }
    }
}
AlexFeetham AlexFeetham

2014/4/3

#
This has now been fixed.
You need to login to post a reply.