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

2019/5/14

Jumping/Falling Problems

Blaka.Flaka Blaka.Flaka

2019/5/14

#
public void jump()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
         if (!onGround) //in middle of jump
        {
            ySpeed++; //adds gravity effect
            setLocation(getX(), getY() + ySpeed);
            if (getY() >= groundLevel)
            {
                setLocation(getX(), groundLevel);
                Greenfoot.getKey();
            }
            
        }
         else
        {
            if (Greenfoot .isKeyDown(controls))
            {
                ySpeed = -15;
                setLocation(getX(), getY() + ySpeed);
            }
        }
    }
    
     public void checkFall()
    {
         if (onGround())
        {
            vSpeed = 0;
        }
         else
        {
            fall();
        }
    }
    
     public boolean onGround()
    {
     Actor under = getOneIntersectingObject (Ground.class); 
     return under != null;
    }
Blaka.Flaka Blaka.Flaka

2019/5/14

#
my players fall through my ground class at the beginning
danpost danpost

2019/5/14

#
See if using ">=" on line 4 helps at all.
Blaka.Flaka Blaka.Flaka

2019/5/14

#
error pops up saying expecting ";" and that "==" is not a statement
danpost danpost

2019/5/14

#
Blaka.Flaka wrote...
error pops up saying expecting ";" and that "==" is not a statement
Show what you tried.
Blaka.Flaka Blaka.Flaka

2019/5/14

#
public void jump()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        [u]boolean onGround >= (getY() == groundLevel);[/u]
         if (!onGround) //in middle of jump
        {
            ySpeed++; //adds gravity effect
            setLocation(getX(), getY() + ySpeed);
            if (getY() >= groundLevel)
            {
                setLocation(getX(), groundLevel);
                Greenfoot.getKey();
            }
            
        }
         else
        {
            if (Greenfoot .isKeyDown(controls))
            {
                ySpeed = -15;
                setLocation(getX(), getY() + ySpeed);
            }
        }
    }
    
     public void checkFall()
    {
         if (onGround())
        {
            vSpeed = 0;
        }
         else
        {
            fall();
        }
    }
    
     public boolean onGround()
    {
     Actor under = getOneIntersectingObject (Ground.class); 
     return under != null;
    }
    
     public void fall()
    {
         if(getY() < 303)
        {
            setLocation (getX(), getY() + vSpeed);
        }
         else
        {
            vSpeed = vSpeed + acceleration;
            vSpeed = 0;
            setLocation(getX(), 30);
        }
        
    }
    
     public void moveRight()
    {
        
         if (Greenfoot.isKeyDown(Rcontrols))
        {
            setLocation (getX() + speed, getY());
        }
    }
    
     public void moveLeft()
    {
        
         if (Greenfoot.isKeyDown(Lcontrols))
        {
            setLocation (getX() - speed, getY());
        }
    }
danpost danpost

2019/5/14

#
You were supposed to change the "==" to ">=".
Blaka.Flaka Blaka.Flaka

2019/5/14

#
oh ok
Blaka.Flaka Blaka.Flaka

2019/5/14

#
they still fall through
danpost danpost

2019/5/14

#
Blaka.Flaka wrote...
they still fall through
I would hate to ask where you found the code you are using. I was never much fond of using checkFall, onGround and fall methods (everyone seems to run into problems when using them). Please refer to my Jump and Run Demo w/Moving Platform scenario to see how I would code it.
Blaka.Flaka Blaka.Flaka

2019/5/14

#
i got the code from videos we watched in class to make jumping animations heres the link to the videos: there is a bunch of other videos too, so you need to scroll through them
danpost danpost

2019/5/14

#
Blaka.Flaka wrote...
i got the code from videos we watched in class to make jumping animations heres the link to the videos: << Link Omitted >>
My problem with the code used in the given videos is that gravity is made to only exist when the actor is not on ground, where actually, gravity is always present and there is always a downward pull on the actor. This is constantly being balanced by any object it may be standing on. Looking at what you have so far, I notice that the onGround method will return true any time the actor intersects a Ground object. This may not always be the case. If your actor jumps and "bumps" its head on a Ground object, it would intersect it, but not be standing on it. Also, you may have noticed that there was a problem at the end of the videos where the actor "sank" into the lower Ground object a bit. A correction in the vertical positioning is needed to have the actor stop at the proper place when "hitting" the Ground object (both when rising and when falling). For vertical motion in my demo, the first thing I did was to increase the vertical speed (apply gravitational acceleration) and fall. Then I checked for intersecting ground (or at bottom edge) and did what needed depending on the results of the checks. The sign of the vertical speed field is used to determine whether the actor was "landing on top of ground" or "bumping head on the underside of ground". The last thing done was the check for initiating a jump, after ground checking was done and the on-ground status was determined.
Blaka.Flaka Blaka.Flaka

2019/5/14

#
i am confused on your coding from the demo some of the coding used i haven't learned yet
Blaka.Flaka Blaka.Flaka

2019/5/14

#
and would this work for an array of character that i have?
danpost danpost

2019/5/15

#
Blaka.Flaka wrote...
i am confused on your coding from the demo some of the coding used i haven't learned yet
What are you confused about? Maybe it can be re-written using code you are familiar with.
Blaka.Flaka wrote...
and would this work for an array of character that i have?
If I understand you correctly, I think the answer is "yes".
You need to login to post a reply.