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

2014/4/6

Jumping Gliche

Greenfootnoob Greenfootnoob

2014/4/6

#
G'day. I was making a simple game where monkey had to jump on platforms to reach the final destination. however, when i jump , and my monkey touches the platforms above, it directly goes to the above platforms, rather than hitting the platform and falling down again. Can anyone help me with this?
public class Monkey extends Animal
{
    private int speed = 2; //movement speed  
    private int vSpeed = 0; //vertical speed  
    private int acceleration = 2; //gravity effect while falling  
    private int jumpStrength = -20; 
    public void act() 
    {
        
        checkFall();
        jump();
checkKeys();
        
        
    }

    private void checkKeys()
    {
        
        if(Greenfoot. isKeyDown("space"))
        {  
            jump();
        }
    }

    public void jump()  
    {  
        if (Greenfoot.isKeyDown("space") && onPlatform())  
        {  
            vSpeed = jumpStrength;  
            fall();  
}
    } 

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

    public boolean onPlatform()  
    {  
        Actor under = getOneIntersectingObject(Platform.class)  ;
        return under !=null;  
    }  

    public void checkFall()  
    {  
        if (onPlatform())  
        {  
            vSpeed = 0;  
        }  
        else  
        {  
            fall();  
        }  
    }  
}
danpost danpost

2014/4/6

#
The method you are using in the 'onPlatform' method will allow jumping ANYTIME the actor is touching a platform. You need (for one) to use a more specific type collision check (probably 'getOneObjectAtOffset(0, getImage().getHeight()/2)'). This will not fix everything, however, as you still need to check for colliding platform from all sides AND adjust the players position when a collision does occur. There seems to be some duplicate execution of code in the class above. Both 'jump' and 'checkKeys' do the same thing (running the 'jump' method; you are just getting there in two different ways). The only place right now that you are moving the actor is in the 'fall' method (using 'setLocation'). That is where you will need to check for collisions with platforms and adjust the actors position (so it is not over the platform -- where the images are overlapping). The sign of the value of vSpeed should be a clue as to whether the platform was above or below the actor. Instead of an 'onPlatform' method, better might be an 'onPlatform' boolean field (or even better -- a local variable within the 'fall' method. What I envision is this: (1) add 'acceleration' to 'vSpeed' (2) vertically move actor ("fall" -- or "rise", if vSpeed is negative) (3) if not intersecting platform, set 'onPlatform' to 'false', else (a) if vSpeed is positive, set actor above platform, set 'vSpeed' to zero, and set 'onPlatform' to 'true' (b) if vSpeed is negative, set actor below platform, set 'vSpeed' to zero and set 'onPlatform' to 'false' (4) if 'onPlatform' is 'true', check key for jumping If this was done in one method (maybe called 'verticalMovement'), in the order specified, you should be able to get your actor to behave.
Greenfootnoob Greenfootnoob

2014/4/6

#
Hey DanPost! Thank you so much for your detailed response. unfortunately for me, I am only 10 years old and I'm new to Greenfoot. do you mind telling me what to add or change in my code(Sorry, i didn't really understand what you said). Your help is much appreciated.
danpost danpost

2014/4/6

#
Pretty much everything after the first six lines of the code above will have to be changed. Of course, even after this is fixed, you will probably want to add some horizontal movement to the monkey(s). The act would (at this point) simply be:
public void act()
{
    verticalMovement();
}
and the verticalMovement would start with
private void verticalMovement()
{
    boolean onPlatform = false;
followed by the sequence I gave above (you do not have to set 'onPlatform' to 'false' in (3) or (3)(b) above as it starts off being 'false'). The outline is fairly straight-forward and should be easy to code. That would be the entire class (as of now).
danpost danpost

2014/4/6

#
Actually, this could be done without an 'onPlatform' method or field. Step (4) can replace the "set 'onPlatform' field to 'true'" in (3)(a).
danpost danpost

2014/4/6

#
My revised outline: (1) add 'acceleration' to 'vSpeed' (2) vertically move actor (3) if intersecting platform (a) if vSpeed is positive (i) set actor above platform (ii) set 'vSpeed' to zero (iii) if 'space' key is down * add 'jumpStrength' to 'vSpeed' (b) if vSpeed is negative (else block) (i) set actor below platform (ii) set 'vSpeed' to zero
You need to login to post a reply.