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

2013/5/4

Elevator

GrimCreeper312 GrimCreeper312

2013/5/4

#
what would be the easiest way to make an elevator that carriers the Player class but lets the player move off of it.
Gevater_Tod4711 Gevater_Tod4711

2013/5/4

#
Well that's realy not easy. In this scenario I used a elevator platform called MovingObject. If you download it and have a look at the code that may help you.
GrimCreeper312 GrimCreeper312

2013/5/5

#
Ive got the elevator working but there is one problem when the player goes to get off the elevator he still goes up and down with it but can move side to side here is the code i used:
private void Elevator()
    {
        if(!onElevator()){
            elevator = false;
        }
        if(onElevator())
        {
                elevator = true;
                if(!Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("left"))
                 setLocation(getX(), Elevator.y + 30);
                else if(Greenfoot.isKeyDown("right")){
                  elevator = false;
                  setLocation(getX() + 4.5, Elevator.y - 30);
                  }
                 else  if(Greenfoot.isKeyDown("right")) {
                  elevator = false;
                  setLocation(getX() - 4.5, Elevator.y - 30);
                }
        }
        if(elevator == true)
        {
            setLocation(getX(), Elevator.y - 30);
        }
       
        
    }

public boolean onElevator()
    {
        Actor actor = getOneIntersectingObject(Elevator.class);
        return actor != null;
    }

public void fall()
    {
        if(!onGround() && !onPlatform())
        {
            if(Greenfoot.isKeyDown("right")) setLocation(getX() + 3, getY() + fallSpeed);
            else if(Greenfoot.isKeyDown("left")) setLocation(getX() - 3, getY() + fallSpeed);
            else setLocation(getX(), getY() + fallSpeed);
            Sword.life = 0;
        }
    }
Gevater_Tod4711 Gevater_Tod4711

2013/5/5

#
In my game I used that the elevator tells the player how much he has to move up or down. That concerns on how much the elevator moves. Maybe that'll work better.
danpost danpost

2013/5/5

#
The logic of your 'fall' method does not look correct. It says "if in air, direct right or direct right or fall." It should be more like "if in air, fall; else, direct right or direct left." And, yes. When the player is on the elevator, the elevator should detect and move the player up and down as it moves.
You need to login to post a reply.