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

2014/10/26

Sliding problem in my 2D Game

MySelfLuls MySelfLuls

2014/10/26

#
I've had tons of problems with trying to figure it out. But when it increases the speed and you release it just slides and dosn't really stop.
        boolean aboutFace;
    boolean onGround;
    boolean atWorldEdge;
    int ySpeed = 0, xSpeed = 0;
    final int jSpeed = 20




private void move()
    {
        ySpeed++; // adds gravity
        if (xSpeed != 0 && onGround) xSpeed +=aboutFace?1:-1; // add friction
        setLocation(getX()+xSpeed/10, getY()+ySpeed/2);
        // check for change in horizontal direction
        if((xSpeed>0 && aboutFace) || (xSpeed<0 && !aboutFace)) 
        {
            getImage().mirrorHorizontally();
            aboutFace = !aboutFace;
        }

        // check for obstacles
        onGround=false; // initialize value
        // check below the actor
        while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
        {
            setLocation(getX(), getY()-1); 
            onGround=true; 
            ySpeed=0;
        }
        // check above the actor
        while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null) 
        {
            setLocation(getX(), getY()+1);
            ySpeed = 0;
        }
        // check to right of actor
        while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
        {
            setLocation(getX()-1, getY());
            xSpeed = 0;
        }
        // check to left of actor
        while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
        {
            setLocation(getX()+1, getY());
            xSpeed = 0;
        }
    }

    public void movement()
    {
        if (Greenfoot.isKeyDown("left")) xSpeed-=2;
        if (Greenfoot.isKeyDown("right")) xSpeed+=2; 
        if (Greenfoot.isKeyDown("up") && onGround) 
    {
        ySpeed -= jSpeed; 
    }
}
danpost danpost

2014/10/26

#
I applied the code to an actor and it slowed to a stop. If you hold down too long, then yes, it will appear to just zip to the edge of the window and stop there even if you release before the edge is reached. There are things you can do to adjust the movement (maybe in the way you want). Maybe apply a maximum value to 'xSpeed' and/or divide by a lower number on line 14, where you use '10', to bring the acceleration and deceleration rates closer together.
MySelfLuls MySelfLuls

2014/10/26

#
danpost wrote...
I applied the code to an actor and it slowed to a stop. If you hold down too long, then yes, it will appear to just zip to the edge of the window and stop there even if you release before the edge is reached.
Yeah it does slow to a stop, but I would like it to stop way faster, but I can't figure it out.
danpost danpost

2014/10/26

#
You could apply more friction (add and subtract 2 instead of 1).
You need to login to post a reply.