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

2019/5/5

Platforms

FortniteMaster69 FortniteMaster69

2019/5/5

#
I wrote this code so that my character can stand on this platform walk on it etc. But that doesn't happen and my character seems to just go through it. When I compile there aren't any syntax errors, so is there something missing from the code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public boolean isOnSolidGround()
{
    boolean isOnGround = false;
     
    if (getY() > getWorld().getHeight() - 30) isOnGround = true;
     
    int imageWidth = getImage().getWidth();
    int imageHeight = getImage().getHeight();
    if (getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, Platform.class) != null||
        getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, Platform.class) != null)
        isOnGround = true;
         
    return isOnGround;
}
danpost danpost

2019/5/5

#
FortniteMaster69 wrote...
is there something missing from the code? << Code Omitted >>
The method would work, if used in a manner for which it is coded. It is coded to check for "standing" on a platform or on the world surface. The method should only be used after moving AND after adjusting for obstacles (including platforms) or world surface collisions. Furthermore, the adjustment for platform collision (when falling, specifically) cannot place the actor above the platform as the isOnSolidGround method checks for base of that actor's image still touching the platform.
FortniteMaster69 FortniteMaster69

2019/5/6

#
I didn't understand a single thing you said. I am very new to Greenfoot, and have to use it for an assessment at school but our teacher hasn't taught us anything, so I am just watching youtube videos and copying them. I watched a guy and copied his code exactly but his worked and mine didnt. I have uploaded the scenario to: Scenario with the source code. I'm not sure what to change, so can you explain it to me like I am a 5 year old? Thank you for your help.
danpost danpost

2019/5/6

#
You have a couple of potential issues. First, you do not zero the velocity when isOnSolidGround returns a true value.
1
2
3
4
// change this line
if (isOnSolidGround()) ;
// to this
if (isOnSolidGround()) velocity = 0;
Next is that your character's image has a lot of transparency around it, making possible for your actor to "stand" hovering over the platform. Another is that you only check for the base of the image touching a platform and if your actor moves up or down too fast (which wont be much, since your platform has very little height to it), it could potentially still miss detecting it and pass right over it.
FortniteMaster69 FortniteMaster69

2019/5/6

#
Thanks for the help the velocity = 0 worked. So as soon as the actor touches solid ground they do not move. Thank you!
You need to login to post a reply.