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

2020/7/2

How to jump like in "Geometry Dash" all the way to the top.

Rullilynn Rullilynn

2020/7/2

#
So I recently started coding in Greenfoot and I want to make an endless running game. How to jump (say I press space) and my character jumps all the way to the top and stays there. Then, if I press space again, it would drop again to the bottom. Any help would be appreciated. Youtube at 30seconds.
danpost danpost

2020/7/2

#
Rullilynn wrote...
So I recently started coding in Greenfoot and I want to make an endless running game. How to jump (say I press space) and my character jumps all the way to the top and stays there. Then, if I press space again, it would drop again to the bottom. Any help would be appreciated. << Link Omitted >>
It looks like there are two things going on in the video. First, you have normal jumping with gravity. Then, you have jumping with the gravity reversed. So, you just need to split apart the components of gravity (into magnitude and direction). Then use the direction value (of '1' or '-1') in your jump codes so you can "jump" on ceilings when gravity is reversed as you would on platforms when not reversed.
Rullilynn Rullilynn

2020/7/2

#
If I want to make it simple, like I don't have to "jump" then fall, I only want to "jump" to go up and then "jump" again to go down. How would I code that? I tried using isKeyDown and setLocation but it only go up if I hold it. I only need to press "jump" once to go up. I'm sorry if I confuse you, I'm still a newbie at this sort of thing. Thanks for replying.
danpost danpost

2020/7/2

#
Rullilynn wrote...
If I want to make it simple, like I don't have to "jump" then fall, I only want to "jump" to go up and then "jump" again to go down. How would I code that? I tried using isKeyDown and setLocation but it only go up if I hold it. I only need to press "jump" once to go up. I'm sorry if I confuse you,
Show current codes.
Rullilynn Rullilynn

2020/7/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @Rully Jehian
 * @1.0
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int GRAVITY = 1;
    private int velocity;
    private boolean spaceDown;
    
    public Player()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth()/3, image.getHeight()/3); //250x610y position
        velocity = 0;
    }
    public void act() 
    {
        movement();
        fall1();
        fall2();
    } 
    public void movement()
    {
        int x = getX();
        int y = getY();
        
        if(Greenfoot.isKeyDown("W") && isOnPlatformBot())
        {
            jump1();
            GRAVITY = -1;
        }
        if(Greenfoot.isKeyDown("S") && isOnPlatformTop())
        {
            jump2();
            GRAVITY = 1;
        }
        setLocation(x, y);
    }
    public void fall1()
    {
        setLocation(getX(), getY() + velocity);
        if (isOnPlatformBot()) velocity = 0;
        else velocity += GRAVITY;
    }public void fall2()
    {
        setLocation(getX(), getY() + velocity);
        if (isOnPlatformTop()) velocity = 0;
        else velocity += GRAVITY;
    }
    public void jump1()
    {
        velocity = -7;
    }
    public void jump2()
    {
        velocity = 7;
    }
    public boolean isOnPlatformBot()
    {
        boolean isOnPlatformBot = false;
        if(getY() >= getWorld().getHeight() - 110) isOnPlatformBot = true;
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneIntersectingObject(Platform.class) != null)
        isOnPlatformBot = true;
        return isOnPlatformBot;
    }
    public boolean isOnPlatformTop()
    {
        boolean isOnPlatformTop = false;
        if(getY() <= getWorld().getHeight() - 610) isOnPlatformTop = true;
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneIntersectingObject(Platform.class) != null)
        isOnPlatformTop = true;
        return isOnPlatformTop;
    }
}
So far I'm able to make it "W" to go up and "S" to go down. But it seems to clip my platform and go through it. I read your comments a lot on others' problem which is a great help. I think I need to put vSpeed to stop my Player if it collide with my platform? But I'm not entirely sure, Thanks for the help.
danpost danpost

2020/7/4

#
I think you just need to set the location of the player when a platform collision occurs.
Rullilynn Rullilynn

2020/7/4

