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

2012/12/11

Moving while in air

programmer274 programmer274

2012/12/11

#
I would like to know how to make my character move horizontally left or right bases on the a or s keys while he is in midair. I was thinking of using a method i already have for detecting if he is on a platform and if either the a or s key is pressed but it doesn't seem to be working. Am I on the right track? public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public boolean onGround() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight() / 2, Ground.class); return under != null; } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public void jump() { if(Greenfoot.isKeyDown("space")) { vSpeed = -10; fall(); } if(under = null && (Greenfoot.isKeyDown("a"))) { slide1(); } if(under = null && (Greenfoot.isKeyDown("a"))) { slide2(); } } public void slide1() { setLocation ( getX() + hSpeed, getY()); hSpeed = hSpeed; } public void slide2() { setLocation ( getX() + hSpeed, getY()); hSpeed = -hSpeed; }
danpost danpost

2012/12/11

#
I think you are on the right track; however (1) I do not see the code for movement on the platform and (2) the following code looks awry:
public void jump()
{
  if(Greenfoot.isKeyDown("space"))
  {
      vSpeed = -10;
      fall();
} // looks OK to here
 // does the following line even compile?
// and what is 'under'?
 if(under =  null && (Greenfoot.isKeyDown("a")))    
 {
     slide1();
    }
     // same thing here?
     if(under = null && (Greenfoot.isKeyDown("a")))    
     {
     slide2();
    }
}
public void slide1()
{ 
  setLocation ( getX() + hSpeed, getY());
  hSpeed = hSpeed; // this does nothing
}
public void slide2()
{ 
  setLocation ( getX() + hSpeed, getY());
  hSpeed = -hSpeed; // this is an abrupt change in direction for the actor
}
which would be equivalent to
public void jump()
{
    if(Greenfoot.isKeyDown("space"))
    {
        vSpeed = -10;
        fall();
    }
    // still unknown 'under'
    if(under ==  null && (Greenfoot.isKeyDown("a")))    
    {
        setLocation ( getX() + 2 * hSpeed, getY()); // too much movement
        hSpeed = -hSpeed;  // still abrupt change in direction
    }
}
If you look at the codes closely, you will find that what I show here (and what you wrote above) is not what you want. You probaby just need to keep things simple. Since you always want left and right movement, do not apply any qualifications to it (no 'onGround()' check).
programmer274 programmer274

2012/12/11

#
well i want qualifications to it because normally moving left and right has the character cycle through 3 walking images. This doesn't look right in mid-air. I just want him to slide left and right and not cycle through any images. That is why i want qualifications.
danpost danpost

2012/12/11

#
Just copy the mouse checks and movement code into the fall method.
You need to login to post a reply.