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

2019/1/31

Why does my character fall through the world?

PostieMalonie PostieMalonie

2019/1/31

#
My character previously jumped but now he just falls. I've played around with timers in order to get him to jump and stay in the air for a while but he instantly fell when leaving the ground. Pls help.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is the character in which the player controls.
 * Within this class, most of the interactive features can be found.
 * This class contains code which reacts to the "BadGuy" class and the "Bullet" Class.
 */
public class Main2 extends Character
{
    GifImage Idle = new GifImage("Frame0.gif"); // Grabs GIF files and plays them when certain buttons are pressed.
    GifImage WalkRight = new GifImage("Walk0.gif");
    GifImage WalkLeft = new GifImage("WalkL0.gif");
    GifImage Fire = new GifImage("FireFrameRight.gif");
    GifImage FireLeft = new GifImage("FireFrameLeft.gif");
    GifImage Jump = new GifImage("Jump.gif");
    private GreenfootImage[] images=new GreenfootImage[27];
    private int jeda=10,num=0,deltax=30,speed=10;
    private int timer;
    private int vSpeed = 10;
    private int acceleration = 10;
    private int jumpStrength = 18;

    private int jtimer = 0;
    //public GreenfootImage image = getImage();

    /**
     * After "Run" is clicked, the 'checkKeys' function constantly checks for what keys are being pressed
     * and if the key pressed has a corresponding function to it.
     */
    public void act()  //Event Loop, this is a loop as it's constantly checking for something to happen.
    {
        for(int i=0;i<1;i++) {
            setImage(Idle.getCurrentImage()); // Changes the image of the actor in order to create an animation.
        } 
        timer++;
        jtimer++;
        fall();
        checkFall();
        checkKeys();
    }  

    /**
     * Whenever key 'A' is held, the actor will move left. When holding 'Shift' whilst also holding 'A' this will increase speed. This will also play an animation.
     * Whenever key 'D' is held, the actor will move right. When holding 'Shift' whilst also holding 'D' this will increase speed. This will also play an animation.
     * Whenever the space bar is pressed, the actor will jump and then fall after a certain period of time.
     * Whenever key 'E' is pressed, a bullet will spawn on the right and an animation will play.
     * Whenever key 'Q' is pressed, a bullet will spawn on the left and an animation will play.
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("e")) //Trigger Function
        {
            for(int i=0;i<1;i++) {
                setImage(Fire.getCurrentImage());
            } 
            shoot();
        }    
        if (jtimer > 0)
        {
            if (Greenfoot.isKeyDown("space") ) //Trigger Function
            {
                for(int i=0;i<1;i++) {
                    setImage(Jump.getCurrentImage());
                    jump();
                    jtimer++;
                }
                if (jtimer > 0)
                {
                    jtimer = 0;
                } 
            } 
        }
        if ( Greenfoot.isKeyDown("d") ) //This is a trigger function because it responds to user input.
        {
            for(int i=0;i<8;i++) {
                setImage(WalkRight.getCurrentImage());
                move(1);
            }
        }
        if ( Greenfoot.isKeyDown("a")) //Trigger Function
        {
            for(int i=0;i<8;i++) {
                setImage(WalkLeft.getCurrentImage());
                move(-1);
            }
        }
        if (Greenfoot.isKeyDown ("shift") && Greenfoot.isKeyDown("d")) //Trigger Function
        {
            move (3);
        } 
        if (Greenfoot.isKeyDown ("shift") && Greenfoot.isKeyDown("a")) //Trigger Function
        {
            move (-3);
        } 
        if (Greenfoot.isKeyDown ("q")) //Trigger Function
        {
            for(int i=0;i<1;i++) {
                setImage(FireLeft.getCurrentImage());
            }
            shootleft();
        } 
    }

    /**
     * This allows the player to run and jump at the same time. This will also play an animation when jumping.
     * After the jump has been activated after a certain period of time, it will begin to fall.
     * When the spacebar is pressed the actor's x and y coordinates will increase creating a jumping animation.
     */
    public void jump()
    {
        vSpeed = jumpStrength;
        fall();
        setImage(images[0]); 
    }

