MalifaciousGames/Mali-s-Macros

Listen macro not working as I assumed

Tranberry opened this issue · 6 comments

<<listen>>
    <h1>Passgae header</h1>
    <p>Press [Space] to play.</p>
    <div id="spacebar">
    </div>
    
    <style>
        #spacebar {
            ...
        }
    </style>
    
    <<when 'keypress'>>

    <<if _event.code === 'Space'>>
        <<goto "anotherPassage">>
    <</if>>

<</listen>>

I would assume that this would work, but instead of the <<goto "anotherPassage">> we tried anything from <<run>> to <<script>>. Please tell me if it is something silly I missed. 💛

The issue is that <<listen>> only works for events that happen within its container, so the keypress happens on document level instead without ever bubbling through the container.

This would work (tho it's not much of a solution):

<<listen>>

    <<textbox '_whatever' ''>>
    
<<when 'keypress'>>

    <<if _event.code === 'Space'>>
        <<goto "anotherPassage">>
    <</if>>

<</listen>>

Chapel's event macro is more along the lines of what you're looking for, that said, my own <<on>> macro would work if it had a localised _event variable the same way <<listen>> does...

This already works:

<<on 'keypress' '' onInit false>>
        <<goto "anotherPassage">>
<</on>>

Except you can't check for a keypress in particular...

we weren't being idiots then

Thanks Mali for the great insight! 💛

Quickest fix for now would be a simple:

<<run $(document).one('keypress', (e) => {
    if (e.code === 'Space') { Engine.play('anotherPassage') };
})>>

yeah, but the plan was to write less js/jq. 👺

Solved with the new <<on>> version, readme still needs to be written...