How to Toggle something with click or key-press?

For questions about using Classic.

Post » Tue Oct 07, 2008 10:24 am

How can I make that only one of the two actions is executed each time, instead of instantly executing both at the same time?

See .cap

It's the same with Mouse or Keyboard, that's why I put both, to see how each one can be fixed.
B
2
S
1
G
4
Posts: 92
Reputation: 1,384

Post » Tue Oct 07, 2008 10:32 am

You can use condition "Else" from "System" - just put this condition as event below event you want have else-d. I don't have Cons right now so can't see CAP.

Event Key "K" pressed : set Value to (Value + 1) % 2
-> SubEvent "Value = 1" : some actions
-> SubEvent "Else" : action if Value NOT = 1

Something like this should solve toggling.
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

Post » Tue Oct 07, 2008 10:48 am

It's a common problem. BROO is right, use ELSE to fix it. The reason your current application doesn't work is because events and triggers run in top to bottom order. The first trigger sets the animation frame to 1. The second trigger tests if it's on animation frame 1 (which it is, it just got changed to it) and as a result changes it straight back to 2. ELSE ensures only one action happens per trigger, which fixes it.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Tue Oct 07, 2008 4:38 pm

[quote="Ashley":3sntjvd2]ELSE ensures only one action happens per trigger, which fixes it.[/quote:3sntjvd2]

All right... I learned something today :)

I've been doing my switch toggles since before Else came out, and never bothered to rethink how to do it. Old habits die hard, I guess.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Oct 08, 2008 12:03 am

Alright... that worked.
But what If I want to have more than two actions?

EDIT: Nevermind, got it.
B
2
S
1
G
4
Posts: 92
Reputation: 1,384

Post » Wed Oct 08, 2008 12:26 pm

Again, I can't do it right.
I want something as simple as this:

- Click on object
If 1, set to 2
If 2, set to 3
If 3, set to 1

Variables, animation frames, whatever.

Also, If I click on something to take me to the next layout, and on that layout I have something on the same position as the previous object also with an action set when is clicked, that action happens immediately.

This even happens when I set "Button is released" instead of "On click".

Please help me, I'm kinda sick of being stuck with something this simple to make the menu for my game. :(
B
2
S
1
G
4
Posts: 92
Reputation: 1,384

Post » Wed Oct 08, 2008 12:45 pm

http://upload.namelezz.net/downloader.p ... button.cap

Push the button, the % means modulo.
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Post » Wed Oct 08, 2008 1:11 pm

[size=85:emvn93xm](Don't have Cons now, can't see CAP)[/size:emvn93xm]

When making menu (for example with 4 options: 0, 1, 2, 3), you'd probably have something like this:
[code:emvn93xm]-> "UP" pressed: set value to (value - 1) % 4
-> "DOWN" presset: set value to (value + 1) % 4[/code:emvn93xm]
Since I didn't know how program deals with modulo of negative number, I did tend to do it this way:
[code:emvn93xm]-> "UP" pressed: set value to (value + 4 - 1) % 4
-> "DOWN" presset: set value to (value + 1) % 4[/code:emvn93xm]
Maybe I'm oversensitive on this stuff, but I don't want have surprising results ^^.
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

Post » Wed Oct 08, 2008 2:54 pm

[quote="BROO":3onoiwue]
Since I didn't know how program deals with modulo of negative number, I did tend to do it this way:
[code:3onoiwue]-> "UP" pressed: set value to (value + 4 - 1) % 4
-> "DOWN" presset: set value to (value + 1) % 4[/code:3onoiwue][/quote:3onoiwue]Why not just
[code:3onoiwue]-> "UP" pressed: set value to (value + 3) % 4
-> "DOWN" presset: set value to (value + 1) % 4[/code:3onoiwue]
Little error with really decoration problem :mrgreen: , also i made almost the same thing but only with adding.
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Post » Wed Oct 08, 2008 6:09 pm

Writing "4-1" provides information about "why this is 3?". You can say it's a calculation that documents code. When performance isn't vital in algorithms, I'd rather try to make code easier to understand.

It'd be the best if you created some sprite called "Conf" and put PV like "ElementsCount" and then done events like:
[code:1cjhwone]-> "UP" pressed: set value to (value + Conf('ElementsCount') - 1) % Conf('ElementsCount')
-> "DOWN" presset: set value to (value + 1) % Conf('ElementsCount')[/code:1cjhwone]
This'd be more flexible and useful for next appliactions.
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest