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

2011/6/4

tutorials

thetoaster thetoaster

2011/6/4

#
i am trying to learn to use greenfoot an wanted to make a platformer but the tutorial(s) is out of date so some of the old rules/code dont work anymore anyone know the new ways?
webmessia webmessia

2011/6/4

#
I've taught how to write platformers thousands of times and this way is a pretty smart way of doing it. First off create a sub-class of World and call it PlatformerWorld (or anything else that follows the Java naming rules). Then create a sub-class of Actor called Person and another called Platform. You don't need to add any code to the world or platform classes. But here is some code for the person. Copy in the code see what it does and tinker, that will get you started. I should really make a full tutorial on this but see what you can learn by looking at this code. Most functionality can be added by writing methods for implementing different forces. If you need any extra help just reply to this thread :)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The person who will move about platforms.
 * @author - Michael "webmessia"
 */
public class Person  extends Actor
{
    //Basic Physics Variables
    private double positionX;
    private double positionY;
    private double velocityX = 0;
    private double velocityY = 0;
    private double accelerationX = 0;
    private double accelerationY = 0;
    
    //Force Values
    private double gravityY = 0.1;
    private double jumpForce = 3.5;
    
    public void addedToWorld(World w){
        //We set our position to the position which we were added to the world at.
        positionX = getX();
        positionY = getY();
    }
    /**
     * Act - do whatever the Person wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Apply Forces to change acceleration to move the person
        applyGravity();
        applyJumpForce();
        //Basic Physics
        velocityX += accelerationX;
        velocityY += accelerationY;
        positionX += velocityX;
        positionY += velocityY;
        accelerationX = 0;
        accelerationY = 0;
        setLocation((int)positionX,(int)positionY);
    }    
    /**
     * Accelerates the person downwards except for when they are on a platform
     */
    private void applyGravity(){
        // Check For collision with platform
        Actor coll = getOneIntersectingObject(Platform.class);
        if(coll == null){ 
            //This is run when the person is not touching a platform
            //This code adds gravity to our total acceleration.
            accelerationY += gravityY;
        } else { 
            //This is run once we hit a platform
            //We make our person stop moving in the y direction. (up/down direction).
            velocityY = 0;
        }
    }
    /**
     * Accelerates the person upwards if space is pressed and we are on a platform
     */
    public void applyJumpForce(){
        Actor coll = getOneIntersectingObject(Platform.class);
        if(coll != null){
            //This is run when the person is touching a platform
            if(Greenfoot.isKeyDown("space")){
                accelerationY -= jumpForce;
            }
        }
    }
}
thetoaster thetoaster

2011/6/4

#
Thanks, I'll give that a go
Crazunitic Crazunitic

2013/11/5

#

"Cannot find symbol - class Platform"

:L Im no coding/Greenfoot expert.....that's why I look up things like this, but no matter how I tweak the code I can't find out how to correctly define this class
You need to login to post a reply.