Fangames > Programming Questions
[Lemon Engine] Invert player controls on collision with object?
(1/1)
Slith:
Well, I feel kinda silly since this has been done in a lot of fangames and also I tried to make this happen for HOURS and nothing seems to work. :(
Basically all I want is to switch left and right buttons. Hope someone can help me.
patrickgh3:
Hi Slith! Here's how I would do it.
We're gonna have a global variable called global.invertControls, and if it's 0, do normal movement, and if it's 1, flipped controls. Let's put this line of code at the beginning of the world object's create event to enable it by default:
global.invertControls = 1;
Now, for the actual control inverting. After looking at Lemon's engine, the code for walking left and right is really complicated and weird, so don't feel bad for not understanding it. (I'd recommend Seph's or Yoyo's engine for new projects in the future). In the player's step event, at the top, the h gets set to 1, 0, or -1, depending on whether walking right, standing still, or walking left. (Don't worry about not understanding the code). So, if we simply multiply h by -1, we'll reverse our direction! Insert the following code AFTER h gets set to either global.R or global.L:
if (global.invertControls) {
h = -h;
}
Your code should look like this now: https://i.imgur.com/7hp21uY.png. Let me know if that helps!
Slith:
Thanks for the answer, I kinda get how this engine works now. Also everything works fine :atkHappy:
But I have one more little problem. Can I make the inverted controls work, when global.grav = 1? For the gimmick, both the view_angle and gravity sometimes switches (it's a weird little room), the inverted controls during changed gravity aren't 100% necessary but still would be nice.
patrickgh3:
Glad it helped! If I understand correctly, you could change the if-statement to be
if (global.grav == 1) {
h = -h;
}
Slith:
Ah, I found it. The engine changes the object "player" to object "player2" as soon as "global.grav = 1;"
That's why it did not work... All I had to do, was put the same if (global.invertControls) statement into the step event of the player2 object... getting there!! :atkHappy:
Navigation
[0] Message Index
Go to full version