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

2016/1/28

Actor gets stuck in platform after he jumps from ground to platform.

BreakingKratos BreakingKratos

2016/1/28

#
Hello everyone I have a problem with my game code. I have an actor (= player1) he can move left and right, when he stands still he does an animation too. When my actor jumps from one platform to another, he gets stuck in the middle of the platform. I have a method fallDown in my player class, I call it after my Actor jumps, so he falls down directly. When i jump from the bottom floor and i land back on it, it give no problem. The problem only appears when i jump from platform to platform. Here is my code for my player1: Also , don't pay attention to the arrays and the 'getIndex' methods, they are only used for the image animation.

public class Player1 extends Actor
{
    private String[] walkLeft = {"Player1/left/left0.png","Player1/left/left1.png",
            "Player1/left/left2.png","Player1/left/left3.png"};
    private String[] walkRight = {"Player1/right/right0.png","Player1/right/right1.png",
            "Player1/right/right2.png","Player1/right/right3.png"};
    private String[] stopLeft = {"Player1/stopLeft/stopLeft0.png","Player1/stopLeft/stopLeft1.png",
            "Player1/stopLeft/stopLeft2.png","Player1/stopLeft/stopLeft3.png"};                            
    private String []stopRight = {"Player1/stopRight/stopRight0.png","Player1/stopRight/stopRight1.png",
            "Player1/stopRight/stopRight2.png","Player1/stopRight/stopRight3.png"};

    private int counterLeft,counterRight,counterStopLeft,counterStopRight;

    private int verticalSpeed, acceleration;
    
    private int jumpCooldown,jumpReady;

    private String directionFaced;

    private boolean moved;

    public Player1()
    {
        counterLeft = 0;
        counterRight = 0;

        counterStopLeft = 0;
        counterStopRight = 0;

        verticalSpeed = 0;
        acceleration = 1;
        
        jumpCooldown = 30;
        jumpReady = 0;

        moved = false;

        setImage("Player1/stopRight/stopRight0.png");
    }

    public void act() 
    {
        checkKeys();
        animation();
        checkFallDown();  
        checkForWall();
        jumpReady = jumpReady + 1;
    }

    private String getIndexWalkLeft(int index)
    {
        if(index >= 0 && index < walkLeft.length)
        {
            return walkLeft[index];
        }
        else
        {
            return"";
        }
    }

    private String getIndexWalkRight(int index)
    {
        if(index >= 0 && index < walkRight.length)
        {
            return walkRight[index];
        }
        else
        {
            return"";
        }
    }

    private String getIndexStopLeft(int index)
    {
        if(index >= 0 && index < stopLeft.length)
        {
            return stopLeft[index];
        }
        else
        {
            return"";
        }  
    }

    private String getIndexStopRight(int index)
    {
        if(index >= 0 && index < stopRight.length)
        {
            return stopRight[index];
        }
        else
        {
            return"";
        }
    }

    public void animation()
    {
        if(!Greenfoot.isKeyDown("left")&& directionFaced == "left")
        {
            stopLeft();
        }
        if(!Greenfoot.isKeyDown("right") && directionFaced == "right")
        {
            stopRight();
        }
    }

    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
            walkLeft();
            moved = true;
            directionFaced = "left";
        }

        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
            walkRight();
            moved = true;
            directionFaced = "right";
        }
        if(Greenfoot.isKeyDown("space") && jumpReady >= jumpCooldown)
        {
            jump();
            jumpReady = 0;
        }
        else
        {
            moved = false;
        }

    }
    
    public void checkFallDown()
    {
        if(isOnPlatform())
        {
            verticalSpeed = 0;
        }
        else
        {
            fallDown();
        }
    }
    
    public void checkForWall()
    {
        
    }
    

    public void jump()
    {
        verticalSpeed = -10;
        fallDown();
    }

    public void fallDown()
    {
        setLocation(getX(), getY() + verticalSpeed);
        verticalSpeed = verticalSpeed + acceleration;
    }

    /**
     * Bij onderstaande methode ga ik controleren of de huidige actor op een platform staat.
     * 'under' gebruik ik omdat ik specifier naar de positie.
     *  Op de plaats '0' staat het x-coordinaat ten opzichte van de locatie van het object.
     *  'under' is hier het object dat getourneerd wordt als er zich iets werkelijk,
     *  onder de voeten van mijn actor bevindt.
     *  Als er niks onder mijn actor kom te staan, dan zal deze methode een 'null' retourneren.
     *  i.p.v. het object under.
     *  
     */
    public boolean isOnPlatform()
    {
        Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2, Platforms.class);
        return under != null;
    }
    
    /**
     * animatie die uitgevoerd wordt als de speler naar links loopt.
     */
    public void walkLeft()
    {
        if(counterLeft%10 == 0)
        {
            setImage(getIndexWalkLeft(counterLeft/10));
        }
        counterLeft = (counterLeft + 1)%40;
    }

    /**
     * animatie als de speler naar rechts loopt.
     */
    public void walkRight()
    {
        if(counterRight%10 == 0)
        {
            setImage(getIndexWalkRight(counterRight/10));
        }
        counterRight = (counterRight + 1)%40;
    }

    public void stopLeft()
    {
        if(counterStopLeft%22 == 0)
        {
            setImage(getIndexStopLeft(counterStopLeft/22));
        }
        counterStopLeft = (counterStopLeft + 1)%88;
    }

    public void stopRight()
    {
        if(counterStopRight%22 == 0)
        {
            setImage(getIndexStopRight(counterStopRight/22));
        }
        counterStopRight = (counterStopRight + 1)%88;
    }
}

danpost danpost

2016/1/29

#
Being the vertical movement is variable, your actor could end up anywhere on (or in) a platform while rising or falling. There is nothing in your code to correct the placement of your player when found to be intersecting a platform.
BreakingKratos BreakingKratos

2016/1/29

#
I did like you said, i have created a method which checks if my main actor sinks into the platform. When the actor gets stuck in the middle of the platform, he automatically rises to the top of the platform. What only matters now, is that the way he does this, is graphical not very nice.
public void checkSinking()
    {
        Actor player1 = getOneIntersectingObject(Platforms.class);
        if(player1 != null)
        {
            setLocation(getX(), getY()-1);
        }
    }

danpost danpost

2016/1/29

#
With the code you are showing, it may take quite a few act cycles for the player to rise above (or onto) the platform. Changing the 'if' to a 'while' would cause the repeated rising to occur at once. The value of the variable ('player1' -- which is not a good name for a Platforms object), however, will need to be reassessed within the loop so that the condition will eventually be false and the loop can be broken out of.
You need to login to post a reply.