<?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>Cameron McKay &#187; Wezzle Development</title>
	<atom:link href="http://cdmckay.org/blog/category/wezzle-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A little bit about Webble and JavaScript games</title>
		<link>http://cdmckay.org/blog/2010/05/19/a-little-bit-about-webble-and-javascript-games/</link>
		<comments>http://cdmckay.org/blog/2010/05/19/a-little-bit-about-webble-and-javascript-games/#comments</comments>
		<pubDate>Wed, 19 May 2010 21:37:48 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Wezzle]]></category>
		<category><![CDATA[Wezzle Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1535</guid>
		<description><![CDATA[Webble is a pure HTML/CSS/JavaScript implementation of some of the core features Wezzle game engine. By &#8220;pure&#8221; I mean that there are no external plugins like Flash used. Furthermore, Webble does not make use of the HTML5 canvas element: all the animation is done purely by moving around HTML elements using JavaScript. Writing a simple [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://couchware.ca/webble">Webble</a> is a pure HTML/CSS/JavaScript implementation of some of the core features <a href="http://couchware.ca/www/wezzle">Wezzle game engine</a>.  By &#8220;pure&#8221; I mean that there are no external plugins like Flash used.  Furthermore, Webble does not make use of the HTML5 canvas element: all the animation is done purely by moving around HTML elements using JavaScript.</p>
<p>Writing a simple game engine using the DOM and JavaScript poses some interesting problems that are atypical to regular website development.  In this article I will discuss a few of the problems that I encountered while writing Webble and how I solved them.</p>
<p><span id="more-1535"></span></p>
<h3>Tracking Mouse Movement</h3>
<p>In Wezzle, there is a selector that follows around the mouse cursor.  This was fairly easy to implement in Java, as having a mouse movement listener isn&#8217;t too costly.  In JavaScript, however, listening to the <code>mousemove</code> event is very expensive.  In fact, listening to <code>mousemove</code> slows down the browser window so much that JavaScript animations become unusable.  This is fine if your game does not animate while you listen to mouse movements.  Unfortunately, with Webble, this was not the case.  I needed to be able to follow the cursor at all times.</p>
<p>What was the solution? Webble has a 8&#215;10 grid called the &#8220;board&#8221; (see image below).  Each board cell can contain a tile. Overlayed on that board is a selector centered on whatever board cell the mouse cursor is on. Thus, Webble only needs to know when the mouse moves over a new board cell. This means that instead of using the <code>mousemove</code> event, I could instead use an invisible 8&#215;10 grid of divs and listen for their less expensive <code>mouseover</code> event.  This solution ended up working extremely well, allowing me to easily keep track of the mouse while simultaneously running animations.</p>
<p><img class="aligncenter size-full wp-image-1557" title="The We*le Board" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/board.png" alt="" width="256" height="320" /></p>
<h3>Animation</h3>
<p>Animation in Webble was quite painless, thanks to jQuery. For most animations, it was as easy as setting the correct parameters for the <a href="http://api.jquery.com/animate/">animate function</a>. In particular, there were some situations were I needed animations to run in lockstep.  In Webble, when you get a line, all the tiles in that line must animated away together.  Thus, doing something like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Gets all the line tiles on the board.</span>
<span style="color: #003366; font-weight: bold;">var</span> tiles <span style="color: #339933;">=</span> board.<span style="color: #660066;">findLineTiles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Get all the underlying DOM elements.</span>
<span style="color: #003366; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>tiles.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>tile<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000066; font-weight: bold;">return</span> tile.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Fade out the elements.</span>
tiles.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Remove tiles from internal board structure.</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;would not work very well and would be inefficient. Why? Because if I had, say, 10 tiles to animate, jQuery would create 10 tile animations.  This wasteful.  Moreover, since they&#8217;re independent animations, there&#8217;s no guarantee they will be moving the same amount each animation step. This visual disparity is exacerbated when the JS engine lags.</p>
<p>Fortunately, the jQuery animate function provides <a href="http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/">a convenient step callback function</a> that allows you to animate many elements in lockstep. As a result, the code above can become a single animation that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// The elements to animate in lockstop.</span>
<span style="color: #003366; font-weight: bold;">var</span> elements <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> ... <span style="color: #660066;">same</span> <span style="color: #000066; font-weight: bold;">as</span> above ... <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> elementsTail <span style="color: #339933;">=</span> elements.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
elements
    .<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span>
        step<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>now<span style="color: #339933;">,</span> fx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            elementsTail.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span>fx.<span style="color: #660066;">prop</span><span style="color: #339933;">,</span> fx.<span style="color: #660066;">now</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// Remove tiles from internal board structure.</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Audio</h3>
<p>The Wezzle game has several sound effects that need to play at specifc times (i.e. when a tile is inserted, when a line is removed, etc.).  Webble features some experimental sound support via the HTML5 audio element.  Unfortunately, like HTML5 video, not everyone can agree on a format.  Firefox supports Vorbis.  Safari (and Safari Mobile) supports MP3.  Chrome supports both.  IE supports none (until IE9, in the distant, distant future).  Thus, in order to support sounds across all modern browsers I needed to make both Vorbis and MP3 versions of all the sound effects.  This is a minor inconvenience that could have been avoided if Apple just played nice and included Vorbis support, at the very least on desktop Safari.</p>
<p>Other than that issue, HTML5 audio is pretty nice to work with.  All it takes to play a sound is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> lineSound <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sounds/line.ogg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
lineSound.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a cinche compared to the hoops that need to be jumped through to get decent sound in Java.  Although HTML5 audio still has a little bit further to go, I think it has a happy future in HTML5.</p>
<h3>Conclusion</h3>
<p>Writing games purely in HTML, CSS and JavaScript is easier than it ever has been thanks to wonderful libraries like jQuery and next generation technologies like HTML5.  Although clever hacks are still required to make everything work together across browsers, I firmly believe that HTML/CSS/JavaScript combo will be a viable browser game platform in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/05/19/a-little-bit-about-webble-and-javascript-games/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Wezzle Beta 1 Screenshots</title>
		<link>http://cdmckay.org/blog/2009/08/10/wezzle-beta-1-screenshots/</link>
		<comments>http://cdmckay.org/blog/2009/08/10/wezzle-beta-1-screenshots/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 04:53:27 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Wezzle]]></category>
		<category><![CDATA[Wezzle Development]]></category>
		<category><![CDATA[bomb]]></category>
		<category><![CDATA[screenshots]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=876</guid>
		<description><![CDATA[Here are some Wezzle screenshots from the latest beta.]]></description>
			<content:encoded><![CDATA[<p>Here are some Wezzle screenshots from the latest beta.</p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2009/12/bomb.png" rel="lightbox[876]"><img class="alignnone size-thumbnail wp-image-1096" title="A bomb exploding in Wezzle" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2009/12/bomb-150x150.png" alt="" width="150" height="150" /></a> <a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2009/12/menu.png" rel="lightbox[876]"><img class="alignnone size-thumbnail wp-image-1097" title="The Wezzle Play Now menu" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2009/12/menu-150x150.png" alt="" width="150" height="150" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/08/10/wezzle-beta-1-screenshots/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

