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

2018/4/2

Gravity

1
2
student1 student1

2018/4/2

#
i'm making a game and so far i have gravity in the game but i want to make it so the player can jump on to platforms but i can't figure out how to place that into the code that i have, please help.
private int ySpeed;
public void Gravity()
   {
      int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
      boolean onGround = (getY() == groundLevel);
      if (!onGround)
      {
          ySpeed++;
          setLocation(getX(), getY()+ySpeed);
          if (getY()>=groundLevel)
          {
              setLocation(getX(), groundLevel);
              Greenfoot.getKey();
          }
      }
      else
      {
          if ("space".equals(Greenfoot.getKey()))
          {
              ySpeed = -15;
              setLocation(getX(), getY()+ySpeed);
          }
      }
   }
danpost danpost

2018/4/3

#
You will need another condition for "on ground" at line 5 to determine if standing on a platform and then an else block following line 14 to deal with landing on a platform. You should remove line 13 as it does nothing by itself (except possibly preventing other code from getting an entered key). Also, line 18 should probably use 'isKeyDown' as follows:
if (Greenfoot.isKeyDown("space"))
student1 student1

2018/4/3

#
sorry danpost but since i'm fairly new to greenfoot i don't know how to code what your telling me, i get what your saying just not the codeing. sorry if its too much.
danpost danpost

2018/4/3

#
student1 wrote...
sorry danpost but since i'm fairly new to greenfoot i don't know how to code what your telling me, i get what your saying just not the codeing. sorry if its too much.
What would you do to check to see if the actor is on a platform?
student1 student1

2018/4/3

#
i'm thinking it would be if touching platform.class then check weather on platform or not. or the condition would that be a boolean
danpost danpost

2018/4/3

#
student1 wrote...
i'm thinking it would be if touching platform.class then check weather on platform or not. or the condition would that be a boolean
You want to for a platform below the actor -- at some y-offset value. What code might you use for that?
student1 student1

2018/4/3

#
boolean onPlatform = (getY() == groundLevel);

if(isTouching(Platform.class))
{
      if(! onPlatform)
      {
            ySpeed++;
            setLocation(getX(), getY()+ySpeed);
            if(getY()>=groundLevel)
            {
                   setLocation(getX(), groundLevel);
            }
      }
}
is this it
danpost danpost

2018/4/3

#
It would not be separate code. It would be coded along with the "on ground" code. Also, touching a platform does not necessarily mean that the actor is standing on it (could go sideways into one or hit its head on one). Line 5 of your original code can be this:
boolean onGround = (getY() == groundLevel) || isOnPlatform();
and you can write a private boolean isOnPlatform method to return an appropriate boolean value using something better than isTouching.
student1 student1

2018/4/3

#
sorry danpost but i'm not sure how to code that.
danpost danpost

2018/4/4

#
student1 wrote...
sorry danpost but i'm not sure how to code that.
I tried to give you a hint:
danpost wrote...
You want to ^check^ for a platform below the actor -- at some y-offset value. What code might you use for that?
student1 student1

2018/4/4

#
public void Gravity()
   {
      int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
      boolean onGround = (getY() == groundLevel);
      if (!onGround)
      {
          ySpeed++;
          setLocation(getX(), getY()+ySpeed);
          if (getY()>=groundLevel)
          {
              setLocation(getX(), groundLevel);
              onPlatform();
          }
      }
      else
      {
          if (Greenfoot.isKeyDown("space"))
          {
              ySpeed = -15;
              setLocation(getX(), getY()+ySpeed);
          }
      }
   }
   
   public Boolean onPlatform()
   {
       Actor under = getOneObjectAtOffset(0, getHeight()/2,Platform.class);
       return under != null;
   }
i'v tried this code, but i'm getting errors please help
danpost danpost

2018/4/4

#
student1 wrote...
<< Code Omitted >> i'v tried this code, but i'm getting errors please help
An actor does not have height -- but its image does. Use getImage().getHeight()/2. Also, boolean on line 25 is best not to be capitalized.
student1 student1

2018/4/4

#
i fixed the code but it does not work. i don't know what went wrong
danpost danpost

2018/4/4

#
student1 wrote...
i fixed the code but it does not work. i don't know what went wrong
danpost wrote...
Line 5 of your original code can be this:
boolean onGround = (getY() == groundLevel) || isOnPlatform();
and you can write a private boolean isOnPlatform method to return an appropriate boolean value using something better than isTouching.
Okay, you called the method onPlatform -- but same difference.
student1 student1

2018/4/4

#
when i tried what you suggested it said that the method could not be found but the way i did it does not come up with any errors it just does not acknowledge platform
There are more replies on the next page.
1
2