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

2015/2/24

Trouble jumping

ShaunThompson12 ShaunThompson12

2015/2/24

#
}
    public int ySpeed= 0;
    public void act()
    {
        int groundLevel = getWorld().getWidth() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                 // clears any key pressed during jump
            }
        }
        else // on ground
        {
            if ("w".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
            }
        }
    }
Can Any one help figure out what is not working? I have two characters and they will not jump at different times. They will only jump at different times and I cant figure it out.
ShaunThompson12 ShaunThompson12

2015/2/24

#
ignore line 13
davmac davmac

2015/2/24

#
I have two characters and they will not jump at different times. They will only jump at different times
Which is it...?
ShaunThompson12 ShaunThompson12

2015/2/25

#
oops... they will jump together and I need them to jump separately.
danpost danpost

2015/2/25

#
Are both actors created from the same class? If so, post the entire class code. If not, are you using "w" as the jump trigger for both actors? If so, change the jump key for one of them.
ShaunThompson12 ShaunThompson12

2015/2/25

#
the first character is "w" and the second character jumps on "up". The Second character is the "problem" character that wont jump.
public class Character extends Actor
{
    private static final double WALKING_SPEED = 0.0;
    public int ySpeed= 0;

    public void act()
    {
        checkKeyPress();//allows keys to be pressed
        move();

    }

    public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
        setLocation(x, y);
    }

    public void checkKeyPress()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            move(-4);//when pressing the left arrow a -4 degree turn is completed
        }
        if(Greenfoot.isKeyDown("right"))
        {
            move(4);//4 degree turn when right is pressed
        }
        int groundLevel = getWorld().getWidth() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground

            }
        }
        else // on ground
        {
            if ("up".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
            }
        }
    }
}
that is the second character.(above)
public class Character2 extends Actor
{
    private static final double WALKING_SPEED = 0.0;
    public int ySpeed= 0;
    public void act()
    {
        checkKeyPress();//allows keys to be pressed
        move();
        int groundLevel = getWorld().getWidth() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                
            }
        }
        else // on ground
        {
            if ("w".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
            }
        }
    }

    public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
        setLocation(x, y);
    }

    public void checkKeyPress()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            move(-4);//when pressing the left arrow a -4 degree turn is completed
        }
        if(Greenfoot.isKeyDown("d"))
        {
            move(4);//4 degree turn when right is pressed
        }
    }

}
this is the first character.
danpost danpost

2015/2/25

#
The only difference I see in the two code sets (other than which keys are being used) is when 'move' is executed. In the letter key character, the vertical movement is done after 'move' in the act method (lines 9 through 28); in the arrow key character, the vertical movement is done before 'move' in the 'checkKeys' method (lines 31 through 50). Consistency might help.
davmac davmac

2015/2/25

#
I think the problem is that you are using Greenfoot.getKey(...) twice (once in each actor). As the documentation for the method says: Get the most recently pressed key, since the last time this method was called. If no key was pressed since this method was last called, it will return null. So, if you have two actors both calling getKey(...) then you have to press the second key inbetween the first and the second one acting, which is going to be pretty much impossible. Why don't you use isKeyDown(...)?
ShaunThompson12 ShaunThompson12

2015/2/26

#
Thank you both very much I made the changes both of you suggested and it worked. Thank You!!!
You need to login to post a reply.