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

2022/1/19

Help with platform game!

cake_9098 cake_9098

2022/1/19

#
Hi, so I'm making a pretty simple platform game with 3 different types of platforms and 1 moving platform. The problem I'm having is that when I'm jumping to other platforms, when I sometimes go off the platform the "gravity" does not work like my actor will just be walking on air. It seems to be happening with 2 platforms only and I'm not sure why.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Riggy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Riggy extends Actor
{


    private int vSpeed = 0;
    private int acceleration = 1;

    /**
     * Riggy Constructor
     *
     */
    public Riggy()
    {

    }

    /**
     * Act - do whatever the Riggy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        checkKeys();
        onLadder(); 
        fall();

    }

    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            move(-5);
        }

        if(Greenfoot.isKeyDown("right"))
        {
            move(5);
        }

        if(Greenfoot.isKeyDown("space") && onPlatform1() || Greenfoot.isKeyDown("space") && onPlatform2() || Greenfoot.isKeyDown("space") && onPlatform3() || Greenfoot.isKeyDown("space") && onmovingPlatform())
        {
            jump();
        }

    }

    public void jump()
    {
        vSpeed = -10; 

    }

    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(onPlatform1())
        {   
            vSpeed = 0;
        } else if (onPlatform2()) {
            vSpeed = 0;
        } else if(onPlatform3()) {
            vSpeed = 0;
        } else if (onmovingPlatform()) {
            vSpeed = 0;
        } else {
            vSpeed = vSpeed + acceleration;
        }

    }
    

    public boolean onPlatform1()
    {
        Actor under = getOneObjectAtOffset(0 , getImage().getHeight()/ 2 - 8, platform1.class);
        return under != null;

    }

    public boolean onPlatform2()
    {
        Actor under = getOneObjectAtOffset(0 , getImage().getHeight()/ 2 - 8, platform2.class);
        return under != null;

    }

    public boolean onPlatform3()
    {
        Actor under = getOneObjectAtOffset(0 , getImage().getHeight()/ 2 - 8 , platform3.class);
        return under != null;

    }

    public boolean onmovingPlatform()
    {
        Actor under = getOneObjectAtOffset(0 , getImage().getHeight()/ 2 - 8, movingPlatform.class);
        return under != null;

    }
    

    public void onLadder()
    {
        ladder ld = (ladder)getOneIntersectingObject(ladder.class);
        if(ld != null && Greenfoot.isKeyDown("up"))
        {   
            vSpeed = 0;
            setLocation(getX(), getY() - 5);

        }
    }

}
I'll post my code but it might be a bit tricky to understand. hopefully someone can help me! thanks.
danpost danpost

2022/1/19

#
Make sure your platform images do not have any excess transparencies in them. The player is considered "on" a platform if it touches any part of the image of the platform, including any transparent part of the image. Neither should the image of the player have any excess transparencies.
You need to login to post a reply.