Discuss Scratch

thaisreyes
New Scratcher
1 post

Help with pressing down key during a set time triggers an event

This is my first time using Scratch and it's for a college assignment. It's very fun, the theme was free and I'm having a small problem as the title states:
Here's the link to my project; https://scratch.mit.edu/projects/75541302/
Sorry the dialogue is in spanish because I'm from Chile
So I'm having problems on the hati1 sprite, I want the player to press the down arrow key until about 3 seconds and then make an event happen! (The moon explodes and Hati eats it.) I tried some tutorials from the wiki but I didn't really undesrtand them specially the one with the timer, please help with a simple solution? This is due friday btw…… Thanks in advance!!
deck26
Scratcher
1000+ posts

Help with pressing down key during a set time triggers an event

Play sound until done will mean the sound is played in full and any check of the timer will only happen after the sound has played. You also have a think for 1 seconds in that script so by the time you get back to the ‘repeat until’ the timer has already gone past 1 second and the repeat just loops continuously.

You need to separate things.

whenbackdropswitchestomoonbroadcastscript2resettimerwaituntiltimer>3stopallsoundsstopother scripts in sprite to stop the 'think' - I haven't checked if that worksbroadcastlunawhenIreceivescript2foreverifkeydown arrowpressed?andtouchingcolor?thenplaysounduntildonethinkHmm......for1secs

Last edited by deck26 (Sept. 7, 2015 08:58:16)

Angusface
Scratcher
12 posts

Help with pressing down key during a set time triggers an event

whenclickedifkeydown arrowpressed?thensetdowntimerto (timer)waituntilnotkeydown arrowpressed?if((timer) - ((downtimer)))>2.59 thenbroadcasthati eat moon
deck26
Scratcher
1000+ posts

Help with pressing down key during a set time triggers an event

The original post is ambiguous, I realise. There are several ways of interpreting what you want.

1 - Once the down arrow is pressed a timer is started which triggers an even after 3 seconds. The sound/think happen if the key is held down but it will only check the key every second or so because the sound and think take time. That's roughly my suggested solution.

2 - The down arrow has to be held down for at least 3 seconds and then released for the event to trigger - that's roughly what @Angusface2 has provided code for.

3 - The down arrow key has to be held down for at least 3 seconds and the event is triggered when the timer reaches 3 seconds, stopping the sound and think. If the down arrow key is released too soon the event is not triggered but the script triggers the event after 3 seconds even if the key is still held down.

So if you can try to give us more info on what you want it would help.





Powered by DjangoBB