<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=Event-Based_Programming</id>
	<title>Event-Based Programming - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=Event-Based_Programming"/>
	<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Event-Based_Programming&amp;action=history"/>
	<updated>2026-04-13T19:23:48Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=Event-Based_Programming&amp;diff=1640&amp;oldid=prev</id>
		<title>Patarakin: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Event-Based_Programming&amp;diff=1640&amp;oldid=prev"/>
		<updated>2022-07-21T08:33:13Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 11:33, 21 июля 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key digida:diff:1.41:old-1639:rev-1640 --&gt;
&lt;/table&gt;</summary>
		<author><name>Patarakin</name></author>
	</entry>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=Event-Based_Programming&amp;diff=1639&amp;oldid=prev</id>
		<title>scratch&gt;Ipaddude: /* Event Workarounds */</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Event-Based_Programming&amp;diff=1639&amp;oldid=prev"/>
		<updated>2020-11-25T16:36:40Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Event Workarounds&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Event-based programming&amp;#039;&amp;#039;&amp;#039; is programming in which the code is based on events, which are similar to message broadcasts. For example, a &amp;quot;when mouse moved&amp;quot; event can trigger all [[script]]s when the mouse is moved. Events have their own attributes, called &amp;#039;&amp;#039;&amp;#039;event attributes&amp;#039;&amp;#039;&amp;#039;. For example, &amp;#039;&amp;#039;When mouse moved&amp;#039;&amp;#039; can have the attributes &amp;#039;&amp;#039;current mouse x position&amp;#039;&amp;#039;, &amp;#039;&amp;#039;previous mouse x position&amp;#039;&amp;#039;, &amp;#039;&amp;#039;distance moved&amp;#039;&amp;#039;, etc.&lt;br /&gt;
