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

2011/6/11

how to prevent player from going through platforms

vollmer1995 vollmer1995

2011/6/11

#
hi! im new to this whole coding thing (first day actually) and i was wondering how you would tell an object not to go through a platform when jumping. any suggestions?
DonaldDuck DonaldDuck

2011/6/12

#
Hi vollmer. In order to do that, you need to check to see if there is an object that intersects your other object at a certain point. To find a platform at your players feet, you need to use this statement... As the getX and Y methods return the center point of the actor, you need to add half of the actors height to reach the feet, and subtract half to reach the head. Actor platform = getOneObjectAtOffset(getX(), getY() + getImage().getHeight()/2, PLATFORM.class); Of course you'd change PLATFORM to whatever your ground actor is. If this returns null, there is nothing there. Otherwise, there is. So... if(platform != null) { setLocation(getX(), getY()+1); } I suggest watching the video tutorials (link here)
vollmer1995 vollmer1995

2011/6/12

#
cool! you wouldnt know how to get a object to move with a moving platform would you? ill upload a new version of what i have so far while i work on that platform thing.
vollmer1995 vollmer1995

2011/6/12

#
http://www.greenfootgallery.org/scenarios/3071
DonaldDuck DonaldDuck

2011/6/12

#
I would in fact. You need to have the player move with the platform... So I suggest introducing a new variable into your code - x_speed. To make the player move currently, you say getX()+1... Instead, you can say getX()+x_speed. This way, when the player is on the platform, the speed of the platform can be added to x_speed. Here's the code for the platform to move...
private void platform_move()
{
    setLocation(getX() + x_speed, getY());
}
Posted below is the way to make the player move with or without the platform. You need to add the variable x_speed into your moving platform class, and the variables x_speed and platform_x_speed into your player class. Everything you're trying to do is in my Super Mario game, so if you want some of the code in-action, go check it out :) Hope this works and helps!
DonaldDuck DonaldDuck

2011/6/12

#
private void player_move()
{
    [platform actor name] moving_platform = ([platform actor name) getOneObjectAtOffset(getX(), getY()+getImage().getHeight()/2, [platform actor name]);
    if(moving_platform != null)
    {
        platform_x_speed = moving_platform.x_speed;
    }
    else
    {
        platform_x_speed = 0;
    }

    if(Greenfoot.isKeyDown("right"))
    {
        x_speed = 1;
    }
    if(Greenfoot.isKeyDown("left"))
    {
        x_speed = -1;
    }

    setLocation(getX() + x_speed + platform_x_speed, getY());
}
This way, The x_speed in the player is not affected by the platform, so you can move right or left, and when it's on a platform, it will move with it too.
You need to login to post a reply.