Search code examples
haskellsdlyampa

Yampa value switch


I have an event SF Input (Event ()) that represents the key-up of the space key. I have some gravity and every time the user releases the key, it must switch between 1 and (-1). So I did this:

gravity <- accumHold 1 <<< (arr $ tag (*(-1))) <<< keyUp SDLK_SPACE -< ev

But what it does is switching the value up and down on repeate until I press the key again. I have no idea where I did wrong there.

The full source is found here, maybe someone can help me.


Solution

  • The obvious answer is to use a switch:

    gravity = dir 1
    dir x = switch (constant x &&& keyUp SDLK_SPACE) (const (dir (-x)))
    

    This is certainly also possible without a switch by mapping over the event values, but the switch seems to be the natural solution. You can also use Netwire, which makes this a lot simpler:

    hold (iterateW negate 1 . keyUp SDLK_SPACE)
    

    The iterateW wire will be available in Netwire 4. Until then you can write it this way:

    hold (accum 1 . pure negate . keyUp SDLK_SPACE)