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

2015/7/2

How do I make an object like a ball jump?

spider65 spider65

2015/7/2

#
I am making a game where you have to jump onto platforms. You can also move forward in the air in the game.
danpost danpost

2015/7/3

#
spider65 wrote...
I am making a game where you have to jump onto platforms. You can also move forward in the air in the game.
Define you variables, summarize the steps needed and formulate the specific conditions that would be required to do the individual steps involved. Work on one step at a time. If and when you run into difficulties in coding it, post what you tried and ask for help on the specific step at hand.
spider65 spider65

2015/7/3

#
Sorry but I figured it out. This is my code for it. import green foot. Thx for replying. *; /** * Write a description of class ball here. * * @author (your name) * @version (a version number or a date) */ public class ball extends Actor { /** * Act - do whatever the Skelaton wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ private int gravity = 0; public void act() { if (onGround() && Greenfoot.isKeyDown("up")) { System.out.println("Actor should jump"); System.out.println("Actor at: (" + getX() + ", " + getY() + ")"); jump(); // Add your action code here. } if (Greenfoot.isKeyDown("right")) { move (3); } if (Greenfoot.isKeyDown("left")) { move (-3); } checkFall();//fill this in with what you want, //but add this here: if (onGround() && Greenfoot.isKeyDown("up")) { jump(); checkFall(); } } private void checkFall() { if(!onGround() || gravity < 0) fall(); } private boolean onGround() { int myHeight = getImage().getHeight(); Actor ground = getOneObjectAtOffset(0, myHeight/2, Ground.class); if(ground != null)//if the ground is really there... return true; //(if not, return false) return false; } //this is quite like SPower's method private void fall() { setLocation(getX(), getY() + gravity); gravity++;//the player will accelerate as he falls } private void jump() { gravity -= 20;//or choose 10 or 15 or whatever works } }
spider65 spider65

2015/7/3

#
But I do need a code for a life bar
spider65 spider65

2015/7/3

#
import greenfoot.*;

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ball extends Actor
{
    /**
     * Act - do whatever the Skelaton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    private int gravity = 0;

    public void act() 
    {
        if (onGround() && Greenfoot.isKeyDown("up"))
        {
            System.out.println("Actor should jump");  
            System.out.println("Actor at: (" + getX() + ", " + getY() + ")");  
            jump();  // Add your action code here.
        }
        if (Greenfoot.isKeyDown("right"))
        {
            move (3);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            move (-3);
        }

        checkFall();//fill this in with what you want,
        //but add this here:
        if (onGround() && Greenfoot.isKeyDown("up"))
        {
            jump();
            checkFall();

        }
    }

    private void checkFall()
    {
        if(!onGround() || gravity < 0)  
            fall();
    }

    private boolean onGround()
    {
        int myHeight = getImage().getHeight();  
        Actor ground = getOneObjectAtOffset(0, myHeight/2, Ground.class);  
        if(ground != null)//if the ground is really there...  
            return true;  
        //(if not, return false)  
        return false;  
    }
    //this is quite like SPower's method  
    private void fall()
    {
        setLocation(getX(), getY() + gravity);  
        gravity++;//the player will accelerate as he falls  
    }

    private void jump()
    {
        gravity -= 20;//or choose 10 or 15 or whatever works
    }
}
You need to login to post a reply.