I've noticed many people don't know how to make a moving platform. You can have a good platforming system that lets you jump on backgrounds & active objects that count as obstactles, but the second you make those active objects move, problems start. Without additional code, a moving platform will just drop you off of it, or wedge you inside of it. It moves, you don't.
Heres how to fix that. Have a detector of some kind, which detects when you are standing on a moving platform. Calling it "Bottom Detector". For example, we have an event:
Always:
Set X of "Bottom Detector" to (X("Player"))
Set Y of "Bottom Detector" to (Y("Player") + 10)
Now we can simply detect if you are on top of a platform as this:
"Bottom Detector" is overlapping "Object":
Making sure we place that line BELOW the one that positions the detector. Now, the trick is to see how far your platform traveled since the last frame of gameplay, and move your character by that same amount. We can have a seperate alterable variable, call it "DifferenceX", and set it to X("Bottom Detector") BEFORE the platform is moved, and then compare it to the position AFTER the platform moved. Then simply move the player by that same amount. In the end, our code looks like this:
Always:
Set X of "Bottom Detector" to (X("Player"))
Set Y of "Bottom Detector" to (Y("Player") + 10)
"Bottom Detector" is overlapping "Object":
Set "Object" Flag 0 On.
Set DifferenceX of "Object" to (X("Object")).
Set DifferenceY of "Object" to (Y("Object")).
(Insert platform movement code inbetween these events)
If "Object" Flag 0 is On:
Set X("Player") to (X("Player") - (X("Object") - DifferenceX("Object")))
Set Y("Player") to (Y("Player") - (Y("Object") - DifferenceY("Object")))
Set "Object" Flag 0 Off.
Now, whenever our platform moves and we are on top of it, we'll move with it.
|