Login | Register

Platform movement

October 9, 2006, 19:16 by Calle
[loading]
-->
<b>Question:</b> This is my problem.... I need a code, so when an enemy goes to the edge of a platform, it will stay on the platform.

<b>Answer:</b> This is a very common problem which occurs to anyone who wants to make a platform game. I will therefore quote some solutions which came up in the topic at GMC. Some of them are good, applies to all kind of platforms in any situations, some are less good and does only apply when the programmer have chosen to use speed etc.

<b>Solution one:</b>
if hspeed>0 and place_free(x+1,*) *=y+the sprites+1 height
hspeed=-hspeed
if hspeed<0 and place_free(x-1,*) *=y+the sprites height+1
hspeed=-hspeed


This is a highly functional solution. It's good, because it's easy to implement. It only works with the variable speed though, so if the case is such that the programmer moves his character using another technique then it won't work.

<b>Solution two [my solution]:</b>
if (place_empty(x+4,y+sprite_height+1)) {
direction += 180;
}


I certainly doesn't like to waste space, and this is also very easy to implement.

<b>Solution three:</b>
"or you could do like the platform tut says and create an invisible marker, when the monster hits it, he turns around. "

This is a classic. Very good if you are not very well with programming.

Now let me quote my remarks on my own solution:
"That'll make your character change direction to the opposite one it reach the end of the platform. However, if your character uses any other method, like perhaps it doesn't go by direction at all, then you should change it a bit. But the principle is the same. If you use speed replacing +4 with +speed+1 might be a good idea. Otherwise 4 should be the size of the step plus one. The code should go in step event."

I hope I have made my point clear and that anyone who has this kind of problem should be able to solve it.

<b>Recommended reading:</b>
http://gmtutorials.com/article/normal/jump-through-plattforms/

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games