    /**
     * When pressing "Q", this will add a bullet into the world which will travel in the left direction.
     */
    public void shootleft()
    {
        if (timer > 30){
            getWorld() .addObject(new LeftBullet(), getX(), getY());
            timer = 0;
        }
    }  

    /**
     * Event Loop, constantly checking for if the character is on the "Ground" class.
     */
    public boolean onGround()
    {
        // Actor under = getOneObjectAtOffset( 0, getImage().getHeight() / 2-8, GroundMain.class);
        Actor under = getOneIntersectingObject(GroundMain.class);
        return under != null;
    }

    /**
     * Event Loop, constantly checking for if the characters X and Y coordinates to check if the character is in the air.
     * If true, the character will fall and stop falling when touching "Ground" class.
     */
    public void checkFall() 
    {
        if(onGround()) {
            vSpeed = 0;
        }   
        else {
            fall();
        }
    }

    public void shoot()
    {
        if (timer > 30){
            getWorld() .addObject(new Bullet(), getX(), getY());
            timer = 0;
        }
    }  

    /**
     * When the player is in the air, the x and y coordinates will decrease and an animation will play.
     */
    public void fall()
    {
        setLocation (getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
}
danpost danpost

2019/1/31

#
The fall method is being called from the checkFall method, so you can remove line 37 -- and you should not need any timers. Hopefully, that will help. Of note, your "single" loops (of iteration count 1) starting:
for (int i=0;i<1;i++)
can all be removed. Just keep what is inside (moved to the next outer block). Your other loops starting:
for (int i=0;i<8;i++)
does little for setting the current image. Getting it once is enough. It will not cause the whole image set to animate. It will only get what image is supposed to be shown at that time.
PostieMalonie PostieMalonie

2019/2/3

#
Thank you Danpost, I've encountered another issue. The jump works perfectly when moving left or right but if standing still it will make my actor disappear then he falls from the sky. Also if holding the space bar he jumps forever, how do I fix this? Thanks.
danpost danpost

2019/2/4

#
PostieMalonie wrote...
The jump works perfectly when moving left or right but if standing still it will make my actor disappear then he falls from the sky. Also if holding the space bar he jumps forever, how do I fix this?.
As far as holding the space, you probably need to check the on-ground state before jumping (at line 64). Again, the timer (jtimer) is probably not needed. I am not yet sure about the idle jumping. Maybe the above fix will take care of that.
nccb nccb

2019/2/4

#
Your jumpStrength is a positive number, which means the character jumps downwards. You need to make jumpStrength negative in order to jump upwards. Your jtimer does not work correctly because you increase it in act, and then check if it's above zero later on in checkKeys, which it always will be. You probably want to change the threshold for jtimer in checkKeys to be more like 30 to make a longer delay between possible jumps. (You may also want to only allow jumping if onGround() returns true; this the simplest way to prevent infinite jumping in the air by holding down the key.)
PostieMalonie PostieMalonie

2019/2/6

#
What line would that be on nccb?
danpost danpost

2019/2/7

#
PostieMalonie wrote...
What line would that be on nccb?
nccb wrote...
Your jumpStrength is a positive number, which means the character jumps downwards. You need to make jumpStrength negative in order to jump upwards.
(line 21 -- along with line 111)
Your jtimer does not work correctly because you increase it in act, and then check if it's above zero later on in checkKeys, which it always will be. You probably want to change the threshold for jtimer in checkKeys to be more like 30 to make a longer delay between possible jumps.
(line 36 -- along with line 58 and again unnecessarily, I believe, at line 67)
(You may also want to only allow jumping if onGround() returns true; this the simplest way to prevent infinite jumping in the air by holding down the key.)
(the current, and only, condition for jumping is at line 60)
You need to login to post a reply.