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

2018/10/3

Anti-Gravity

Venbha Venbha

2018/10/3

#
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);
        }
    }    
}
Venbha Venbha

2018/10/3

#
I need to edit these. But when i did tried changing the positive integers into negative and vice versa, it gave me something i didnt need...
Super_Hippo Super_Hippo

2018/10/4

#
What exactly is Anti-Gravity? Do you want gravity upside down or something like two magnets which are both positive or negative?
Venbha Venbha

2018/10/5

#
Super_Hippo wrote...
What exactly is Anti-Gravity? Do you want gravity upside down?
The code works for JUMPING. so i want gravity Upside down. Thank you
Super_Hippo Super_Hippo

2018/10/5

#
Like this?
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 = 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")) //maybe change that to "down"
 
        {
            vSpeed = -JUMP_STRENGTH;
        }
         
        if(Greenfoot.isKeyDown("right"))
        {
            move(7);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            move(-7);
        }
    }    
}
You need to login to post a reply.