&lt;br /&gt;
== Scratch Events ==&lt;br /&gt;
A type of [[Hat Block]], Event Blocks, create an event listener which will run the attached code upon the event triggering. There are the four built-in events in Scratch:&lt;br /&gt;
&lt;br /&gt;
* [[When Green Flag Clicked (block)|When Green Flag Clicked]]&lt;br /&gt;
* [[When () Key Pressed (Events block)|When () Key Pressed]]&lt;br /&gt;
* [[When () Clicked (block)|When () Clicked]]&lt;br /&gt;
* [[When I Receive () (block)|When I Receive ()]]&lt;br /&gt;
&lt;br /&gt;
== Workarounds for Events ==&lt;br /&gt;
These tutorials will give scripts for the main events:&lt;br /&gt;
&lt;br /&gt;
* Mouse moved&lt;br /&gt;
* Mouse down/up/clicked&lt;br /&gt;
* Any key pressed/up/down&lt;br /&gt;
* Format of an event&lt;br /&gt;
&lt;br /&gt;
=== Mouse Moved ===&lt;br /&gt;
A mouse moved event is not pre-programmed in Scratch but can be easily replicated. In other programming languages, this event triggers specific lines of code to execute when the mouse moves. The mouse does not necessarily need to move over a particular object. Movement over a particular object is often referred to as &amp;quot;hover&amp;quot; and is commonly used in CSS.&lt;br /&gt;
&lt;br /&gt;
In Scratch, a simple script that checks if the mouse has been moved works as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
forever&lt;br /&gt;
set [lastMouseX v] to (mouse x)&lt;br /&gt;
set [lastMouseY v] to (mouse y)&lt;br /&gt;
wait (0) secs //frame pause so the condition is not checked in the same frame&lt;br /&gt;
if &amp;lt;not &amp;lt;&amp;lt;(mouse x) = (lastMouseX)&amp;gt; and &amp;lt;(mouse y) = (lastMouseY)&amp;gt;&amp;gt;&amp;gt; then&lt;br /&gt;
...&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mouse Up/Down/Clicked ===&lt;br /&gt;
This event required no lists. It is a bundle of three events:&lt;br /&gt;
&lt;br /&gt;
* Mouse down&lt;br /&gt;
* Mouse up&lt;br /&gt;
* Mouse clicked (down for less than 0.4 seconds)&lt;br /&gt;
&lt;br /&gt;
Here is the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
forever&lt;br /&gt;
wait until &amp;lt;mouse down?&amp;gt;&lt;br /&gt;
mouse down :: custom stack&lt;br /&gt;
wait until &amp;lt;not &amp;lt;mouse down?&amp;gt;&amp;gt;&lt;br /&gt;
mouse up :: custom&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
forever&lt;br /&gt;
wait until &amp;lt;mouse down?&amp;gt;&lt;br /&gt;
wait (0.4) secs&lt;br /&gt;
if &amp;lt;not &amp;lt;mouse down?&amp;gt;&amp;gt; then&lt;br /&gt;
mouse clicked :: custom&lt;br /&gt;
else&lt;br /&gt;
wait until &amp;lt;not &amp;lt;mouse down?&amp;gt;&amp;gt;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Key Up/Down/Pressed ===&lt;br /&gt;
This event is the same as the [[#Mouse Up/Down/Clicked|mouse up/down/clicked]] event, but for the keyboard.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
forever&lt;br /&gt;
wait until &amp;lt;key [key v] pressed?&amp;gt;&lt;br /&gt;
key down :: custom&lt;br /&gt;
wait until &amp;lt;not &amp;lt;key [key v] pressed?&amp;gt;&amp;gt;&lt;br /&gt;
key up :: custom&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
forever&lt;br /&gt;
wait until &amp;lt;key [key v] pressed?&amp;gt;&lt;br /&gt;
wait (0.4) secs&lt;br /&gt;
if &amp;lt;not &amp;lt;key [key v] pressed?&amp;gt;&amp;gt; then&lt;br /&gt;
key pressed :: custom&lt;br /&gt;
else&lt;br /&gt;
wait until &amp;lt;not &amp;lt;key [key v] pressed?&amp;gt;&amp;gt;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable-Switch Alternative ==&lt;br /&gt;
Not all programming languages are optimized for a plentiful amount of built-in events, and some cannot even multi-thread, so variables must be used in replacement of event. A variable simply stores a value, and interchanging the value of the variable can be used to trigger various looping conditions and run a sequence of code. For example, in Scratch:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;when this sprite clicked&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
Can be replicated with the following code:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
when gf clicked&lt;br /&gt;
forever&lt;br /&gt;
if &amp;lt;&amp;lt;touching [mouse-pointer v]&amp;gt; and &amp;lt;mouse down&amp;gt;&amp;gt; then&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
Both have almost exactly the same functionality, but there is one notable technical difference. Using the click event hat block (first example), suppose the sprite is clicked a &amp;#039;&amp;#039;&amp;#039;second&amp;#039;&amp;#039;&amp;#039; time while the script is still running. Rather than the same script running two instances in parallel, the script will be interrupted and start from the beginning again. This can result in undesirable results.&lt;br /&gt;
&lt;br /&gt;
The second example will not be interrupted, meaning that if the sprite is clicked while the script is being executed, the script will have to finish. Once the script finishes, it will wait until the sprite is clicked again, &amp;#039;&amp;#039;&amp;#039;even&amp;#039;&amp;#039;&amp;#039; if the sprite was clicked &amp;#039;&amp;#039;&amp;#039;while&amp;#039;&amp;#039;&amp;#039; the script was running. In conclusion, the event-based method is better for an interruption-allowing script, whereas the variable-based method is better for a script that needs to entirely execute on the variable-based virtual event.&lt;br /&gt;
&lt;br /&gt;
Having the same script run in multiple instances on Scratch would require an even more hefty process, as neither variable or event-based switch logic could accomplish this with simplicity in Scratch. It would require a lot of complexity that allows scripts to optimize themselves to recognize multiple virtual threads.&lt;br /&gt;
[[Category:Scripting Tutorials]]&lt;/div&gt;</summary>
		<author><name>scratch&gt;Ipaddude</name></author>
	</entry>
</feed>