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

2018/4/6

help with Gravity code

1
2
3
danpost danpost

2018/4/9

#
Okay, after line 8 add the if for the platform check.
student1 student1

2018/4/9

#
need help with this
public void checkfall()
   {
      if(onGround())
      {
          vSpeed = 0;
      }else
      {
            fall();
      }
   }
 
   public void fall()
   {
       setLocation(getX(), getY() + vSpeed);
       vSpeed = vSpeed + acceleration;
       
       int worldBottom = getWorld().getHeight() - getImage().getHeight()/2;
       boolean ground = (getY() == worldBottom);
       if(getY()>=worldBottom)
       {
          setLocation(getX(),worldBottom);
       }       
       if(getY()>=worldBottom)
       {
          Actor platform = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
          int platformX = platform.getX();
          int platformHeight = platform.getImage().getHeight();
          setLocation(platformX,platformHeight/2);
       } 
   }
 
   public boolean onGround()
   {
       Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
       return under !=null;
   }
danpost danpost

2018/4/9

#
Line 23 through 25 should be:
Actor platform = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
if (platform != null)
{
student1 student1

2018/4/9

#
i fixed it and it works without showing errors now but when i jump onto a platform it spawns at the top and falls continually
danpost danpost

2018/4/9

#
student1 wrote...
i fixed it and it works without showing errors now but when i jump onto a platform it spawns at the top and falls continually
Okay. Now remove the checkFall and onGround methods; in the act method, change checkFall to fall; and add the following line after both lines 21 and 28:
vSpeed = 0;
Oh, and switch lines 14 and 15.
student1 student1

2018/4/9

#
ok i did that but when the player is on a platform it is continually falling from above the platform onto it and i'm wanting it to just sit on the platform when the player is moving
danpost danpost

2018/4/9

#
student1 wrote...
ok i did that but when the player is on a platform it is continually falling from above the platform onto it and i'm wanting it to just sit on the platform when the player is moving
Show your revised class code for review.
student1 student1

2018/4/9

#
public void fall()
   {
       vSpeed = vSpeed + acceleration;
       setLocation(getX(), getY() + vSpeed);
       
       int worldBottom = getWorld().getHeight() - getImage().getHeight()/2;
       boolean ground = (getY() == worldBottom);
       if(getY()>=worldBottom)
       {
          setLocation(getX(),worldBottom);
          vSpeed = 0;
       }
       Actor platform = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
       if(platform !=null)
       {
          int platformX = platform.getX();
          int platformHeight = platform.getImage().getHeight();
          setLocation(platformX,platformHeight/2);
          vSpeed = 0;
       } 
   }
danpost danpost

2018/4/9

#
student1 wrote...
<< Code Omitted >>
You need to use the y-coordinate of the platform as well as the actor's image height to determine new y-coordinate of actor at line 18.
student1 student1

2018/4/9

#
how would i do that
danpost danpost

2018/4/9

#
student1 wrote...
how would i do that
public void fall()
{
    int worldBottom = getWorld().getHeight();
    int myMidHeight = getImage().getHeight()/2;
    vSpeed = vSpeed + acceleration;
    setLocation(getX(), getY() + vSpeed);
    if (getY() >= worldBottom)
    {
        setLocation(getX(), worldBottom-myMidHeight);
        vSpeed = 0;
    }
    Actor platform = getOneObjectAtOffset(0, myMidHeight, Platform.class);
    if (platform != null)
    {
        int platformHeight = platform.getImage().getHeight();
        setLocation(getX(), platform.getY()-platformHeight/2-myMidHeight);
        vSpeed = 0;
    } 
}
I moved a couple variables to the top of the method since they are used unconditionally and often.
student1 student1

2018/4/9

#
thank you it works perfectly except this part
setLocation(getX(), worldBottom-myMidHeight);
so i took out myMidHeight and it works the way i wanted it to but the jumping part is when you hold space it flies to the top of the world but i'm sure it will be fine
danpost danpost

2018/4/9

#
student1 wrote...
thank you it works perfectly except this part
setLocation(getX(), worldBottom-myMidHeight);
so i took out myMidHeight and it works the way i wanted it to but the jumping part is when you hold space it flies to the top of the world but i'm sure it will be fine
Lines 7 and 9 should both use 'worldBottom-myMidHeight'. (that was the problem there)
student1 student1

2018/4/9

#
i tried what u said but the player floats in the air a little but anyway it's fine so thank you and i'm just curious but when the game goes to the game over screen i want it to have text that can be clicked on to go to level 1, i'v done it before but it was really a long time ago so i can't remember the code for it if i need to start a separate discussion i will
danpost danpost

2018/4/9

#
student1 wrote...
i tried what u said but the player floats in the air a little but anyway it's fine so thank you
You probably have your old line 6, where worldBottom is defined differently than my line 3.
and i'm just curious but when the game goes to the game over screen i want it to have text that can be clicked on to go to level 1, i'v done it before but it was really a long time ago so i can't remember the code for it if i need to start a separate discussion i will
Probably would be best, being it has nothing to do with gravity.
There are more replies on the next page.
1
2
3