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

2017/4/16

How to get a character to move with another moving platform

1
2
sinlessclown sinlessclown

2017/4/16

#
I want the charachter to be able to move along with a platform that is also moving.
Yehuda Yehuda

2017/4/16

#
In the platform you can try this:
        Actor character = getOneIntersectingObject(/*the name of your character's class*/.class);
        if (character != null) {
            character.setLocation(getX(), getY() - getImage().getHeight() / 2 - character.getImage().getHeight() / 2);
        }
I don't know if this is very good because it will lock the character to the middle of the platform.
sinlessclown sinlessclown

2017/4/16

#
This is the code for my moving platform which just moves back and forth when it reaches the end of the world.
private int direction = 3;   
    public void act() 
    { 
        move(); 
    }
 
    private void move() 
    {  
        if ((direction < 0 && getX() == 0) || (direction > 0 && getX() == getWorld().getWidth()-1)) direction = -direction;  
        move(direction);
    }
The platform is in the air and there is a seperate ground that doesn't move.
Yehuda Yehuda

2017/4/16

#
With the following code the platform will move everything that's touching it:
        for (Object obj : getIntersectingObjects(Actor.class)) {
            ((Actor) obj).setLocation(((Actor) obj).getX() + direction, ((Actor) obj).getY());
        }
You can add it in right after line 4 or 10.
sinlessclown sinlessclown

2017/4/16

#
The code you gave me is moving all the objects beside it left or right however it isn't moving the player which is on top of the block.
Yehuda Yehuda

2017/4/16

#
You can change line 9 to:
        if (direction < 0 && getX() <= getImage().getWidth() / 2
                || direction > 0 && getX() >= getWorld().getWidth() - getImage().getWidth() / 2) {
            direction = -direction;
        }
to make the platform look like it's actually bouncing off the edge's of the world. It doesn't have to be written in four lines, you can write it as one but then it will be quite long.
sinlessclown wrote...
The code you gave me is moving all the objects beside it left or right however it isn't moving the player which is on top of the block.
You can change the Actor.class to the name of the character's class so that it won't move anything else. The only reason it won't move the character would be because the character isn't touching the platform, you have to make sure the character is sitting on top of the platform. If you can't do this then show the code that makes the character land on top of the platform.
sinlessclown sinlessclown

2017/4/16

#
I meant that if the charachter was to the side of the moving block, on another platform, it was pushed left or right (a good thing) however when I jumped on top of the platform, the player did not move along with the block. This is a download link to my platformer: https://www.dropbox.com/s/vjtvj5fklaompp9/Greenfoot%20Platformer?dl=0
danpost danpost

2017/4/16

#
Try the following instead of the code Yehuda suggested:
setLocation(getX(), getY()-1);
Actor player = getOneIntersectingObject(Player.class);
if (player != null) player.setLocation(player.getX()+direction, player.getY());
setLocation(getX(), getY()+1);
Replace 'Player.class' with the correct class name if incorrect.
Yehuda Yehuda

2017/4/16

#
What did you upload, all I see is a big blank white page. Are you trying to show me the scenario, an image a video? Just use this site (if you're showing me something in your dropbox folder then I probably won't be able to see it).
sinlessclown sinlessclown

2017/4/16

#
Danpost- Your code worked well, I was wondering if you could explain what it does since I'm not sure how the code itself works.
Yehuda Yehuda

2017/4/16

#
Since the platform isn't touching the player, he moves it up a pixel (to make player not be null), then moves the player along with the platform (along the x-axis) then moves it back down a pixel. (Simpler then showing the player class code.)
sinlessclown sinlessclown

2017/4/16

#
Ohh, I see, and also Yehuda my bad. Just uploaded the entire platformer folder on dropbox thinking that it would work.
sinlessclown sinlessclown

2017/4/16

#
Another question. Is it possible to stop the platform from moving the entire way from one worlds edge to another, and instead make it move a set distance forward and back.
Yehuda Yehuda

2017/4/16

#
In the if (on line 9) just change the numbers. if ((direction < 0 && getX() == 0) || (direction > 0 && getX() == getWorld().getWidth()-1)) or if (direction < 0 && getX() <= getImage().getWidth() / 2 || direction > 0 && getX() >= getWorld().getWidth() - getImage().getWidth() / 2) All the bold code makes the platform stop at the world's edge.
sinlessclown sinlessclown

2017/4/16

#
How do I make it so that the moving block moves to the right wall first?
There are more replies on the next page.
1
2