#
Sorry for the late reply, Greenfoot website went down or is it just me? Anyway since I couldn't open any discussion in Greenfoot, I actually did think what you just said and made it so. Here is the final result.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @Rully Jehian
 * @1.0
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int velocity;
    boolean turns = true;
    GreenfootImage bit = new GreenfootImage("P1-8bit.png");
    GreenfootImage dragon = new GreenfootImage("P1-dragon.png");
    GreenfootImage personal = new GreenfootImage("P1-personal.png");
    GreenfootImage purp = new GreenfootImage("P1-purp.png");
    GreenfootImage rainbow = new GreenfootImage("P1-rainbow.png");
    GreenfootImage white = new GreenfootImage("P1-white.png");
    GreenfootImage square = new GreenfootImage("P1-square.png");
    
    GreenfootSound myMusicJump = new GreenfootSound("Cute Jump Sound Effect.mp3");  
    GreenfootSound myMusicJumpdown = new GreenfootSound("Cute Jump Sound Effect down.mp3");
    public Player()
    {
        //250x610y position
        
        velocity = 0;
    }
    public void act() 
    {
        selection();
        movement();
        turning();
        fall();
        isOnPlatformBot();
        isOnPlatformTop();
        hitByEnemies();
    } 
    public void selection()
    {
        if(Greenfoot.isKeyDown("1")) setImage(white);;
        if(Greenfoot.isKeyDown("2")) setImage(bit);
        if(Greenfoot.isKeyDown("3")) setImage(dragon); 
        if(Greenfoot.isKeyDown("4")) setImage(personal);
        if(Greenfoot.isKeyDown("5")) setImage(purp);
        if(Greenfoot.isKeyDown("6")) setImage(rainbow);
        if(Greenfoot.isKeyDown("7")) setImage(square);
    }
    public void movement()
    {
        int x = getX();
        int y = getY();
        
        if(Greenfoot.isKeyDown("W") && isOnPlatformTop() == false)
        {
            jump1();
            turns = false;
            myMusicJump.play();
            myMusicJump.setVolume(25);
        }
        if(Greenfoot.isKeyDown("S") && isOnPlatformBot() == false)
        {
            jump2();
            turns = true;
            myMusicJumpdown.play();
            myMusicJumpdown.setVolume(25);
        }
        if(Greenfoot.isKeyDown("A") && isOnPlatformBot() == true)
        {
            x -= 3;
            turns = true;
            turn(-3);
        }
        if(Greenfoot.isKeyDown("A") && isOnPlatformTop() == true)
        {
            x -= 3;
            turns = false;
            turn(3);
        }
        if(Greenfoot.isKeyDown("D") && isOnPlatformBot() == true)
        {
            x += 3;
            turns = true;
            turn(3);
        }
        if(Greenfoot.isKeyDown("D") && isOnPlatformTop() == true)
        {
            x += 3;
            turns = false;
            turn(-3);
        }
        setLocation(x, y);
    }
    public void turning()
    {
        if(turns == true)
        {
            turn(9);
        }
        else
        {
            turn(-9);
        }
        
    }
    public void fall()
    {
        setLocation(getX(), getY() + velocity);
        if (isOnPlatformBot()) velocity = 0;
        else if(isOnPlatformTop()) velocity = 0;
    }
    public void jump1()
    {
        velocity = -10;
    }
    public void jump2()
    {
        velocity = 10;
    }
    public boolean isOnPlatformBot()
    {
        boolean isOnPlatformBot = false;
        if(getY() >= getWorld().getHeight() - 110) isOnPlatformBot = true;
        return isOnPlatformBot;
    }
    public boolean isOnPlatformTop()
    {
        boolean isOnPlatformTop = false;
        if(getY() <= getWorld().getHeight() - 610) isOnPlatformTop = true;
        return isOnPlatformTop;
    }
    public void hitByEnemies()
    {
        Actor enemies = getOneIntersectingObject(Enemies.class);
        if(enemies != null || getX()>=1320)
        {
            Greenfoot.setWorld(new GameOver());
        }
    }  
}
Thank you for everything
Rullilynn Rullilynn

2020/7/4

#
I removed the entire platform since it was useless and made my actor go the other way if it reaches certain coordinate.
You need to login to post a reply.