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

2012/6/13

gravity help

tylers tylers

2012/6/13

#
hi ive got this code were i got peice of grass as an actor and got some walls to fall on it. the walls are going to get longer but the first couple of imes it falls it works untell it gets a bit big and it bounces up and down please help? :D this is the wall class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class wall here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class wall extends Actor
{
    private int speed = 2;
    private int vSpeed = 0;
    private int acceleration = 1;
    boolean ground = false;
    int tall;
    public wall(boolean ground,int tall)
    {
        getImage().scale(30,tall);
        this.ground=ground;
        this.tall=tall;
        if(ground)
        {
            GreenfootImage img = getImage();
            img.mirrorVertically();
        }
    }

    /**
     * Act - do whatever the wall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation((int) (getX() + 1.0), (int) (getY() + 0.6));
        if(ground)
        checkFall();
    }    

    public void checkFall()
    {
        if(onGround()) {
            vSpeed = 0;
            setLocation ( getX(),(354+tall));
        }
        else {
            fall();
        }
    }

    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed); 
        vSpeed = vSpeed + acceleration;
    } 
     public boolean onGround()
    {
        Actor under = getOneIntersectingObject(grass.class);
        return under != null;
    }
}
limefortheworld limefortheworld

2012/6/14

#
Bounces up and down? Try on line 43 setLocation (getX(),(354+tall)). It seems like the tall value is the full height of the image.
You need to login to post a reply.