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

2020/3/14

How can i make my actor stay on platforms?

YothMan YothMan

2020/3/14

#
So i made this code by following a video but it didn't get the results i wanted. I am very new to programming and i don't know what i am doing. This is for a school project :(
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    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;
            
            while(didBumpHead()){
                setLocation(getX(), getY()+1);
            }
        }
        else velocity+=GRAVITY;
    }
    
    public void jump(){
        velocity=-8;
    }
    
    public void move(){
        int x =getX();
        int y=getY();
 
        if (Greenfoot.isKeyDown("left")&& canMoveLeft()) x-=STEP;
        if (Greenfoot.isKeyDown("right")&& canMoveRight()) x+=STEP;
        setLocation(x,y);
        

    }
    
    public boolean isOnSolidGround(){
        boolean isOnGround= false;
        
       if(getY() > getWorld().getHeight()-50) isOnGround=true;
       
       int imageWidth = getImage().getWidth();
       int imageHeight= getImage().getHeight();
       if(getOneObjectAtOffset(imageWidth/-1, imageHeight/1, Platform.class)!=null||
           getOneObjectAtOffset(imageWidth/1, imageHeight/1, Platform.class)!=null)          
          isOnGround = true;
       
        return isOnGround;
    }
     
    public boolean didBumpHead(){
       boolean bumpedHead=false;  
        
        int imageWidth = getImage().getWidth();
       int imageHeight= getImage().getHeight();
       if(getOneObjectAtOffset(imageWidth/-1, imageHeight/-1, Platform.class)!=null||
           getOneObjectAtOffset(imageWidth/1, imageHeight/-1, Platform.class)!=null)
           
       bumpedHead= true;
       
       return bumpedHead;
    }
    
    public boolean canMoveLeft(){
        boolean canMoveLeft=true;  
        
        int imageWidth = getImage().getWidth();
       int imageHeight= getImage().getHeight();
       if(getOneObjectAtOffset(imageWidth/-1-STEP, imageHeight/-1, Platform.class)!=null||
           getOneObjectAtOffset(imageWidth/-1-STEP, imageHeight/1-0, Platform.class)!=null)
           
       canMoveLeft= false;
       
       return canMoveLeft;
    }
    
    public boolean canMoveRight(){
         boolean canMoveLeft=true;  
        
        int imageWidth = getImage().getWidth();
       int imageHeight= getImage().getHeight();
       if(getOneObjectAtOffset(imageWidth/1+STEP, imageHeight/-1, Platform.class)!=null||
           getOneObjectAtOffset(imageWidth/1+STEP, imageHeight/1-0, Platform.class)!=null)
           
       canMoveLeft= false;
       
       return canMoveLeft;
    }
}
YothMan YothMan

2020/3/14

#
My world has two actor platforms ("Platform", "Small platform") , however my actor somehow comes through to the these platforms even goes floating in some areas. My main actor jumps really high, and i dont know how to reduce it pls help :(
CieloMungcal CieloMungcal

2020/3/14

#
i can help
danpost danpost

2020/3/14

#
Please refer to my Jump and Run Demo w/Moving Platform scenario. (you cannot rely on some videos giving you "well-grounded" -- sorry for the pun -- code)
CieloMungcal CieloMungcal

2020/3/14

#
private int vSpeed = 0; private int acceleration = 1; private int jumpHeight = -8; //makes actor jump if(Greenfoot.isKeyDown("up")) { vSpeed = jumpHeight; fall(); } //checks if standing on land boolean onGround() { Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, land.class); return under != null; } public void checkFalling() { if (onGround() == false) { fall(); } } //falling private void fall() { setLocation (getX(), getY() + vSpeed); vSpeed = vSpeed +acceleration; }
YothMan YothMan

2020/3/14

#
THANK YOU SO MUCHHH!!!!
YothMan YothMan

2020/3/14

#
@Danpost THANK YOU MUCH TOO!!
YothMan YothMan

2020/3/15

#
CieloMungcal wrote...
private int vSpeed = 0; private int acceleration = 1; private int jumpHeight = -8; //makes actor jump if(Greenfoot.isKeyDown("up")) { vSpeed = jumpHeight; fall(); } //checks if standing on land boolean onGround() { Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, land.class); return under != null; } public void checkFalling() { if (onGround() == false) { fall(); } } //falling private void fall() { setLocation (getX(), getY() + vSpeed); vSpeed = vSpeed +acceleration; }
@CieloMungal my actor just slips through the Platform i dont know why. I changed the the "Ground" to my actor name but it just fall through
You need to login to post a reply.