How do I change directions with the same button?

Just started using Construct 2? Post your questions here

Post » Tue Mar 10, 2015 8:48 pm

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
B
8
S
2
G
1
Posts: 34
Reputation: 623

Post » Wed Mar 11, 2015 12:33 pm

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
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Mar 11, 2015 8:51 pm

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?
B
8
S
2
G
1
Posts: 34
Reputation: 623

Post » Wed Mar 11, 2015 9:37 pm

Then use a hidden ClickedButtonSprite or globals with on key pressed...
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Mar 11, 2015 10:02 pm

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
B
8
S
2
G
1
Posts: 34
Reputation: 623

Post » Thu Mar 12, 2015 12:53 pm

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,293
Reputation: 70,769

Post » Thu Mar 12, 2015 7:09 pm

Thanks for all the advice so far. Here is where I am at:
Image
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.
B
8
S
2
G
1
Posts: 34
Reputation: 623

Post » Thu Mar 12, 2015 7:38 pm

Right click the first "Is direction" and select, "invert"
Add an "Else" to the second "Is direction"
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Thu Mar 12, 2015 8:03 pm

Tried that, it just makes the object keep moving to the left.
B
8
S
2
G
1
Posts: 34
Reputation: 623

Post » Thu Mar 12, 2015 8:56 pm

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)

DirectionsFixed.png


**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.
You do not have the required permissions to view the files attached to this post.
B
23
S
4
G
2
Posts: 364
Reputation: 2,392

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: plinkie and 2 guests