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

2017/5/12

need help w/ combining move and jump for actor

chlo chlo

2017/5/12

#
My actor can move left and right and also jump but I was wondering if anyone knew a way to allow the actor to move while it's in the air when it jumps, or to let the actor jump while it's moving left or right?
Yehuda Yehuda

2017/5/12

#
It depends on your code. If you have an 'if' statement for performing those actions with each key, then just pressing two keys at once should work. But if you have 'else if' or something like that then it won't work. If you can't figure it out then post the class code.
danpost danpost

2017/5/12

#
If you code the horizontal and vertical movements separately (in the same act method), they will both work together.
public void act()
{
    moveVertically(); // rising, falling and detecting jump key
    moveHorizontally(); // detecting left and right movement keys
}
Either replace the individual method calls with the proper code or add those methods into the class with the appropriate codes.
chlo chlo

2017/5/12

#
I'm still kinda lost & don't know which part to exactly change so here is my code for the actor:
public class Avatar extends Actor
{
    private int ySpeed;
    private int apexTimer;
    private SaturnGame world;
    
    public void act()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/1;
        boolean onGround = (getY() == groundLevel);
        if (!onGround)
        {
           if (ySpeed == 0 && apexTimer > 0) apexTimer--; 
           if (ySpeed == 0 && apexTimer > 0) return; 
           ySpeed++; 
           setLocation(getX(), getY()+ySpeed); 
           if (getY()>=groundLevel) 
            {
                setLocation(getX(), groundLevel);
                Greenfoot.getKey();
            }
        }
        else 
        {
          if (Greenfoot.isKeyDown("up"))
            {
                ySpeed = -15;
                setLocation(getX(), getY()+ySpeed); 
                apexTimer = 30;
            }
          if(Greenfoot.isKeyDown("right"))
            {
                move (3);
                setImage("resizeimage.net-output.png");
            }
          if(Greenfoot.isKeyDown("left"))
            {
                move (-3);
                setImage("avatarright.png");
            }
          if (getOneIntersectingObject(Monster2.class) != null)
            {
                getWorld().removeObject(this);
            }
       }
    }  
 }
danpost danpost

2017/5/13

#
I think lines 5 and 20 can be removed safely. I also do not see a need for the apexTimer field. Gravity still works when the avatar is on the ground. To keep things more in the way things really happen, I prefer to apply gravity and move vertically regardless of current location. Then, if found on ground, adjust position and check jump. Something like this:
public void act()
{
    // vertical movement
    int groundLevel = getWorld().getHeight()-getImage().getHeight();
    ySpeed++;
    setLocation(getX(), getY()+ySpeed);
    if (getY() >= groundLevel) 
    {
        setLocation(getX(), groundLevel);
        ySpeed = 0;
        if (Greenfoot.isKeyDown("up"))
        {
            ySpeed = -15;
        }
    }
    // horizontal movement
    if (Greenfoot.isKeyDown("right"))
    {
        move (3);
        setImage("resizeimage.net-output.png");
    }
    if (Greenfoot.isKeyDown("left"))
    {
        move (-3);
        setImage("avatarright.png");
    }
    // death
    if (getOneIntersectingObject(Monster2.class) != null)
    {
        getWorld().removeObject(this);
    }
}
Line 9 in your code (my line 4) does not seem to have the correct expression for obtaining the ground level value. Only have the height of the avatar should be considered along with the height of the world. If their difference does not match with what is needed, some other offset needs to be taken into account.
You need to login to post a reply.