m (Krett12 moved page How to Cre to How to Create A Not Tap Holding Sensor: oops what was its name again)
m (added a comment)
 
(24 intermediate revisions by 16 users not shown)
Line 1: Line 1:
[[Category:Scripting Tutorials]]
+
{{Wiki Standards|Not enough information|date=February 2016}}
Without much work, when you hold a button, it can do things the button was set to do for a repeated amount of times. However, if you want to make the button be not holded, but only pressed once at a time, you can use the scripting in this tutorial.
+
[[Category:Scripting Tutorials|Key Tapping Sensor]]
 +
Without much work, when you hold a button, it can do things the button was set to do for a repeated amount of times. However, if you want to make the button be not held, but only pressed once at a time, you can use the scripting in this tutorial.
 +
 
 
== What To Do ==
 
== What To Do ==
 
===Holding Script===
 
===Holding Script===
This is the holding-allowed [[script]], which you want to avoid:
+
This is the [[script]] which will allow holding, which you want to avoid:
 
<scratchblocks>
 
<scratchblocks>
 
when flag clicked
 
when flag clicked
 
forever
 
forever
if <key [...] pressed?>
+
if <key [... v] pressed?> then
 
next backdrop
 
next backdrop
 
end
 
end
 
end
 
end
 
</scratchblocks>
 
</scratchblocks>
 +
If you use this one, you might go forward more than one backdrop before you release the key.
 +
You can also use this one:
 +
<scratchblocks>
 +
when [... v] key pressed
 +
next backdrop
 +
</scratchblocks>
 +
That will make it a little better because there will be a tiny pause after a [[Hat Block|hat block]], although holding ''will'' still work. This is why you do not want to use it in a [[platformer]].
  
 
===Non-Holding Script===
 
===Non-Holding Script===
This is the non-holding-allowed script, which you want to use.
+
This is the script which does ''not'' allow holding, which you will want to use.
 
<scratchblocks>
 
<scratchblocks>
 
when flag clicked
 
when flag clicked
 
forever
 
forever
if <key [...] pressed?>
+
wait until <key [... v] pressed>
 
next backdrop
 
next backdrop
wait until <not <key [...] pressed?>>
+
wait until <not <key [... v] pressed?>>//this block will not let the script going until said key is stopped being pressed
end
 
 
end
 
end
 +
</scratchblocks>
 +
This will make it sense the key, perform an action, and wait for the key to be released before checking again. <!--This will make it begin to sense the next key action after the key is released, which means you press the key and the backdrop will switch, then you need to release it and press it again to switch to next backdrop.-->
 +
You can also change the order to:
 +
<scratchblocks>
 +
when flag clicked
 +
forever
 +
wait until <key [... v] pressed?>
 +
wait until <not <key [... v] pressed?>>
 +
next backdrop
 +
 +
 +
</scratchblocks>
 +
If you use this code, you have to press the key, and the backdrop will not change until you release it.
 +
 +
== Advanced Cases ==
 +
Let's say you are making a platformer, this can be useful when you run into issues
 +
 +
<scratchblocks>
 +
when [... v] key pressed
 +
forever
 +
wait until <not <key [... v] pressed?>>
 +
broadcast [... v]
 +
stop [this script v]
 +
</scratchblocks>
 +
 +
and then you want to make this:
 +
 +
<scratchblocks>
 +
when I receive [... v]
 +
next backdrop
 
</scratchblocks>
 
</scratchblocks>
  
 
== Uses ==
 
== Uses ==
This could be useful for many things, such as announcement projects, or projects with only words. You do not want people skipping through words quickly on those projects, so this script could be useful.
+
A key tapping sensor could be useful for many things, such as announcement projects, or projects with only words. It's often important in these projects, that users don't skip through the information quickly, so this script could be useful.
 
+
If one wants to create a [[platformer]], the first code could be used to to create nice, smooth controls.
== What to not use it for ==
+
[[Category:Scripting Tutorials|Create a Key Tapping Sensor]][[Category:Game Design Tutorials]]
Do not use this in [[platformer]]s with good physics and gravity, because it could make the jump only be a very small one. Instead, use it in the project examples in the part of this article [[How to Create a Non-Holding Pressing Sensor#Uses|Uses]].
 

Latest revision as of 10:10, 12 April 2019

Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (February 2016)
Reason: Not enough information

Without much work, when you hold a button, it can do things the button was set to do for a repeated amount of times. However, if you want to make the button be not held, but only pressed once at a time, you can use the scripting in this tutorial.

What To Do

Holding Script

This is the script which will allow holding, which you want to avoid:

when flag clicked
forever
if <key [... v] pressed?> then
next backdrop
end
end

If you use this one, you might go forward more than one backdrop before you release the key. You can also use this one:

when [... v] key pressed
next backdrop

That will make it a little better because there will be a tiny pause after a hat block, although holding will still work. This is why you do not want to use it in a platformer.

Non-Holding Script

This is the script which does not allow holding, which you will want to use.

when flag clicked
forever
wait until <key [... v] pressed>
next backdrop
wait until <not <key [... v] pressed?>>//this block will not let the script going until said key is stopped being pressed
end

This will make it sense the key, perform an action, and wait for the key to be released before checking again. You can also change the order to:

when flag clicked
forever
wait until <key [... v] pressed?>
wait until <not <key [... v] pressed?>>
next backdrop


If you use this code, you have to press the key, and the backdrop will not change until you release it.

Advanced Cases

Let's say you are making a platformer, this can be useful when you run into issues

when [... v] key pressed
forever
wait until <not <key [... v] pressed?>>
broadcast [... v]
stop [this script v]

and then you want to make this:

when I receive [... v]
next backdrop

Uses

A key tapping sensor could be useful for many things, such as announcement projects, or projects with only words. It's often important in these projects, that users don't skip through the information quickly, so this script could be useful. If one wants to create a platformer, the first code could be used to to create nice, smooth controls.