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

2024/8/8

Issues with platform detection

Cyatri Cyatri

2024/8/8

#
When I move a platform, if the player jumps on a moving platform it will phase through the platform, and sometimes when a platform is hit from the side the player phases through the platform. Otherwise, it seems completely fine. Any thoughts on a solution?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
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 dir = 2;
    private boolean inTheAir = false; 
    private int jumpHeight = 10;
    private int walkSpeed = 5; 
    private double fallSpeed = 0.4;
    private double deltaX = 0;
    private double deltaY = 0; 
    private int groundHeight = getImage().getHeight()/2;
    private int sideWidth = getImage().getWidth()/2;
    private World myWorld; 
    int worldHeight;
    int worldWidth; 
    int xPos;
    int yPos;
    private String[] fireboyRight = {"fireboyD.png", "fireboyD1.png", "fireboyD2.png"};
    int currentImage;
    int currentImageU;
    private GreenfootImage myImage = getImage();

    public void addedToWorld(World myWorld)
    {
        this.myWorld = myWorld;
        this.worldHeight = myWorld.getHeight();
        this.worldWidth = myWorld.getWidth(); 
    }
    public void act()
    {
        if(inTheAir)
        {
            fall();
        }
        else
        {
            getCommand();
        }
        checkCollision();
        move();
        animateOnMove();
    }
    private void run(String direction)
    {
        if(direction == "left")
        {
            deltaX = walkSpeed * -1;
        }
        else
        {
            deltaX = walkSpeed;
        }
    }
    private void stop()
    {
        deltaX = 0;
    }
    private void jump()
    {
        deltaY -= jumpHeight;
        inTheAir = true; 
    }
    private void fall()
    {
        deltaY += fallSpeed;
    }
    private void move()
    {
        double newX = getX() + deltaX;
        double newY = getY() + deltaY;
        
        Actor platformBelow = getOneObjectAtOffset((int)deltaX,groundHeight+14 + (int) deltaY, Platform.class);
        Actor platformAbove = getOneObjectAtOffset((int)deltaX,groundHeight-5 + (int) deltaY-60, Platform.class);
        Actor platformToLeft = getOneObjectAtOffset(sideWidth+5 + (int) deltaX,(int) deltaY,Platform.class);
        Actor platformToRight = getOneObjectAtOffset(sideWidth-5 + (int)deltaX,(int) deltaY,Platform.class);
        if(platformBelow!=null)
        {
            deltaY=0;
            inTheAir = false;
            newY = platformBelow.getY()-60;
        }
        else if(getY()>=worldHeight - groundHeight)
        {
            deltaY=0;
            inTheAir = false;
            
        }
        else
        {
            inTheAir = true;
        }
        if(platformAbove!=null)
        {
            if(deltaY<0)
            {
                deltaY=0;
                GreenfootImage platformImage = platformAbove.getImage();
                int bottomOfPlatform = platformAbove.getY()+platformImage.getHeight()/2;
                newY = bottomOfPlatform + groundHeight;
                
            }
        }
        if(getX()<=sideWidth)
        {
            deltaX = Math.abs(deltaX);
        }
        if(getX()>=worldWidth-sideWidth)
        {
            deltaX = Math.abs(deltaX) * -1;
        }
        if(platformToRight!=null)
        {
            deltaX = 0;
        }
        if(platformToLeft!=null)
        {
            deltaX = 0;
        }
        setLocation((int)newX,(int)newY);
    }
    private void getCommand()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            run("left");
        }
        else if(Greenfoot.isKeyDown("right"))
        {
            run("right");
        }
        else
        {
            stop();
        }
        
        
        if(Greenfoot.isKeyDown("up"))
        {
            jump();
        }
        
    }
    private void checkCollision()
    {
        if(isTouching(Fire.class))
        {
            removeTouching(Player2.class);
        }
    }
    
    private void animateOnMove()
    { 
        if(getX()!=xPos)
        {
            currentImage++;
            if(currentImage>=fireboyRight.length * 4)
            {
                currentImage=0;
            }
            
            
            GreenfootImage newImage = new GreenfootImage(fireboyRight[currentImage/4]);
            if(getX()<xPos)
            {
                newImage.mirrorHorizontally();
            }
            
            setImage(newImage);
            myImage=getImage();
        }
        else
        {
            setImage("Fireboy.png");
        }
        xPos = getX();
        yPos = getY();
    }
}
danpost danpost

2024/8/12

#
You are trying to use double values to track the coordinates of the actor more precisely. However, a lot of accuracy is lost with lines 80 and 81. You are adding an int value to the doubles, deltaX and deltaY. You need to keep track of the location coordinates of the actor in double fields. Also, you are not adjusting the position of the actor when colliding with a platform while moving horizontally. Either one or both of these could lead to the unwanted behavior you describe.
You need to login to post a reply.