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

2013/11/30

Problem with Platforms

JasonZhu JasonZhu

2013/11/30

#
I am trying to make a platform game, but I can't make the player stay above the bricks. I am using a shoe as a detection object. I noticed that because the act iterations are choppy, the player passes the intersection of the top of the brick... If you view my code, you'd notice that I've tried working around this, but it's not working still... Anyone who has a good background with platforms? Help is greatly Appreciated! Shoe Class:
public class Shoe extends Actor
{
    Player player;
    Brick brick;

    public Shoe(Player player)
    {
        this.player = player;
    }

    public void act() 
    {
        if(player!=null){
            setLocation((int)player.getExactX(),(int)(player.getExactY() + player.getImage().getHeight()/2));
        }
        if(getY()>690){
            die();
        }
        if(aboveBrick()==true){
            player.setLocation(getX(),brick.getY()-8);
        }
    }    

    public Object getIntersectingBrick()
    {
        if(player!=null){
            Object brick = getOneIntersectingObject(Brick.class);
            if(brick != null){
                if(((Brick)brick).getY() - ((Brick)brick).getImage().getHeight()/2 > 
                getY() + getImage().getHeight() / 2){
                    return brick;
                }
            }
        }
        return null;
    }

    private void die()
    {
        for(int a=0;a<50;a++){
            getWorld().addObject(new Part(),getX(),getY());          
        }
        player = null;
        getWorld().removeObject(this);
    }
    
    private boolean aboveBrick()
    {
        if(getX()>brick.getX()-15&&getX()<brick.getX()+15&&getY()<brick.getY()-30){
            return true;
        }else{
            return false;
        }
    }
}
Player Class:
public class Player extends SmoothMover
{
    private static final Vector GRAVITY = new Vector(90,0.2);
    private static final Vector JUMP = new Vector(270,5);
    private static final Vector LEFT = new Vector(180,0.7);
    private static final Vector RIGHT = new Vector(0,0.7);
    private static final float FRICTION = 0.05f;
    private Shoe shoe;

    public Player()
    {

    }

    public void act() 
    {
        if(shoe == null)return;
        Object brick = shoe.getIntersectingBrick();
        if(brick!=null){
            if(getMovement().getDirection()>90&&getMovement().getDirection()<180){
                getMovement().setDirection(180);
            }else if(getMovement().getDirection()<90&&getMovement().getDirection()>0){
                getMovement().setDirection(0);             
            }else if(getMovement().getDirection()==90){
                getMovement().setNeutral();
            }
            getMovement().scale(FRICTION);            
            if(Greenfoot.isKeyDown("space")){
                jump();
            }  
            if(Greenfoot.isKeyDown("left")){
                steer(true);
            }  
            if(Greenfoot.isKeyDown("right")){
                steer(false);
            }  

        }else{
            fall();            
        }   
        move();
        if(getY()>690){
            die();
        }
    }  

    private void die()
    {
        for(int a=0;a<50;a++){
            getWorld().addObject(new Part(),getX(),getY());          
        }
        getWorld().addObject(new GameOver(),500,350);
        shoe = null;
        getWorld().removeObject(this);        
    }

    public void setShoe(Shoe shoe)
    {
        this.shoe = shoe;
    }

    private void fall()
    {
        addForce(GRAVITY);
    }

    private void jump()
    {
        addForce(JUMP);
    }

    private void steer(boolean left)
    {
        if(left){
            addForce(LEFT);
        }else{
            addForce(RIGHT);
        }
    }
}
Brick Class: Just an Object
Entity1037 Entity1037

2013/11/30

#
You can detect where the shoe is about to be and see if there's an object there, and if there is, then align the shoe next to the object so it doesn't move normally and go into it. I've made a collision detection scenario that uses this concept if you'd like to have a reference (just don't copy it exactly). http://www.greenfoot.org/scenarios/9172
You need to login to post a reply.