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

2013/9/29

how can i make it so my player can only jump if on gound?(platformer)

finbololz finbololz

2013/9/29

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class player extends Actor
 {
     private int speed = 4;
     private int vSpeed = 0;
     private int acceleration = 1;
     private int jumpStrength = 9;
     
     private GreenfootImage moveLeft1 = new GreenfootImage("C:/Users/Claire/Desktop/Finlays folder/greenfoot/platformer/images/playerleftrun1.gif");
     private GreenfootImage moveLeft11 = new GreenfootImage("C:/Users/Claire/Desktop/Finlays folder/greenfoot/platformer/images/playerleftrun1.1.gif");
     private GreenfootImage moveLeft2 = new GreenfootImage("C:/Users/Claire/Desktop/Finlays folder/greenfoot/platformer/images/playerleftrun2.gif");
     private GreenfootImage moveRight1 = new GreenfootImage("C:/Users/Claire/Desktop/Finlays folder/greenfoot/platformer/images/playerrightrun1.gif");
     private GreenfootImage moveRight11 = new GreenfootImage("C:/Users/Claire/Desktop/Finlays folder/greenfoot/platformer/images/playerrightrun1.1.gif");
     private GreenfootImage moveRight2 = new GreenfootImage("C:/Users/Claire/Desktop/Finlays folder/greenfoot/platformer/images/playerrightrun2.gif");
     private int frame = 1;
     private int animationCounter = 0;
    /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeys();
       checkfall();
       
       
       
       animationCounter ++;
    }   
    
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("player-left.gif");
            moveLeft();
    }
     if (Greenfoot.isKeyDown("right") )
    {
            setImage("player-right.gif");
            moveRight();
    }
    if (Greenfoot.isKeyDown("up") )    {
            jump();
    }
   }
   
   public void jump()
   {
       vSpeed = -jumpStrength;
       fall();
       
    }
   
   public void checkfall()
   {
    if(onGround()) {
        vSpeed = 0;
        
    }
    else{
         fall();
    }
   }
    
   public boolean onGround()
    {
     Actor under = getOneObjectAtOffset( 0, getImage().getHeight() / 2 , platform.class);
     return under != null;
    }
    
   public void fall()
   {
     setLocation ( getX(), getY() + vSpeed);
     vSpeed = vSpeed + acceleration;
  
    } 
    
   public void moveRight()
   {
       setLocation ( getX() + speed, getY() );
       if(animationCounter % 12 == 0)
       animateRight();
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
           setImage(moveRight1);    
    
        }
        else if(frame ==2)
        {
            setImage(moveRight2);  
            frame = 1;
            return;
        }
        
        frame ++;
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
        if(animationCounter % 12 == 0)
        animateLeft();
    }
    
    
    public void animateLeft()
    {
        if(frame == 1)
        {
           setImage(moveLeft1);    
    
        }
        else if(frame ==2)
        {
            setImage(moveLeft2);  
            frame = 1;
            return;
        }
        
        frame ++;
}
}
GreenHouse GreenHouse

2013/9/29

#
it's like you said; only jump if on ground
public void jump()  
{ 
       if(onGround()) { 
             vSpeed = -jumpStrength;  
             fall(); 
       } 
         
}
You need to login to post a reply.