<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tupil Code Blog &#187; gui</title>
	<atom:link href="http://blog.tupil.com/tag/gui/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tupil.com</link>
	<description>(Get up early, code often)</description>
	<lastBuildDate>Fri, 27 Aug 2010 10:50:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Look ma, no callbacks!</title>
		<link>http://blog.tupil.com/look-ma-no-callbacks/</link>
		<comments>http://blog.tupil.com/look-ma-no-callbacks/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 20:46:02 +0000</pubDate>
		<dc:creator>Chris Eidhof</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[arrows]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://blog.tupil.com/?p=27</guid>
		<description><![CDATA[In this article, we will see how we can use arrows in Javascript. Arrows are a concept from functional programming, and we&#8217;ll see how they can make our life in Javascript a lot easier. Our code uses the excellent Arrowlets library. It&#8217;s still alpha code, but it&#8217;s already quite useful. We&#8217;re going to build a [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, we will see how we can use arrows in Javascript. Arrows are a concept from functional programming, and we&#8217;ll see how they can make our life in Javascript a lot easier. Our code uses the excellent <a href="http://www.cs.umd.edu/projects/PL/arrowlets/">Arrowlets</a> library. It&#8217;s still alpha code, but it&#8217;s already quite useful.</p>
<p>We&#8217;re going to build a small paddle game, where you can control the paddle with your keyboard. The first thing we&#8217;re going to build is the paddle. This is straightforward, if the user presses a key, we&#8217;ll move sideways, depending on the key that&#8217;s pressed. First, listening to a key press:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  ElementA<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>EventA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keypress'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>movePaddle<span style="color: #009900;">&#41;</span>
                    .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>Repeat<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">repeat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #660066;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You should read the first two lines of the code like this: if the &#8216;keypress&#8217; event happens on the document, we&#8217;re going to move the paddle. This is very different from regular javascript events, because there you attach an event and do the movePaddle every time there is a keypress. However, using arrowlets, the event is only caught once. Therefore we also need to have the next line that says: do this forever. The last line actually starts the code.</p>
<p>The body of the movePaddle function is only two lines:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> movePaddle<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">37</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">4</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">39</span> <span style="color: #339933;">?</span> <span style="color: #CC0000;">4</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  paddle.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span> <span style="color: #339933;">=</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> Math.<span style="color: #660066;">min</span><span style="color: #009900;">&#40;</span>paddleLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> offset<span style="color: #339933;">,</span> <span style="color: #CC0000;">230</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When the user presses a button &#8220;Start&#8221; that will start the game. Before we actually start animating, we&#8217;ll have to reset the field and place the ball. The ElementA function will find an element in the DOM, and the next resetGameOver updates the status.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  ElementA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'start'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>EventA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>ElementA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gameOver'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>resetGameOver<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The function resetGameOver code looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> resetGameOver <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next, we&#8217;ll reset the position of the ball and tell it to start moving every 30ms:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">                   .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>ElementA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ball'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>resetBall<span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>moveBall.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The code for resetBall simply resets the position and direction of the ball:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> resetBall <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ball<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  dir <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#125;</span>
  startPos <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span> <span style="color: #339933;">+</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">40</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
  ball.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span> <span style="color: #339933;">=</span> startPos<span style="color: #339933;">;</span> ball.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;120px&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> ball<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that it returns the ball, so the next function can make use of it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> moveBall <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ball<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> newLeft <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>ball.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> dir.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> newTop  <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>ball.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span><span style="color: #009900;">&#41;</span>  <span style="color: #339933;">+</span> dir.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newLeft <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> newLeft <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">260</span><span style="color: #009900;">&#41;</span> dir.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">-</span> dir.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newTop  <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> dir.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">-</span> dir.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
  ball.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span> <span style="color: #339933;">=</span> newLeft <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
  ball.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> newTop <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newTop <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">240</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newLeft <span style="color: #339933;">&gt;</span> paddleLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">5</span> <span style="color: #339933;">&amp;&amp;</span> newLeft <span style="color: #339933;">&lt;</span> paddleLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">45</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> 
      dir.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">-</span> dir.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> Repeat<span style="color: #009900;">&#40;</span>ball<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">return</span> Done  <span style="color: #009900;">&#40;</span>ball<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">return</span> Repeat<span style="color: #009900;">&#40;</span>ball<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first two lines calculate a new left and top. If the ball bounces against one of the side walls, we invert the x-direction. Similarly for the top. If, however, we reach the bottom of the screen, things a bit more complicated: if the ball hits the paddle, we invert the y-direction and return Repeat(ball). The Repeat means that we&#8217;re not finished with our animation. If the ball misses the paddle, we return Done(ball). This means our animation is finished an the user&#8217;s game is over.</p>
<p>Now we can go on with the next part of our game handling code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">                   .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>ElementA<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gameOver'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>gameOver<span style="color: #009900;">&#41;</span>
                   .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span>Repeat<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">repeat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>We show the gameOver status message, and then repeat this infinitely, so the user can start a new game. And that&#8217;s it, that&#8217;s all the code. Here&#8217;s a link to <a href="http://code.tupil.com/arrowlets">the game</a>, and also a link to <a href="http://code.tupil.com/arrowlets/paddle.js">the source code.</a>.</p>
<p>Disclaimer: The code only works in Firefox and Opera. In Safari there&#8217;s no keypress event for the arrow keys, and the arrowlets library currently doesn&#8217;t support IE.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tupil.com/look-ma-no-callbacks/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
