The Else condtion

For questions about using Classic.

Post » Sat Apr 26, 2008 1:19 pm

I have a sprite I want to use as a button and I want to change it's color depending on a private variable named "color". If it s red when I click on it, I want it to turn green and vice versa.



Doing like in the event sheet above gives permanent green (or permanent red if I change the order of those last two subevents). I thought the ELSE condition might solve my problem, but I can't figure out how to use it, it seems I get a "parsed else" erro no matter where I put it. Can anyone explain to me how the Else condition is supposed used?

And in case I am wrong about that an Else condition would solve the problem, does anyone know a solution?

cap if anyone wants: http://www.mediafire.com/?otvk14dcmk0
B
3
G
4
Posts: 28
Reputation: 1,133

Post » Sat Apr 26, 2008 3:34 pm

I generally use a timer. Insert a global variable named 'timer' and in the event sheet make an event:

If global variable 'timer' is greater than 0, subtract 1 from global variable 'timer'

and in each event

If global variable 'timer' is equal to 0

and in the actions after clicking

set 'timer' to 2

I don't know about using the else command though.
Moderator
B
88
S
32
G
33
Posts: 3,005
Reputation: 27,432

Post » Sat Apr 26, 2008 9:29 pm

ELSE doesn't seem to be working at the moment, I'll take a look in the next build - if it was working, it would solve your problem though :P

The reason it has no effect is because events are read in top-to-bottom order. When you click a sprite, event 5 is run:
Is 'color' 0? Yes it is, so set it to 1.
Event 6 is then run:
Is 'color' 1? Yes it is - event 5 just set it to 1 - so the actions run, and it gets set back to 0.

So the event sets it to 1 then immediately sets it back to 0. If ELSE worked, you'd just make the second condition ELSE.

In this case, the simplest way to toggle between 0 and 1 is to use:

On left clicked Sprite
> Set 'color' to 1 - 'color'

1 - 0 is 1 and 1 - 1 is 0 so it toggles it between 0 and 1 without any subevents.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Sat Apr 26, 2008 11:18 pm

You're right Ash, but that workaround just feels dirty.

Perhaps just the simplest, least creative way works best here.

+Clicked on button
+Button.color == 1
-set button.color to 0

+Clicked on button
+Button.color == 0
-set button.color to 1

The way you suggest will still work better in the event there are more than two colors to cycle. Even better if ELSE is fixed sometime. But for this purpose, two events handle it just fine.
B
3
S
2
G
4
Posts: 310
Reputation: 2,120

Post » Sun Apr 27, 2008 7:52 am

Yay lots of good suggestions :) Thanks guys!

uhhh...captainoblivious, isn't that the same as in the picture in the first post?
B
3
G
4
Posts: 28
Reputation: 1,133

Post » Sun Apr 27, 2008 3:05 pm

CaptainOblivious, your solution still has exactly the same problem, because triggers execute top down order as well.

For more than two states use a reverse ordering, or wait until ELSE is fixed :P

Eg.

On button clicked
value = 0
> Set value to 4 (note final state 4)
value = 1
> Set value to 0
value = 2
> Set value to 1
value = 3
> Set value to 2

(separate event)
value = 4
> set value to 3
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 9 guests