ok.. I know now how to create gravity in scenarios... but now it occured to me that.. Could I make Anti gravity just like I have been creating gravity? If yes, How? IK theres a way..
Here's my code:
import greenfoot.*;
public class Jumper extends Actor
{
private static final int GRAVITY = 1;
private static final int JUMP_STRENGTH = 20;
private int vSpeed;
public void act()
{
int halfHeight = getImage().getHeight()/2;
int groundLevel = getWorld().getHeight()-halfHeight;
boolean onGround = false;
vSpeed += GRAVITY;
setLocation(getX(), getY()+vSpeed);
if (getY() > groundLevel)
{
onGround = true;
setLocation(getX(), groundLevel);
vSpeed = 0;
}
else
{
Actor obj = getOneIntersectingObject(Obstacle.class);
if (obj != null)
{
int dir = (int)Math.signum(vSpeed);
if (dir > 0) onGround = true;
int objHalfHeight = obj.getImage().getHeight()/2;
setLocation(getX(), obj.getY()-dir*(objHalfHeight+halfHeight));
vSpeed = 0;
}
}
if (onGround && Greenfoot.isKeyDown("up"))
{
vSpeed = -JUMP_STRENGTH;
}
if(Greenfoot.isKeyDown("right"))
{
move(7);
}
if(Greenfoot.isKeyDown("left"))
{
move(-7);
}
}
}
