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

2014/4/14

Jumping on the ground

Arthur114 Arthur114

2014/4/14

#
How to make my actor so he can jump not only on the ground.class(platforms) but also on the bottom of the background thank you.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Vilkas here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Vilkas extends Actor
{
       private GreenfootImage image1 = new GreenfootImage("wombat.png");  
    private GreenfootImage image2 = new GreenfootImage ("wombat-left.png");  
    private int vSpeed = 0;
    private int acceleration = 2;
    private boolean jumping;
    private int jumpStrength = 20;
    
    public void act() 
    {
       checkFall();
        animate();
      CheckKeys();
      platformAbove();
      checkRightWalls();
      checkLeftWalls();
    }    
    public void CheckKeys()
    {
         if(Greenfoot.isKeyDown("a")){  
            move(-5);  
        }  
          
        if(Greenfoot.isKeyDown("d")){  
            move(5);  
        }   
        if(Greenfoot.isKeyDown("space") && jumping == false){  
            jump();  
        }   
    }
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
     public void animate()  
    {  
        if((Greenfoot.isKeyDown("d"))){  
        setImage (image1);  
    }  
        if((Greenfoot.isKeyDown("a"))){  
            setImage (image2);  
        }  
}
public void fall()   
{  
    setLocation(getX(),getY()+ vSpeed);  
    if(vSpeed <=9)
    {
        vSpeed = vSpeed + acceleration;
    }
    jumping = true;
}  
public boolean onGround()
{
    int spriteHeight = getImage().getHeight();
    int yDistance = (int) (spriteHeight / 2 + 50 );
    Actor ground = getOneObjectAtOffset (0,getImage().getHeight() / 2, Ground.class);
    if(ground == null)
    {
        jumping = true;
        return false;
    }
    else
    {
        moveToGround(ground);
        return true;
    }
}
public void checkFall()
{
    if(onGround()) {
        vSpeed=0;
    }
    else {
        fall();
    }
}
public void moveToGround(Actor ground)
{
    int groundHeight = ground.getImage().getHeight();
    int newY = ground.getY() - (groundHeight + getImage().getHeight()) / 2;
    
    setLocation(getX(),newY);
    jumping = false;
}
public boolean platformAbove()  
    {  
        int spriteHeight = getImage().getHeight();  
        int yDistance = (int)(spriteHeight/-2);  
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Ground.class);  
        if(ceiling != null)  
        {  
            vSpeed = 1;  
            bopHead(ceiling);  
            return true;  
        }  
        else  
        {  
            return false;  
        }  
    }  
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);
        
        Actor rightWall = getOneObjectAtOffset(xDistance,0,Ground.class);
        if(rightWall == null)
        {
            return false;
        }
        else {
            stopByRightWall(rightWall);
            return true;
        
        }
    }
    public void stopByRightWall (Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() - (wallWidth + getImage().getWidth()) / 2;
        setLocation(newX - 5, getY());
    }
    public void bopHead(Actor ceiling)  
    {  
        int ceilingHeight = ceiling.getImage().getHeight();  
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;  
        setLocation(getX(), newY);  
    }  
     public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);
        
        Actor leftWall = getOneObjectAtOffset(-xDistance,0,Ground.class);
        if(leftWall == null)
        {
            return false;
        }
        else {
            stopByleftWall(leftWall);
            return true;
        
        }
    }
    public void stopByleftWall (Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth()) / 2;
        setLocation(newX + 5, getY());
    }
}
danpost danpost

2014/4/14

#
I believe that adding acceleration to the speed and falling should be done each act cycle regardless of the 'onGround' state (gravity does not go away just because you have found the ground -- it is just counter-balanced by the force the ground exerts in the opposite direction). Therefore, I feel that the 'boolean jumping' field should be a local variable (if used at all), not an instance field; and its value should be determined each act after falling is executed. The sign of the 'speed' field can determine where to 'stop' the actor when contacting a platform ('bopHead' and 'moveToGround'). To jump, you just need to ask 'Is there a platform at my feet or am I standing on the bottom edge of the world?'. This can be determined without having to use a boolean variable. The following should be a guideline for the vertical movement of an actor:
// add acceleration to speed and fall
// check for ground objects and, if found, adjust position (if speed is less than zero, than actor is bopping head else the actor is landing on ground -- set speed to zero after adjusting position)
// ensure actor is not going under the world (adjust position for bottom edge intrusion)
int halfHeight = getImage().getHeight()/2;
int maxY = getWorld().getHeight()-halfHeight;
if (getY() > maxY) setLocation(getX(), maxY);
// check for jumping (if on ground or at bottom edge of world)
if ((getOneObjectAtOffset(0, halfHeight+1, Ground.class) != null || getY() == maxY) && Greenfoot.isKeyDown("space")) // jump
Arthur114 Arthur114

2014/4/14

#
I'm newbie here could you explain me where to change/delete code so i can get what i need ? :/ Thanks.
Arthur114 Arthur114

2014/4/14

#
danpost wrote...
I believe that adding acceleration to the speed and falling should be done each act cycle regardless of the 'onGround' state (gravity does not go away just because you have found the ground -- it is just counter-balanced by the force the ground exerts in the opposite direction). Therefore, I feel that the 'boolean jumping' field should be a local variable (if used at all), not an instance field; and its value should be determined each act after falling is executed. The sign of the 'speed' field can determine where to 'stop' the actor when contacting a platform ('bopHead' and 'moveToGround'). To jump, you just need to ask 'Is there a platform at my feet or am I standing on the bottom edge of the world?'. This can be determined without having to use a boolean variable. The following should be a guideline for the vertical movement of an actor:
// add acceleration to speed and fall
// check for ground objects and, if found, adjust position (if speed is less than zero, than actor is bopping head else the actor is landing on ground -- set speed to zero after adjusting position)
// ensure actor is not going under the world (adjust position for bottom edge intrusion)
int halfHeight = getImage().getHeight()/2;
int maxY = getWorld().getHeight()-halfHeight;
if (getY() > maxY) setLocation(getX(), maxY);
// check for jumping (if on ground or at bottom edge of world)
if ((getOneObjectAtOffset(0, halfHeight+1, Ground.class) != null || getY() == maxY) && Greenfoot.isKeyDown("space")) // jump
Atm it's really hard to understand :(
danpost danpost

2014/4/14

#
Arthur114 wrote...
I'm newbie here could you explain me where to change/delete code so i can get what i need ? :/ Thanks.
The code is so far off from my line of thinking that it would be very difficult to just say change this and adjust that. To get close to what I was thinking, you would have to re-write most of the code within the class.
danpost danpost

2014/4/15

#
For an example, I uploaded a demo for your benefit. You can view the code while running the scenario.
You need to login to post a reply.