The Else condtion

This forum is currently in read-only mode.
  • 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.

    <img src="">

    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:

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

    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.

  • 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.

  • Yay lots of good suggestions Thanks guys!

    uhhh...captainoblivious, isn't that the same as in the picture in the first post?

  • 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


    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

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