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

2022/1/22

how to make a jump last longer

cake_9098 cake_9098

2022/1/22

#
im trying to make a jumping game and i wanted to know how can i make my actor stay in the air longer after they jump (pressing spacebar). ive written the basic code for jumping and gravity and it all works well but im just looking for some code that will allow the actor to stay in the air longer while jumping, before it comes back down. thanks.
Super_Hippo Super_Hippo

2022/1/22

#
How does your code look like? Maybe you can just lower a gravity constant?
cake_9098 cake_9098

2022/1/22

#
Super_Hippo wrote...
How does your code look like? Maybe you can just lower a gravity constant?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mouse here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mouse extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpHeight = -8;


    /**
     * Act - do whatever the Mouse wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        fall();
        checkSpace();

    }  

    public void checkSpace()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            vSpeed = jumpHeight;
            fall(); 
           

        }

    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
}
cake_9098 cake_9098

2022/1/22

#
Super_Hippo wrote...
How does your code look like? Maybe you can just lower a gravity constant?
if i lower it to a negitve, gravity then works in the opposite direction.
danpost danpost

2022/1/23

#
cake_9098 wrote...
if i lower it to a negitve, gravity then works in the opposite direction.
There are two ways to make your actor stay in the air longer. One is just to make it jump higher. The other is to lower the effect of gravity while maintaining it as a positive value. Here, you will have to decrease the jump force to compensate for the change in height achieved. Since you cannot keep gravity positive by decreasing the integer value of one, you will need to start using partial pixel values. This is done by tracking and using the location coordinates of the actor as either double values or as integer units of some fraction of a pixel. Use the SmoothMover class provided by greenfoot to utilize double values; or, use my QActor class provided in my Asteroids w/Improved QActor class scenario to utilize sub-pixel units. Also, interestingly enough, I have a support subclass of QActor called GQActor which implements gravity and includes methods for jumping. It has a lot of features. You can find it here.
You need to login to post a reply.