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

2014/7/7

Help walking on platforms

Moho Moho

2014/7/7

#
i need helo walking on platform. my player seems to be falling through the platform. heres the code!
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;  // (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 Actor2
{
    private int speed = 2; //movement speed  
    private int vSpeed = 0; //vertical speed  
    private int acceleration = 2; //gravity effect while falling  
    private int jumpStrength = -8;
    /**
     * 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() 
    {
        move();

    }

    public void move()
    {
        if(Greenfoot.isKeyDown("W"))
        {
            vSpeed = jumpStrength;
            fall();
        }
        if(Greenfoot.isKeyDown("D"))
        {
            setLocation(getX()+3, getY());
            animate();

        }
        if(Greenfoot.isKeyDown("A"))
        {
            setLocation(getX()-3, getY());
        }
        if(Greenfoot.isKeyDown("space"))
        {

        }

    }

    private int imageTimer;  
    private int maxImageTime = 10;  

    public void animate()  
    {  
        if (imageTimer%maxImageTime == 0)  
        {  
            if (imageTimer == 0) setImage("Walk3.fw.png" /* +".png" */); // or whatever  
            else setImage("Spritewalk2.fw.png" /* +".png" */); // or whatever  
        }  
        imageTimer = (imageTimer+1)%(maxImageTime*2);  
    } 

    public void fall()  
    {  
        setLocation(getX(), getY()+vSpeed);  
        vSpeed = vSpeed + acceleration;  
    } 

    public boolean onPlatform()  
    {  
        Actor under = getOneObjectAtOffset (0, getImage().getHeight()/2, Platform1.class);  
        return under != null;  
    }  

    public void checkFall()  
    {  
        if (onPlatform())  
        {  
            vSpeed = 0;  
        }  
        else  
        {  
            fall();  
        }  
    } 

}
NikZ NikZ

2014/7/7

#
getOneObjectAtOffset() checks x and y coordinates. That means the player will have to be exactly in the middle of the platform. getOneIntersectingObject() checks for an object whose image intersects the object in which the method was called.
NikZ NikZ

2014/7/7

#
I believe this might do it:
public void checkPlatforms()
    {
        // reset to original position of sensor after testing is done
        if (getY() < GROUNDLEVEL && !isTouching(Platform.class)) {
            int startY = getY();
            MrSMWorld mworld = (MrSMWorld)getWorld();
            List plats = getWorld().getObjects(Platform.class);
            dontmove = false;
            for (int counter = 0; counter < plats.size(); counter++) {
                Platform platform = (Platform)plats.get(counter);
                if (platform.getY() > getY()) 
                    dontmove = true; 
            }
            if (plats.isEmpty() || dontmove == false) {
                heightToFallTo = GROUNDLEVEL;
            }
            else {
                while (dontmove) {
                    if (getY() >= GROUNDLEVEL) {
                        heightToFallTo = GROUNDLEVEL;
                        dontmove = false;
                    }
                    for (int counter = 0; counter < plats.size(); counter++) {
                        Platform platform = (Platform)plats.get(counter);
                        // if intersecting a platform, get the platform's height
                        if (intersects(platform)) {
                            dontmove = false;
                            heightToFallTo = platform.getY();
                        }

                    }
                    if (dontmove) {
                        setLocation(getX(), getY() + 20); 
                    }
                }
                setLocation(getX(), startY);
            }
        }
    }

This is actually for a sensor, which is invisible and my character just follows it around, making it LOOK like my character is doing everything.
NikZ NikZ

2014/7/7

#
Is there a limit to acceleration? What if you character is falling through the platform because it skipped over it?
You need to login to post a reply.