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

2014/4/2

gliche when jumping

Greenfootnoob Greenfootnoob

2014/4/2

#
G'day. my actor jumps perfectly, however when i jump to high platforms, and fall down, it doesn't land on the platform, below, instead, it goes right down to the bottom of the world. can anyone help me please?
public class Frog extends Actor
{
    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() 
    {
        checkKeys();
        checkFall();
        jump();
         
    }

  
    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 = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);  
        return under != null;  
    }  

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

}
^^This is my code^^
danpost danpost

2014/4/2

#
Your 'fall' method needs to check for intersecting platforms and adjust the position of the frog. The frog could jump into (going upward) into a platform or fall into (going downward) into a platform; so, you will have to determine which and position the frog accordingly. Using 'getIntersectingObjects' would give you a wider range of intersection checking (at least to begin with) so your frog will not pass a platform without it being detected.
Greenfootnoob Greenfootnoob

2014/4/3

#
sorry, i don't really understand, could you please tell me what to add or change? Thanks!
trias95 trias95

2014/4/3

#
Lol, this example is the one from youtube isnt it. i had a play with this myself last night. i find that
 public boolean onPlatform()    
    {    
        Actor under = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);    
        return under != null;    
    }    
  
doesnt actually work that well. you see. when falling. this code is checking to see if the bottom row of pixels of your image is touching the platform and of course, with the use of acceleration it isnt uncomon for that bottom line of your character to of passed the platform before it can be detected and stopped moving. i found a fix for this was to instead of using the code
getOneObjectAtOffset (0, getImage().getHeight()/2, Platform.class);    
to instead use the code
Actor under = getOneIntersectingObject(yourclass.class)
     return under !=null;
this code checks if the image is touching it, it has a much better chance at catching the player. Also, jumping too high and falling at a constantly accelerating rate will mean that even this code has a chance to not catch the player correctly and as such, you should cap the jump height :) Good luck
Greenfootnoob Greenfootnoob

2014/4/4

#
Thank you so much trias95, it worked!
trias95 trias95

2014/4/4

#
Happy to help :)
You need to login to post a reply.