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

2021/2/25

Moving platform up and down

Genota Genota

2021/2/25

#
I want to create something like an elevator, but I dont have an working idea yet. Can someone give me an example/ideas how to create it?
danpost danpost

2021/2/25

#
First create a non-moving platform, then add the movement (using speed and direction) and collision/carrying codes.
Genota Genota

2021/2/26

#
I tried something, but how can I change the direction for a y coordinate?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Elevator_Plattform here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Elevator_Plattform extends Ground
{
    private int speed = 0;
    private int count = 0;
    boolean hitByProjectile = false;
    private int direction = 1;
    private int verticalSpeed = 3;
    private int verticalSpeed2 = -3;
    private int horizontalSpeed = 0;
    /**
     * Act - do whatever the Elevator_Plattform wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAround(horizontalSpeed, verticalSpeed);
        count++;
    }  
        public void moveAround(int changeX, int changeY)
    {
        int x = getX();
        int y = getY();
        int newX = x + changeX;
        int newY = y + changeY;
        setLocation(newX,newY);
        count = (count+1)%120;
        
        if (count == 0) speed = -speed;
        { 
            move(horizontalSpeed, verticalSpeed2);
        }
    }
    public void move(int changeX, int changeY)
    {
        int x = getX();
        int y = getY();
        int newX = x + changeX;
        int newY = y + changeY;
        setLocation(newX,newY);
        
    }
}
danpost danpost

2021/2/26

#
First, get rid of all stuff not currently being used and clean up code a bit:
public class Elevator_Platform extends Ground
{
    private int speed = 3;
    
    public void act() 
    {
        setLocation(getX(), getY()+speed);
        count = (count+1)%120;
        if (count == 0) speed = -speed;
    }
}
Genota Genota

2021/2/26

#
Yeah Okay, thank you... Just one more thing. The Player is going through the Platform, when its moving up.
danpost danpost

2021/2/26

#
Genota wrote...
Yeah Okay, thank you... Just one more thing. The Player is going through the Platform, when its moving up.
Both will need to check for each other when moving. When platform moves and touches player, should the player be pushed? If so, what is wanted if player is pushed into the ground (or another object)?
Genota Genota

2021/2/26

#
Like an elevator. So if the platform is going up, the player should be go up too (pushed). In my world, it will never happen, that the player get pushed into another object/ground or anything else, because of the design of the map.
danpost danpost

2021/2/26

#
Genota wrote...
Like an elevator. So if the platform is going up, the player should be go up too (pushed). In my world, it will never happen, that the player get pushed into another object/ground or anything else, because of the design of the map.
Okay:
public class Elevator_Platform extends Ground
{
    private int speed = 3;
     
    public void act() 
    {
        setLocation(getX(), getY()+speed);
        Actor player = getOneIntersectingObject(Player.class);
        if (player != null) player.setLocation(player.getX(), player.getY()+speed);
        count = (count+1)%120;
        if (count == 0) speed = -speed;
    }
}
You need to login to post a reply.