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

2015/2/14

Can't work out bounding boxes

h123n h123n

2015/2/14

#
Hi, I've been trying to make my physics engine stop the player falling when it gets within a certain radius of grass. I've looked it up but no-one seems to have anything simple. I've tried playing scenarios but my browser crashes on me. Anyone know how it's done? Here's my code:
import greenfoot.*;

/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Person extends Actor
{
    int MotionY = 0;
    int MotionX = 0;
    int MiningLeague = 100;
    boolean Ground = false;
    /**
     * Act - do whatever the Person wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
       setLocation( getX() + MotionX , getY() );
       setLocation( getX(), getY() - MotionY);
       
       //Here's the code that makes you sink through the ground to the center
       Actor grass;
       grass = getOneObjectAtOffset(0, 0, Grass.class);
       if (grass != null)
       {
           MotionY = 0;
           Ground = true;
       }
       else
       {
           MotionY-=1;
           Ground = false;
       }
        if (Greenfoot.isKeyDown("a"))
        {
            //MotionX-=1;
            move(-4);
        }
               if (Greenfoot.isKeyDown("d"))
        {
            //MotionX+=1;
            move(4);
        }
               if (Greenfoot.isKeyDown("space"))
        {
            if (Ground == true)
            {
                MotionY = 15;
                Ground = false;
            }
        }
              if (Greenfoot.isKeyDown("shift"))
        {
            if (Ground == true)
            {
                Actor Gem;
                if (Greenfoot.getRandomNumber(MiningLeague) == 1)
                {
                    World world;
                    world = getWorld();
                    world.addObject(new Gem(), getX(), getY());
                    MiningLeague-=1;
                }
            }
        }
    }    
}
danpost danpost

2015/2/14

#
The Actor class has several collision checking methods available that perform in different ways. Maybe you should check out the Actor class API documentation to see what is available because 'getOneObjectAtOffset' may not be the best one to use in this case.
You need to login to post a reply.