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

2017/6/12

Check distance to ground

LadyKataka LadyKataka

2017/6/12

#
First let me mention that I am pretty new to programming. I am programming a Jump 'n Run and my problem is the following: When my character falls she is supposed to stop by checking whether she is on ground. However, when the fall speed gets too great she only first has the chance to check when she already is inside a block. So I want to check the distance to the ground below her if there is any and whether that distance is greater than her fall speed - if so she shall not fall as far as the fall speed but the calculated distance instead. I thought it might work with the method getObjectsInRange when filtering what has the same X and a Y smaller than my character's but I am very unsure whether this will actually work, or how to create the necessary method. Can someone help?
Super_Hippo Super_Hippo

2017/6/12

#
You could let her move into the block and then move upwards until she is not in the block anymore.
//after moving
while (isTouching(Ground.class)) setLocation(getX(), getY()-1);
However, this does not prevent her from moving into the ground from other directions. To do that, you would need to check from which direction she entered the block and move back accordingly.
You need to login to post a reply.