doodle/dev-assets/doodads/buttons/sticky.js
Noah Petherbridge 4d08bf1d85 Switch JavaScript engine to goja
* Switch from otto to goja for JavaScript engine.
* goja supports many ES6 syntax features like arrow functions,
  const/let, for-of with more coming soon.
* Same great features as otto, more modern environment for doodads!
2022-01-16 20:09:27 -08:00

36 lines
674 B
JavaScript

function main() {
let pressed = false;
// When a sticky button receives power, it pops back up.
Message.Subscribe("power", (powered) => {
if (powered && pressed) {
Self.ShowLayer(0);
pressed = false;
Sound.Play("button-up.wav")
Message.Publish("power", false);
Message.Publish("sticky:down", false);
}
})
Events.OnCollide((e) => {
if (!e.Settled) {
return;
}
if (pressed) {
return;
}
// Verify they've touched the button.
if (e.Overlap.Y + e.Overlap.H < 24) {
return;
}
Sound.Play("button-down.wav")
Self.ShowLayer(1);
pressed = true;
Message.Publish("power", true);
Message.Publish("sticky:down", true);
});
}