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

2022/9/14

Why does my character suddenly stop moving right?

ClinderCinder ClinderCinder

2022/9/14

#
It moves left just fine but suddenly it sops moving right at places like the edge or on top a platform. ( I just started greenfoot and this is partially from a tutorial so it might just be a dumb mistake im not seeing)
import greenfoot.*;

/**
 * Write a description of class guineaPig here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class guineaPig extends Actor
{
    private final int gravity =1;
    private int velocity;
    private int imageNumber;
    coin coin;
    /**
     * Act - do whatever the guineaPig wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        fall();
        if(Greenfoot.isKeyDown("space") && isOnSolidGround()) jump();
        move();
        lvlChange1();
        hit();
    } 

    public void hit(){
        if( isTouchingSpike() )
        {
            setLocation(60, 500);

        }

    }

    public void Jumper(){
        velocity = 0;

    }

    public void fall(){
        setLocation(getX(), getY() + velocity);
        if(isOnSolidGround()) {velocity = 0;
            while(isOnSolidGround()){
                setLocation(getX(), getY() -1);
            }
            setLocation(getX(), getY() +1);
        }
        else if( velocity< 0 && didBumpHead()) velocity =0;
        else velocity += gravity;
    }

    public void jump(){
        velocity = -20;

    }

    public void move() {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) {
            imageNumber = 1;
            changeImage();
            x -=9; }
        else if(Greenfoot.isKeyDown("right") && canMoveRight()) {
            imageNumber = 0;
            changeImage();
            x +=9; 
        }
        setLocation(x,y);
    }

    public void lvlChange1(){

        if(getWorld() instanceof level1 && coin.score == 4){
            if(isAtEdge()&& getX()>700 && getY() > 500){
                Greenfoot.setWorld(new level2());}

        }

    }

    public boolean isOnSolidGround(){
        boolean isOnGround = false;

        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth /-2 , imageHeight /2,ground.class) !=null || 
        getOneObjectAtOffset(imageWidth /2 , imageHeight /2 -1,ground.class) !=null)
            isOnGround = true;
        return isOnGround;

    }

    public boolean isTouchingSpike(){
        boolean touchingSpike = false;
        if(getOneObjectAtOffset(0, 0, spike.class) !=null){

            touchingSpike = true;}
        return touchingSpike;
    }

    public boolean didBumpHead(){
        boolean bumpedHead = false;
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth /-2, imageHeight /-2,ground.class) !=null ||
        getOneObjectAtOffset(imageWidth /2, imageHeight /-2,ground.class) !=null) 
            bumpedHead = true;
        return bumpedHead;

    }

    public boolean canMoveLeft(){
        boolean canMoveLeft = true;
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2 - 9, imageHeight / -2  ,ground.class) !=null ||
        getOneObjectAtOffset(imageWidth /-2 -9, imageHeight /2 - 1,ground.class) !=null) 
            canMoveLeft = false;
        return canMoveLeft;

    }

    public boolean canMoveRight(){
        boolean canMoveRight = true;
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / 2 +9, imageHeight / -2 ,ground.class) !=null ||
        getOneObjectAtOffset(imageWidth /2 + 9, imageHeight /2 - 1,ground.class) !=null) 
            canMoveRight = false;
        return canMoveRight;

    }

    public void changeImage(){

        if(imageNumber == 0){
            setImage("guineaRight.png");
        }
        if(imageNumber == 1){
            setImage("guineaLeft.png");
        }
    }

}
ClinderCinder ClinderCinder

2022/9/15

#
nevermind i got rid of the - 1 on line 91
You need to login to post a reply.