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;
}
}
}
}
}
