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

2015/6/12

Actor on a platform

sehoch sehoch

2015/6/12

#
Hello everyone, I need help because my actor can get on the platform and move with it, but it will be great if he could only go on the platform by the top and not by the bottom and both sides because he stick to it I've tried something like this but it tells me that I can't use a static thing in a non-static method:
 
public void checkXY()
    { if(Pony.getY<Plateform.getY())
        
        { 
            fall();
        }
    
    
    }
and there's the complete code at the moment :
public void fall()
    {
      setLocation (getX(), getY() + vSpeed);
      vSpeed = vSpeed + acceleration;
    }
    
    public boolean onGroud ()
    {
      int spriteHeight = getImage().getHeight();
      int lookForGround = (int) (spriteHeight/2);
      
      Actor ground = getOneObjectAtOffset (0, lookForGround, Platform.class);
      if (ground == null)
      {
          return false;
      }
      else
      {
          return true;
      }
    }
    
    public void checkFall ()
    {
      if (onGround())
      {
         vSpeed = 0;
      }
      else
      {
         fall(); 
      }
    }
    
    private void jump()
    {
        vSpeed = -jumpStrength;
        fall();
    }
Thanks !
danpost danpost

2015/6/12

#
It would be better to have all the code in the methods shown in one method as they all deal with vertical movement. Also, since gravity is ALWAYS in effect, it would be better to apply gravity and move vertically unconditionally. By doing so, the following ground check can determine the on-ground state for the future jump check. Something like this:
public void moveVertically()
{
    vSpeed += acceleration;
    setLocation(getX(), getY()+vSpeed); // unconditional vertical movement
    int direction = (int)Math.signum(vSpeed); // the sign of 'vSpeed' (-1, 0 or 1)
    boolean onGround = false; // local variable to hold on ground state
    while (getOneIntersectingObject(Platform.class) != null) // any platform intersection
    {
        setLocation(getX(), getY()-direction); // move back off
        vSpeed = 0; // stop movement
        if (direction == 1) onGround = true; // if "falling", set on-ground state to true
    }
    if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -jumpStrength;
}
The horizontal movement should be coded separately and both should be called from the act method. Also, all other method calls that deal with movement should be removed from the act method. The movement should be one of the first things processed from the act method. The code for moving horizontally can be written in a similar fashion. Get input to determine direction of movement and move; then check for obstacles and world edges and perform necessary actions. There are several benefits of how the code is written above: * the is a minimum on the number of instance fields required (just the 'vSpeed' field; where 'acceleration' and 'jumpStrength' are constants) * the movement code is self-contained * all actions are done at the time of determination that they need done (we are not saving flags and returning to do those actions later or reassessing the state to perform actions that could have been done when the state was first assessed) * there is no unnecessary refactoring of the code; refactoring is useful (1) when the code is executed multiple times from different places in the code -- use discretion when the refactored method would only be called a few times and contain just one or two lines of code; or (2) when the method is over-sized -- if it does not fit on your screen, it is probably oversized;
You need to login to post a reply.