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

2016/11/18

Detecting Platform Glitch

gmacias gmacias

2016/11/18

#
For some unknown reason to me, the main player ("Willa" class) does not "walk" directly on top of the Platform objects. Instead, it looks like she is walking through the platform (note: not referring to stacked platforms). Sometimes she does walk directly on top of the platform if I make her jump, this doesn't always fix the problem though. Not sure how to fix this glitch. Any help would be appreciated. Please let me know if I need to post the other code. Thank you!
public class AnimatedActor extends Actor
{
    private int frame = 0;              //current frame
    private String name = "willa";       //base name for image
    private String extension = ".gif";  //file extension
    private int speed = 10;             //speed of animation
    private int speedCounter = 0;       //time to next image change

    public void animate(int first, int last)	//first frame e.g. 0, last frame e.g. 3
    {
        if (speedCounter >= speed)		//Only animate when counter has reached speed
        {
            speedCounter = 0;		//reset counter
            if (frame < first || frame >= last)	//if the frame is outside the first/last range
            {
                frame = first;		//set to first frame
            }
            else
            {
                frame++;			//otherwise add 1 to the frame number
            }
            setImage(name + frame + extension);  //display the next image
        }

        else
        {
            speedCounter++;		//if  counter not up to speed add 1 to counter
        }
    }
}
public class Willa extends AnimatedActor
{
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpHeight = -12;  // how high to jump
   
    /**
     * Act - do whatever the Willa wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        control();
        checkFalling();
    }    
    
    private void control()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            animate (4,5);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            animate (2,3);
        }
        if (Greenfoot.isKeyDown("up")&& (onGround() == true))
        {
            vSpeed = jumpHeight;
            fall();
        }
    }
    private void fall()
    {
        setLocation(getX(),getY()+ vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    private boolean onGround()
    {
        Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
        return under != null;
    }
    
    private void checkFalling()
    {
        if (onGround() == false)
        {
            fall();
        }
    }

}
danpost danpost

2016/11/19

#
When an object is encountered when moving vertically (as well as horizontally, by the way), the position of the actor needs to be adjusted so that it is not constantly "in" the object. Use the value (sign) of 'vSpeed' to determine if the encounter was a head bump or landing on feet. Then you will know where to position the actor with respect to the encountered object.
You need to login to post a reply.