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

2016/5/16

Issues with player-controlled character falling through platforms

jphb12 jphb12

2016/5/16

#
When I jump on platforms, I fall straight through after a few seconds. Also, I am able to jump through the bottom of the platform and land on top (this is also not intended) Does anyone have any idea how to fix this? Here is my code:
   private void checkContact(){
        Actor checkContact = getOneIntersectingObject(Level1Platform.class);
        Actor checkContact2 = getOneIntersectingObject(Level2Platform.class);
        Actor checkContact3 = getOneIntersectingObject(Level3Platform.class);
        if(checkContact !=null){
            int checkContactY = checkContact.getY();
            if(getY()>(checkContactY)){
                Surface = (checkContactY-50);
                setLocation(getX(), Surface);
                //This will keep R2D2 on the platform and display a message when a collsion occurs
            }
            if(RLspeed < 0){
                Surface = 366;
                setLocation(getX(), Surface);
            }
        }else{
            Surface = 366;
        }
        if(checkContact2 !=null){
            int checkContactY = checkContact2.getY();
            if(getY()>(checkContactY)){
                Surface = (checkContactY-30);
                setLocation(getX(), Surface);
                //This will keep R2D2 on the platform and display a message when a collsion occurs
            }
            if(RLspeed < 0){
                Surface = 366;
                setLocation(getX(), Surface);
            }
        }else{
            Surface = 366;
        }
        if(checkContact3 !=null){
            int checkContactY = checkContact3.getY();
            if(getY()>(checkContactY)){
                Surface = (checkContactY-30);
                setLocation(getX(), Surface);
                //This will keep R2D2 on the platform and display a message when a collsion occurs
            }
            if(RLspeed < 0){
                Surface = 366;
                setLocation(getX(), Surface);
            }
        }else{
            Surface = 366;
        }
    }
Surface = Level of the ground CheckContact1, 2 and 3 = For platforms 1, 2 & 3 respectfully. (Different sprites for each level)
danpost danpost

2016/5/17

#
You must determine the direction that R2D2 is "falling" (up or down) when making contact with a platform to figure out whether he should stop below or above the platform (if moving up, then stop under the platform; if moving down, stop to stand on the platform). My Jump and Run Demo w/Moving Platform scenario shows how this is accomplished in the Who class.
You need to login to post a reply.