I Wanna Community

[Lemon Engine] Invert player controls on collision with object?

0 Members and 1 Guest are viewing this topic.

Offline Slith

  • 12
  • OS:
  • Windows 7/Server 2008 R2 Windows 7/Server 2008 R2
  • Browser:
  • Firefox 51.0 Firefox 51.0
  • Playstyle: Keyboard
[Lemon Engine] Invert player controls on collision with object?
« on: February 23, 2017, 12:51:19 PM »
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.

Offline patrickgh3

  • 168
  • stay optimistic! :D
  • OS:
  • Windows 10 Windows 10
  • Browser:
  • Chrome 56.0.2924.87 Chrome 56.0.2924.87
    • Github
  • Playstyle: Keyboard
Re: [Lemon Engine] Invert player controls on collision with object?
« Reply #1 on: February 23, 2017, 07:21:52 PM »
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: http://i.imgur.com/7hp21uY.png. Let me know if that helps!

Offline Slith

  • 12
  • OS:
  • Windows 7/Server 2008 R2 Windows 7/Server 2008 R2
  • Browser:
  • Firefox 51.0 Firefox 51.0
  • Playstyle: Keyboard
Re: [Lemon Engine] Invert player controls on collision with object?
« Reply #2 on: February 24, 2017, 09:00:18 AM »
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.

Offline patrickgh3

  • 168
  • stay optimistic! :D
  • OS:
  • Windows 10 Windows 10
  • Browser:
  • Chrome 56.0.2924.87 Chrome 56.0.2924.87
    • Github
  • Playstyle: Keyboard
Re: [Lemon Engine] Invert player controls on collision with object?
« Reply #3 on: February 24, 2017, 05:21:44 PM »
Glad it helped! If I understand correctly, you could change the if-statement to be
if (global.grav == 1) {
    h = -h;
}

Offline Slith

  • 12
  • OS:
  • Windows 7/Server 2008 R2 Windows 7/Server 2008 R2
  • Browser:
  • Firefox 51.0 Firefox 51.0
  • Playstyle: Keyboard
Re: [Lemon Engine] Invert player controls on collision with object?
« Reply #4 on: February 25, 2017, 05:22:18 AM »
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:
« Last Edit: February 25, 2017, 05:25:10 AM by Slith »