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

2019/6/22

Object keeps stucking at the world-edges.

MisterUnknown MisterUnknown

2019/6/22

#
My player-objects keep getting stuck at the edges of the world, eventhough I set up these couple of codes to prevent it from happening:
    public boolean atWorldLeft()
    {
        if(getX() < 20)
            return true;
        else return false;
    }


    
    public boolean atWorldRight()
    {
        if(getX() > getWorld().getWidth() - 20)
            return true;
        else return false;
    }
    public boolean atWorldTop()
    {
        if(getY() < 20)
            return true;
        else return false;
    }
    
    public boolean atWorldBottom()
    {
        if(getY() > getWorld().getHeight() - 20)
            return true;
        else return false;
    }
As well as: public void wallBounce() { if(atWorldRight()) { xVel *= -1.1; setLocation((int) (getX() + xVel), (int) (getY() + yVel)); } if(atWorldLeft()) { xVel *= -1.1; setLocation((int) (getX() + xVel), (int) (getY() + yVel)); } if(atWorldTop()) { yVel *= -1.1; setLocation((int) (getX() + xVel), (int) (getY() + yVel)); } if(atWorldBottom()) { yVel *= -1.1; setLocation((int) (getX() + xVel), (int) (getY() + yVel)); } } Any ideas of how to fix it?
danpost danpost

2019/6/22

#
The conditions in each of part in wallBounce are incomplete. It is not enough that the player is touching an edge to change direction, the player should be moving toward that edge, also:
public void wallBounce()
{
    if (atWorldTop() && yVel < 0 || atWorldBottom() && yVel > 0) yVel *= -1.1;
    if (atWorldLeft() && xVel < 0 || atWorldRight() && xVel > 0) xVel *= -1.1;
}
MisterUnknown MisterUnknown

2019/6/23

#
How would I make my player move towards that egde and integrate it into my code?
danpost danpost

2019/6/23

#
MisterUnknown wrote...
How would I make my player move towards that egde and integrate it into my code?
I think you misunderstand. The added conditions check for the actor moving toward an edge. You do not want the actor to turn around when it is already facing away from an edge (that it, immediately after it already turned around). So, the added conditions prevent that from happening.
MisterUnknown MisterUnknown

2019/6/23

#
Alright. Thank you
You need to login to post a reply.