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

2024/8/25

Character getting inside platforms

CowboyIggy CowboyIggy

2024/8/25

#
I did a moving platform:
public MovingAreia(int distancia)
    {
        this.distancia = distancia;
        this.limiteContagem = distancia*2;
    }
    
    public void act()
    {
        contagem++;
        movementX();
        movementY();
        resetContagem();
    }
    
    public void movementY()
    {
        if(contagem < distancia)setLocation(getX(), getY() + vSpeed);
        if(contagem > distancia)setLocation(getX(), getY() - vSpeed);
    }
    
    public void movementX()
    {
        if(contagem < distancia)setLocation(getX() + speed, getY());
        if(contagem > distancia)setLocation(getX() - speed, getY());
    }
    
    public void resetContagem(){if(contagem == limiteContagem)contagem=0;}
But I'm having an issue when my character walks on it, when it moves horizontally the char isn't moved together with the platform but that's ok since the character can move on its on. The real problem is when I try to move on a platform that is moving vertically cause my character does not move within the movement of the platform so when the platform goes up my character stays the same place and then get inside the block of platform. My character code:
public boolean onGround()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Plataforma.class);
        return under != null;
    }
    
    public void checkFall()
    {
        if(onGround()){
            vSpeed =0;
        } else
        {
            fall();
        }
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    public void moveRight()
    {
        setLocation(getX() + speed, getY());
    }
    
    public void moveLeft()
    {
        setLocation(getX() - speed, getY());
    }
Can Somebody help me?
danpost danpost

2024/8/26

#
CowboyIggy wrote...
I did a moving platform: << Code Omitted >> But I'm having an issue when my character walks on it, when it moves horizontally the char isn't moved together with the platform but that's ok since the character can move on its on. The real problem is when I try to move on a platform that is moving vertically cause my character does not move within the movement of the platform so when the platform goes up my character stays the same place and then get inside the block of platform. My character code: << Code Omitted >>
When a collision occurs between the moving platform and the player, what are you doing to make sure they are not overlapping by the end of the act step? That means anytime either moves, you must check for collisions and compensate for any overlapping. As far as the platform moving the player upward, you may need to have the platform check to see if the player is not obstructed above and end game if player is "pressed", otherwise move the player also. A method in the player could be added to check for the "squeeze" and end the game should a "squeeze" occur; otherwise, adjust the player's position so that it is "above" the platform and not "on" or "in" it.e is no overlapping. This player instance method would be called by a platform when moving upward and colliding with player.
You need to login to post a reply.