How do I change directions with the same button?

  • Essentially, the whole game is controlled by one button. The character moves automatically (left or right) and with the same button the character moves in the opposing direction. Can anyone help me on this? All I can get is moving in the one direction (right) only. Thanks

  • add a boolean to the button, call it "right" and default value, "true"

    then

    event

    button clicked

    action

    toggle boolean button.right

    subevent 1

    boolean button.right = true

    action

    set angle of motion 0

    subevent 2

    boolean button.right = false (inverted)

    action

    set angle of motion 180

  • Sorry for not being clearer. By button I mean a physical button rather than an on-screen one. What if I just want it to be any button on the keyboard, controller or tap of the screen?

  • Then use a hidden ClickedButtonSprite or globals with on key pressed...

  • How do I implement this part

    subevent 1

    boolean button.right = true

    Because when issuing a subevent there is no set boolean option available for my button. Thanks

  • Instead of a boolean instance variable, use a global variable (for example named "Direction") and on the key press set its value to either 0 or 1.

    Then in the event that makes your character move, check what is the value of "Direction" and make the character move accordingly (when the value is 0 the character is going right and the sprite is not mirrored, when the value is 1 the character moves left and the sprite is mirrored).

    If this still doesn't apply to your project, consider posting/attaching the exact capx for your project so we can investigate and answer according to the specificities of what you've implemented so far.

  • Thanks for all the advice so far. Here is where I am at:

    Now, because I don't want the object to move in either direction immediately, I disabled its movement until the player touches the screen. When that happens, the object automatically moves to the right (so I chose bullet behaviour for this) and when I touch the screen again, the object moves automatically in the opposite direction. This works.... once and only once. So what happens is when I touch the screen the game begins and the object moves right and when I touch again the object moves to the left... but keeps moving to the left regardless of how many times I continue to touch the screen.

  • Right click the first "Is direction" and select, "invert"

    Add an "Else" to the second "Is direction"

  • Tried that, it just makes the object keep moving to the left.

  • You have the angles wrong. 0 is for the LEFT direction and 180 for the RIGHT direction by default it goes to the side. So 180 and -180 is the same direction. You can look at this. The sprite has a instance variable to hold its current direction versus using a button. I set the speed to 0 instead of disabling it.

    Here is an image to better understand. The angles (**FIXED I had it reversed)

    [attachment=1:29mjvdr7][/attachment:29mjvdr7]

    **I just saw that you wanted a button but not the standard one. You can have a sprite and when there is a tap on it to execute the ACTIONS. Also fixed the file that it will only set the speed if it is zero.

  • Thank you so much for the explanation and the example given. When I first looked at your capx I thought "Now it's so obvious.... ... .. . wait what?" but after looking through it and going through the same process on my capx I have better understood it. Really nice and neat way to toggle between two different directions. Again, thank you and thank you to everyone who chipped in this topic!

  • Nice!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Nice!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)