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
student1 student1

2018/4/6

#
i have gravity code but when i run it the player does not stand at the bottom instead half the player is not shown and sometimes it falls through the platform an some help.
public class Player extends Actor
{
   private int vSpeed = 0;
   private int acceleration = 2;
   private int speed = 5;
   private int jumpStrength = 6;
   
   public void act() 
   {
       //calling on methods
       move();
       checkfall();
   }
   

   public void move()
   {
     if(Greenfoot.isKeyDown("left") || (Greenfoot.isKeyDown("a")))
     {
         move(-4);
         setImage("Player-left.png");
     }
     
     if(Greenfoot.isKeyDown("right")|| (Greenfoot.isKeyDown("d")))
     {
         move(4);
         setImage("Player-right.png");
     }
     
     if(Greenfoot.isKeyDown("space")|| (Greenfoot.isKeyDown("w")))
     {
         jump();
     }
   }

   public void jump()
   {
       vSpeed = -jumpStrength;
       fall();
   }   

   public void checkfall()
   {
      if(onGround())
      {
          vSpeed = 0;
      }else
      {
            fall();
      }
   }

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

   public boolean onGround()
   {
       Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class);
       return under !=null;
   }
danpost danpost

2018/4/6

#
To get a better idea of what the code is doing, I am going to show the non-refactored version here:
public class Player extends Actor
{
    private int vSpeed = 0;
    private int acceleration = 2;
    private int speed = 5;
    private int jumpStrength = 6;
    
    public void act() 
    {
        // moving
        if (Greenfoot.isKeyDown("left") || (Greenfoot.isKeyDown("a")))
        {
            move(-4);
            setImage("Player-left.png");
        }
        if (Greenfoot.isKeyDown("right") || (Greenfoot.isKeyDown("d")))
        {
            move(4);
            setImage("Player-right.png");
        }
        // jumping
        if (Greenfoot.isKeyDown("space")|| (Greenfoot.isKeyDown("w")))
        {
            vSpeed = -jumpStrength;
            setLocation(getX(), getY() + vSpeed);
            vSpeed = vSpeed + acceleration;
        }
        // falling/landing
        if (getOneObjectAtOffset(0,getImage().getHeight()/2,Platform.class) !=null) vSpeed = 0;
        else
        {
            setLocation(getX(), getY() + vSpeed);
            vSpeed = vSpeed + acceleration;
        }
    }
}
Hope I did that right.
danpost danpost

2018/4/6

#
Okay. I do not see any code for hitting the bottom edge of the world. Also, the only thing the code does for "landing" on a platform is stop the vertical motion; so the vertical location is not adjusted to be actually "on" the platform (it could end up "sunk" into the platform as well).
student1 student1

2018/4/6

#
how can i fix that
danpost danpost

2018/4/7

#
student1 wrote...
how can i fix that
Add code to adjust y-coordinate of actor if near the bottom edge of the world. Also, add code to adjust y-coordinate of actor if landing/standing on a platform.
student1 student1

2018/4/8

#
i don't know the code for when the actor is at bottom of the world and i'm not sure on how to adjust the y-coordinate for when on a platform
danpost danpost

2018/4/9

#
student1 wrote...
i don't know the code for when the actor is at bottom of the world and i'm not sure on how to adjust the y-coordinate for when on a platform
The adjustments are quite similar. You take half the height of the image of the actor and subtract it from some know y-coordinate value that is considered ground level (the height of the world for bottom edge and the y-coordinate of the platform minus half its image height for top of platform). Set y-coordinate to that without changing the x-coordinate of the actor.
student1 student1

2018/4/9

#
so for the bottom of the world would it be this:
getWorld().getHeight() - getImage().getHeight()/2
but i don't know what the code would look like for the platform
danpost danpost

2018/4/9

#
student1 wrote...
so for the bottom of the world would it be this:
getWorld().getHeight() - getImage().getHeight()/2
That would be the new y-coordinate to set the actor at -- yes.
but i don't know what the code would look like for the platform
You first need to get a reference to the intersecting platform:
Actor platform = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
Then you can get the y-location and image of the platform for the wanted expression:
int platformX = platform.getX();
int platformHeight = platform.getImage().getHeight();
You probably will not have to explicitly name variables for these values -- just use the right side expressions in the setLocation call.
student1 student1

2018/4/9

#
i'm having a little trouble figuring out hot to put the code you helped with into my code
danpost danpost

2018/4/9

#
student1 wrote...
i'm having a little trouble figuring out hot to put the code you helped with into my code
I cannot see what you tried.
student1 student1

2018/4/9

#
i'm meaning the code that you shortened at the top of the discussion
danpost danpost

2018/4/9

#
student1 wrote...
i'm meaning the code that you shortened at the top of the discussion
Please show your revised code. I hope it is not still exactly as it was given above (it better not be). You should have at least tried something.
student1 student1

2018/4/9

#
i changed the fall section of my code but i managed to break the jumping part and i don't how to fix that and i don't know how th apply the platform part to the code
student1 student1

2018/4/9

#
 public void fall()
   {
       int worldBottom = getWorld().getHeight()-getImage().getHeight()/2;
       boolean ground = (getY()==worldBottom);
       if(getY()>worldBottom)
       {
          setLocation(getX(),worldBottom);
       }
   }
There are more replies on the next page.
1
2
3