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

2 days ago

How do i make it so the player kepts their horizontal velocity when they were moving on the ground, but cant change horizontal velocity midair?

JosukeHigashikata JosukeHigashikata

2 days ago

#
The title is what i want to happen, but the only things that im able to do instead is either have the character move in the air, or when jumping the player completely stops and jumps in one spot. i dont know if the code i have right now is required (and which parts to include and exclude) so i will send if someone needs it
danpost danpost

8 hours ago

#
The title is confusing. Sounds like you should be thinking it as: If "on the ground", allow a change in velocity. Think about coding it that way.
JosukeHigashikata JosukeHigashikata

7 hours ago

#
danpost wrote...
The title is confusing. Sounds like you should be thinking it as: If "on the ground", allow a change in velocity. Think about coding it that way.
I did try that, but it didnt work and im not sure what is wrong heres my code for the characters:
public class Fighter extends Actor
{
    // todo: would it be better to put these in each character instead
    int weight;
    int xVelocity;
    int yVelocity;
    int moveSpeed;
    int hp = 100;
    int jumpHeight;
    int xPos = 150;
    int yPos = 150;
    
    /**
     * Act - do whatever the Fighter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        
    }
    public boolean isGrounded(){
        // if the players position is below the arenas limit, the player is
        // considered grounded
        if (yPos >= Arena.ground){
            yPos = Arena.ground;
            return true;
        }
        return false;
    }
    public void fall(){
        // moves down if above the ground, if below, doesnt move at all
        if (isGrounded() == false){
            yVelocity = yVelocity + Arena.gravity + weight;
            yPos = yPos + yVelocity;
        } else {
            yVelocity = 0;
        }
    }
    public void jump(String up){
        if (Greenfoot.isKeyDown(up)){
            //can only jump when grounded
            if (isGrounded()){
                yVelocity = jumpHeight;
                //yPos = yPos - 1;
            }
        }
    }
    public boolean moveLeft(String left){
        if (Greenfoot.isKeyDown(left)){
            //can only change speed when on the ground,
            //same for moveright
            if (isGrounded()){
                xVelocity = -moveSpeed;
                return true;
            }
        }
        return false;
    }
    public boolean moveRight(String right){
        if (Greenfoot.isKeyDown(right)){
            if (isGrounded()){
                xVelocity = moveSpeed;
                return true;
            }
        }
        return false;
    }
}
and heres the actual player
public class Jotaro extends Fighter
{
    GifImage gifImage = new GifImage("jotarokujopixel.gif");
    /**
     * Act - do whatever the Jotaro wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        weight = 1/2;
        jumpHeight = -10;
        moveSpeed = 3;
        
        setLocation(xPos, yPos);
        xPos = getX();
        yPos = getY();
        xPos = xPos + xVelocity;
        yPos = yPos + yVelocity;
        isGrounded();
        fall();
        jump("W");
        //todo this code is HORRID please put this into fighter somehow later
        if (moveLeft("A") && moveRight("D")){
            // this is supposed to make the player not move if both buttons
            // are held but it doesnt work for some reason lol
            xVelocity = 0;
            setImage("jotarokujopixel21.png");
        } else if (moveLeft("A")){
            moveLeft("A");
            setImage(gifImage.getCurrentImage());
        } else if (moveRight("D")){
            moveRight("D");
            setImage(gifImage.getCurrentImage());
        }
        if (moveLeft("A") == false && moveRight("D") == false){
            xVelocity = 0;
            setImage("jotarokujopixel21.png");
        }
    }
}
danpost danpost

2 hours ago

#
JosukeHigashikata wrote...
I did try that, but it didnt work and im not sure what is wrong heres my code for the characters: << Codes Omitted >>
Looks like lines 15 through 18 in the Jotaro class codes might have something to do with it.
You need to login to post a reply.