I am currently updating my game to use the computer cursor via the Browser-Events extension. I have a splash text appear upon clicking on a sprite. I am wondering how I can hide this text after a set amount of time. Ex. Cursor clicks on sprite, splash text appears, wait 3 seconds, splash text hides.
Ok, so basically if you are coding in blocks the short answer is “lol no, not unless I figure out how to make an extension that does it for you” and if you aren’t using blocks… well…
This seemed like a nice challenge, but it beat me
@richard is there any way to make a simulated button push happen while the splash dialogue is running? It pushes the scene, and I can’t find a way to run a simulated button push while inside the scene function! I tried adding a scene push handler, but it doesn’t seem to be working, and I can’t find another way to either make the function think that the A button is pressed or force the scene to close prematurely. I’ve sunk more time than I’d like to admit into this single question and at this point I need more resolution than just “it’s not possible… not for me at least”
tried fake button press
Use the timer extension.
That is a cool idea, although not many people do that because most of the time you can simply press a button for the splash to go away. Perhaps use something along the lines of a timer and on every game update? I’m not entirely sure. Does anyone know how to do this?
yes, it’s possible.
makecode’s runtime has what’s called an “event bus” for handling hardware-generated events like button presses. every event consists of two numbers: an event id and an event value. for button presses, the event id is either the id for the ControllerButtonEvent.Pressed
, ControllerButtonEvent.Released
, or ControllerButtonEvent.Repeat
events. the event value is an id number that corresponds to the actual button being pressed. for example, the “Left” button is 1, the “Up” button is 2, etc. All of the buttons (including the ones for players 2-4) have their own unique id numbers. The id number 0 is a wildcard and corresponds to any button being pressed/released/repeated.
so, to simulate a button press, all you need to do is raise an event on the event bus. to do that, you can use the control.raiseEvent
API like so:
control.raiseEvent(ControllerButtonEvent.Pressed, controller.A.id);
or at least that’s how it would normally work. the splash screen actually listens for a different event so that it doesn’t interfere with the button handlers that are registered by the user code. so instead of using ControllerButtonEvent.Pressed
, we can use the event id INTERNAL_KEY_DOWN
instead:
control.raiseEvent(INTERNAL_KEY_DOWN, controller.A.id);
and here is an example project that demonstrates this:
OHHHHH I guess I just had it in my head that things running in parallel would be tied to the scene, so I didn’t even try just putting a parallel function outside of all the other stuff!
lol you can see I tried it, just not correctly!
Side note, why isn’t that code in the screenshot running? I’ve tried adding stuff to that ScenePuchHandler and nothing is happening!
i expect the bug is something to do with when the handler is being registered or when you’re setting the splashActive
variable. the scene is pushed immediately when game.splash
is called, so you need to call addScenePushHandler
and set splashActive
to true before game.splash
is called.
for example:
also, i wouldn’t use game.addScenePushHandler
for this! that API is more for extensions that need to maintain state for different scenes.
Well don’t use splash text lol. Do a background sprite then fancy text.
Thank you so much for this however after adding it into my project the JavaScript block needs to go on some kind of cooldown, and I can’t use it unless I wait 3ish seconds. Project Link
Like, dragging the block in the simulator? Or using it in your game? If you send a “button pressed” event but forget the “button unpressed” event, the game thinks the button is pressed and won’t send more events unless you add a “button unpressed” event after.
Thanks for this. I figured it out and now it works as it should. If your wondering how I fixed it, I imagine you aren’t, I could put it in another comment. Thanks again for you and richard’s help!