    <?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>FlexWiz BlogFlexWiz Blog</title>
	<atom:link href="http://blog.flexwiz.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexwiz.net</link>
	<description>On Game Development for Mobile and Flash</description>
	<lastBuildDate>Sun, 22 Apr 2012 15:36:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Finding Inspiration in Mechner&#8217;s Journals</title>
		<link>http://blog.flexwiz.net/finding-inspiration-in-mechners-journals/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=finding-inspiration-in-mechners-journals</link>
		<comments>http://blog.flexwiz.net/finding-inspiration-in-mechners-journals/#comments</comments>
		<pubDate>Sat, 21 Apr 2012 23:39:45 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Apple]]></category>

		<guid isPermaLink="false">http://blog.flexwiz.net/?p=992</guid>
		<description><![CDATA[I Started reading Jordan Mechner&#8217;s development journals on the making of Prince of Persia, just after he released the source code of the original game on github. Jordan Mechner is a game industry pioneer &#8211; he created and coded Karateka in the early 80s for the Apple II as a one man team and made it [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">I Started reading Jordan Mechner&#8217;s development journals on the <a href="http://jordanmechner.com/ebook/" target="_blank">making of Prince of Persia</a>, just after he <a href="http://jordanmechner.com/blog/2012/04/source/" target="_blank">released the source code</a> of the original game on github.</p>
<p align="center"><img class="wp-image-997 alignnone" style="margin: 8px; align: center;" src="http://blog.flexwiz.net/wp-content/uploads/2012/04/pop1-sword-copy.png" alt="" width="336" height="230" /></p>
<p>Jordan Mechner is a game industry pioneer &#8211; he created and coded <a href="http://en.wikipedia.org/wiki/Karateka_(video_game)" target="_blank"><em>Karateka</em> </a>in the early 80s for the Apple II as a one man team and made it to #1 selling title, using revolutionary techniques to achieve realistic character animation and cinematic story telling. He later on continued to create Prince of Persia using the same techniques and found even greater success.</p>
<p>As a gamer and developer who grew up in the 80s, I got to play the original <em>Karateka </em>on the Apple II and the even better porting if it on the Commodore 64. It was definitely one of the best 8-bit arcade games ever made. For anyone too young to remember, those were the days with no internet, only floppy disks (or cassette tapes) as the common storage device, and crude development tools.</p>
<p align="center"><img class="alignnone size-full wp-image-1008" title="apple_2e" src="http://blog.flexwiz.net/wp-content/uploads/2012/04/apple_2e.png" alt="" width="350" height="300" /></p>
<p>In those days, programming was a craft. For nearly a decade, a program had to fit into less that 64K of RAM and use so called &#8216;Hi Res&#8217; graphics with only 16 colors (or even less if you used an Apple II) for any compelling visual. Most coding was done with 6502 assembly language (that&#8217;s machine code) &#8211;  can&#8217;t get any hard core than that.  What more can I say, I just love the 80s <img src='http://blog.flexwiz.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Reading Mechner&#8217;s journals, you get a glimpse at the creative process including small daily details and challenges that game developers were facing in those days. Not surprisingly, most are still true today.</p>
<p>I specially liked this entry that describes how he creates the animation frames for the character:</p>
<blockquote>
<h4>April 29, 1986</h4>
<p>[...] I still think this can work. The key is not to clean up the frames too much. The figure will be tiny and messy and look like crap… but I have faith that, when the frames are run in sequence at 15 fps, it’ll create an illusion of life that’s more amazing than anything that’s ever been seen on an Apple II screen. The little guy will be wiggling and jittering like a Ralph Bakshi rotoscope job… but he’ll be alive. He’ll be this little shimmering beacon of life in the static Apple-graphics Persian world I’ll build for him to run around in.</p></blockquote>
<p>This is a fascinating read &#8211; check it out the page for The <a href="http://jordanmechner.com/ebook/" target="_blank">Making of Prince of Persia</a> &#8211; with its funny video trailer showing the actual video footage with the gameplay scenes.</p>
<p>This book is a great source of inspiration for any creative person. You feel the excitement all the way through. Imo, this is a must read for any aspiring game developer, and I highly recommend it.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/finding-inspiration-in-mechners-journals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tip: Backwards Compatibility and Mac OS X</title>
		<link>http://blog.flexwiz.net/tip-backwards-compatibility-and-mac-os-x/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tip-backwards-compatibility-and-mac-os-x</link>
		<comments>http://blog.flexwiz.net/tip-backwards-compatibility-and-mac-os-x/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 23:06:40 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://blog.flexwiz.net/?p=967</guid>
		<description><![CDATA[Targeting a game to a specific flash player version can be useful for many reasons. Since Player 11 was released there where many features added, but there are a few issues to consider. For a game that runs on a [desktop] browser, player update may be required. This should not be a problem, except for some [...]]]></description>
			<content:encoded><![CDATA[<p>Targeting a game to a specific flash player version can be useful for many reasons. Since Player 11 was released there where many features added, but there are a few issues to consider.</p>
<p>For a game that runs on a [desktop] browser, player update may be required. This should not be a problem, except for some older versions of Mac OS X that can only run Flash 10.x, but not Flash 11. This is the case for OS X 10.4 (Tiger), and 10.5 (Leopard) &#8211; so if you want to target these your only option is to have your game compiled to Flash player 10 specs.</p>
<p align="center"><img class="wp-image-978" style="margin-top: 8px; margin-bottom: 8px;" title="install_flash_player_620px" src="http://blog.flexwiz.net/wp-content/uploads/2012/04/install_flash_player_620px1.jpg" alt="" width="434" height="210" /></p>
<p>The best bet for this case is to target player version 10.3. First of all make sure you are not using any of the Flash 11 specific features (Stage3D and native JSON). Next you need to set the compiler option so the output SWF file would run properly on the older player, since the Flex 4.6 SDK defaults to Flash 11.</p>
<p>To do that, you need to invoke the swf-version compiler option. On any of the decent IDEs there is access to compiler command line options. Flash Builder, FDT and Flash Develop have it inside project settings, and if you are using ant script to build the code, the command line options are available in the mxmlc tag.</p>
<p>Simply add the following to the additional compiler arguments:<br />
<code><br />
-swf-version=12</code></p>
<p>The swf-version compiler option specifies the SWF file format version of the output SWF file. Features requiring a later version of the SWF file format are not compiled into the application. This is different from the Player version in that it refers to the SWF specification versioning scheme. In this case 12 is the scheme for player 10.3.</p>
<p>As an added bonus, you&#8217;ll be able to debug the game code as if it is running in the target version. Any code that requires Flash 11 will cause a run time error with stack trace &#8211; that comes in handy in case there you left &#8216;invalid&#8217; features hiding in the code.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/tip-backwards-compatibility-and-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SpriteSheet Animation in AS3</title>
		<link>http://blog.flexwiz.net/spritesheet-animation-in-as3/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spritesheet-animation-in-as3</link>
		<comments>http://blog.flexwiz.net/spritesheet-animation-in-as3/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 20:33:05 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[Blitting]]></category>
		<category><![CDATA[SpriteSheet]]></category>
		<category><![CDATA[TexturePacker]]></category>

		<guid isPermaLink="false">http://blog.flexwiz.net/?p=919</guid>
		<description><![CDATA[Over the last few years sprite-sheet animation gained popularity among game developers. It offers some real advantages over movie clips &#8211; lighter and potentially faster performance, with the added bonus of code portability. If you plan to deploy your game on Android and iOS  as well, this is definitely the way to go. I found that most [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-medium wp-image-934" title="sheetDemo3ab" src="http://blog.flexwiz.net/wp-content/uploads/2011/11/sheetDemo3ab-300x258.png" alt="" width="300" height="258" />Over the last few years sprite-sheet animation gained popularity among game developers. It offers some real advantages over movie clips &#8211; lighter and potentially faster performance, with the added bonus of code portability. If you plan to deploy your game on Android and iOS  as well, this is definitely the way to go.</p>
<p>I found that most of the tutorials on sprite sheets are for the Cocos2D engine (in Objective-C for iOS). Here I will show a practical and convenient way to animate sprite sheets in AS3, making the game perform well on both Flash in browser and AIR mobile.</p>
<p><span id="more-919"></span></p>
<p>There are a few off the shelf engines that use variations of spritesheets, among them Starling (Flash 11 only) and Flixel. These may be perfect for certain type of projects, but I decided to write my own classes to minimize the dependency on 3rd party code.</p>
<h3>Demo</h3>
<div class="wp-caption aligncenter" style="width: 540px"><a href="http://blog.flexwiz.net/sources/animSprite/AnimDemo.swf"><img title="demo" src="http://blog.flexwiz.net/wp-content/uploads/2011/12/demo1a.png" alt="" width="530" height="394" /></a><p class="wp-caption-text">Click image to see the demo</p></div>
<p>Download the full <a href="https://bitbucket.org/flexwiz/spritesheet" target="_blank">demo code</a> from BitBucket <a href="https://bitbucket.org/flexwiz/spritesheet" target="_blank">here</a>.</p>
<h3>The Tools</h3>
<p>You would need a code IDE (FlashDevelop, FDT or Flash Builder) and an image tool that can pack textures. I use the popular <a href="http://www.texturepacker.com/" target="_blank">TexturePacker</a>, in my opinion the best texture tool for developers. If anyone knows anything better, please comment.</p>
<h3>Preparing the assets</h3>
<p>The animated sprite is created by the artist  with their animation package of choice. This is a sequence of images that form the animation frames.  In this case (and most others) all frames are the same size.</p>
<p>Open TexturePacker and import the individual images into the projects. Select json as the data format, check &#8216;Allow free sizes&#8217; and make sure &#8216;Allow rotation&#8217; is not checked. Publish the texture to the output folder. <a href="http://blog.flexwiz.net/wp-content/uploads/2011/12/settings.png" target="_blank">The setting I use</a> can be seen <a href="http://blog.flexwiz.net/wp-content/uploads/2011/12/settings.png" target="_blank">here</a>.</p>
<p>There are many different options with TexturePacker, that are out of scope of this article. For more in depth look, check out Chris Fletcher&#8217;s tutorial <a href="http://chris-fletcher.com/tag/texturepacker-tutorial/" target="_blank">here</a>.</p>
<p>Once published, you end up with a large PNG file &#8211; the texture sheet, and a json file to go with it.</p>
<h3>Reading the texure with the AnimTextureSheet class</h3>
<p>I created the AnimTextureSheet class to encapsulate a texture sheet and its data. After loading your animation assets, you get a BitmapData from the sheet PNG and a string from the json file. After decoding the json string, the function populateFrameArray serialize the frame data into an array which is then passed into the  newly created AnimTextureSheet.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">	<span style="color: #6699cc; font-weight: bold;">var</span> seqRaw<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = JSON<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">decode</span><span style="color: #000000;">&#40;</span> sheetData <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> animData<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span>= <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	populateFrameArray<span style="color: #000000;">&#40;</span>animData<span style="color: #000066; font-weight: bold;">,</span> seqRaw<span style="color: #000066; font-weight: bold;">.</span>frames<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Sort</span>
	animData<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sortOn</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;id&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Create and initialize the tile sheet</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> tileSheet<span style="color: #000066; font-weight: bold;">:</span>AnimTextureSheet= <span style="color: #0033ff; font-weight: bold;">new</span> AnimTextureSheet<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	tileSheet<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span>texture<span style="color: #000066; font-weight: bold;">,</span> animData<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Once initialized, the tille sheet is ready to go, and the  drawFrame() method can be called to blit a frame into a destination bitmap buffer.</p>
<p><strong>UPDATE</strong><br />
When compiling with Flash Player 11 (or Flex 4.6) there is a build error due to the new native JSON parser:<br />
<code>Can not resolve a multiname reference unambiguously. JSON [..]</code><br />
To resolve the problem, simply change the first line to read:<br />
<code>var seqRaw:Object =  com.adobe.serialization.json.JSON.decode( sheetData );</code></p>
<h3>Play it</h3>
<p>To actually display the animation,  we need a display object to hold the bitmap. For that purpose I created the AnimSprite class, that is based on flash.display.Bitmap. This class in only for convenience - you can create your own &#8211; it demonstrates the possible use cases for an animated sprite.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">	<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> createAnim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>AnimSprite
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> anim<span style="color: #000066; font-weight: bold;">:</span>AnimSprite = <span style="color: #0033ff; font-weight: bold;">new</span> AnimSprite<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		anim<span style="color: #000066; font-weight: bold;">.</span>initialize<span style="color: #000000;">&#40;</span>_texSheet<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		anim<span style="color: #000066; font-weight: bold;">.</span>addSequence<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;all&quot;</span><span style="color: #000066; font-weight: bold;">,</span> arFrames<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		anim<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">play</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;all&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">return</span> anim<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Call on enter frame event</span>
	<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> onFrameUpdate<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">pause</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> arSprites<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> 	<span style="color: #000000;">&#123;</span>
			AnimSprite<span style="color: #000000;">&#40;</span>arSprites<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>updateAnimation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The above code shows how AnimSprite is actually used.</p>
<ol>
<li>instantiate the AnimSprite</li>
<li>initialize it with  a tile sheet</li>
<li>add one or more sequences (indexed frames) with an individual frame rate</li>
<li>add it to the stage.</li>
</ol>
<p>To play it:</p>
<ol>
<li>Call play() to start playing a sequence</li>
<li>call updateAnimation() on every frame</li>
</ol>
<h3>How it works</h3>
<p>AnimSprite uses partial blitting technique to animate. It uses the bitmapData as a frame buffer that holds the frame image that gets displayed. On every update, the updateAnimation()  function checks if the animation frame need to advance, and if so calls advanceFrame() &#8211; that sets the dirty flag when the current frame index is changed.  The the following method is then used to draw the frame:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">	<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> drawFrameInternal<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#123;</span>
		dirty = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #004993;">bitmapData</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">fillRect</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">bitmapData</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rect</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		mAnimSheet<span style="color: #000066; font-weight: bold;">.</span>drawFrame<span style="color: #000000;">&#40;</span>curIndex<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">bitmapData</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Check out the code for more details on the AnimSprite class.</p>
<p>That&#8217;s pretty much it. Let me know if you find any bugs or problems.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/spritesheet-animation-in-as3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Make Your Own Level Editor &#8211; Part II</title>
		<link>http://blog.flexwiz.net/make-your-own-level-editor-part-ii/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=make-your-own-level-editor-part-ii</link>
		<comments>http://blog.flexwiz.net/make-your-own-level-editor-part-ii/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 09:28:49 +0000</pubDate>
		<dc:creator>theWiz</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[Contents]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.flexwiz.net/?p=900</guid>
		<description><![CDATA[On part 1 of this series, I outlined the basic principles of handling the content pipeline, and how the process should work. To continue, we move on to building the level. As mentioned on part 1, I use the Flash IDE (CS4 or CS5) because of its support for automation and custom commands. Basic familiarity with Flash is [...]]]></description>
			<content:encoded><![CDATA[<p>On <a title="Make Your Own Level Editor – Part I" href="http://blog.flexwiz.net/make-your-own-level-editor-part-i/">part 1 of this series</a>, I outlined the basic principles of handling the content pipeline, and how the process should work. To continue, we move on to building the level. As mentioned on part 1, I use the Flash IDE (CS4 or CS5) because of its support for automation and custom commands. Basic familiarity with Flash is required.<span id="more-900"></span></p>
<p style="padding-left: 30px;"><em>This article is cross posted on<a title="#AltDevBlogADay" href="http://altdevblogaday.com/2011/09/10/make-your-own-level-editor-part-ii/" target="_blank"> #AltDevBlogADay</a>. See my <a href="http://altdevblogaday.com/author/amos-laber/" target="_blank">other articles</a> published on #AltDevBlogADay <a href="http://altdevblogaday.com/author/amos-laber/" target="_blank">here</a>.</em></p>
<h4>Mapping Objects and Assets</h4>
<p><img class="size-full wp-image-16271 alignright" style="margin-left: 8px; margin-right: 8px;" src="http://altdevblogaday.com/wp-content/uploads/2011/09/edit02.png" alt="" width="482" height="375" /></p>
<p>Before actually making the first level, we need to create a manifest of all game objects. These can be anything from background elements, pick-ups or characters. Most are represented by static bitmaps or sprites-sheets where the game object is displayed using a single asset.</p>
<p>The idea is to map a logical game object (class) to a physical asset (bitmap or simple shape). For simplicity, we assume the size of the asset defines the bounding box of the logical game object. Each entry in the manifest will pair the class name with the asset file name, and a class ID. Its recommended to use some kind of naming convention with prefixes etc.</p>
<h4>Building the Basic Level</h4>
<p>Next we create a level template. This is a project with an empty level that would serve as a base for all future levels. Create a new Flash project (fla) and import all your assets into the library and save the file.</p>
<p>Flash keeps library objects either as the source assets or movie-clips, with the latter used as a container for a display object that can be manipulated.<br />
For each of the source assets, create a movieclip for it (using F8), and name it to match the class id in the manifest. Save the file and start dragging objects to the stage. You can define a layer for graphic elements that are not exported and place there references, guides and bitmaps from the mock up. Make sure to have a rectangle that represents the screen bounds.Use the first level as template for the rest, reusing the same library objects.</p>
<h4>Exporting the Level</h4>
<p>The flash project file (fla) is how a levels is saved. This would be an intermediate file that will only be used to modify the level on design time. To actually use it in the game, we need to export the level data to a format that can be loaded at runtime.</p>
<p>Export the level data means we serialize it to XML, which will contain entries for all instances of game objects, each with is metadata (like class name, position, rotation and size). For that, we make use of JSFL &#8211; the JavaScript Flash API that&#8217;s included with the Flash IDE.</p>
<p>The following JSFL script creates an XML file and saves it in the current folder. To run, use Commands -&gt; &#8216;Run Command&#8217; and then point to the script file (should be saved with jsfl extension). After the first run, the script name will appear under the &#8216;Commands&#8217; menu for quick access.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//==========================================</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">// Export all stage instances to xml</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> doc <span style="color: #339933;">=</span> fl.<span style="color: #660066;">getDocumentDOM</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	doc.<span style="color: #660066;">selectAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> sel <span style="color: #339933;">=</span> fl.<span style="color: #660066;">getDocumentDOM</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">selection</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> myXml <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> iLen <span style="color: #339933;">=</span> sel.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> iLen<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> sel<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">instanceType</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;symbol&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
		myXml <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;    &lt;child type='&quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">libraryItem</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">;</span>
		myXml <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; x='&quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' y='&quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">;</span>
		myXml <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot; rotation='&quot;</span> <span style="color: #339933;">+</span> s.<span style="color: #660066;">rotation</span><span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #339933;">;</span>
		myXml <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	doc.<span style="color: #660066;">selectNone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        myXml <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&lt;/level&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	flash.<span style="color: #660066;">outputPanel</span>.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	flash.<span style="color: #660066;">trace</span><span style="color: #009900;">&#40;</span>myXml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> exportFileName <span style="color: #339933;">=</span> doc.<span style="color: #660066;">pathURI</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/%20/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
                                doc.<span style="color: #000066;">name</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\.fla$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	flash.<span style="color: #660066;">outputPanel</span>.<span style="color: #660066;">save</span><span style="color: #009900;">&#40;</span>exportFileName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	flash.<span style="color: #660066;">trace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;File saved to &quot;</span> <span style="color: #339933;">+</span> exportFileName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here is a sample of the resulting file:</p>
<p><img class="size-full wp-image-16255 alignnone" style="margin-top: 10px; margin-bottom: 10px;" src="http://altdevblogaday.com/wp-content/uploads/2011/09/SampleXml.gif" alt="" width="616" height="158" /><br />
A major advantage of this export is the ability to easily customize the XML data by changing the script to add different properties for each entry (lines 15-18).</p>
<p>&nbsp;</p>
<h4>Loading the Level</h4>
<div>Loading the level into the game is a matter of de-serializing the XML data into game objects. This is truly platform agnostic: you could read the XML from any language or platform and parse the results however you like. The most common are for Flash, iOS and XNA.</div>
<div>The process for loading should be something like:</div>
<blockquote>
<ol>
<li>Read list of game objects from XML into an array.</li>
<li>Iterate on the array. For each item:
<ol>
<li>Pass the class name to a factory method to create a new instance of the object.</li>
<li>Locate the asset (resource) associated with the object, by looking it up on the object manifest map.</li>
<li>Call the init function on the newly created instance, to populate it with the asset and metadata.</li>
<li>Place the object on stage</li>
</ol>
</li>
<li>End</li>
</ol>
</blockquote>
<p>Your mileage may vary, but that should do the trick. Here I assume all game entity classes are based on one base class, and make use of polymorphism to construct and populate them.</p>
<p>&nbsp;</p>
<h3>Round Trip and Edit Cycle</h3>
<div>
<p>As mentioned before, the level is also saved as a native &#8216;fla&#8217; file, as intermediate, so it can be loaded and exported at any later time. The most effective way to edit a level is by instant round tripping between the game (preview) and the editor. Round tripping is the process of moving from runtime back to design in order to edit or modify the level data.</p>
<div id="attachment_16333" class="wp-caption aligncenter" style="width: 595px"><img class="size-full wp-image-16333 " src="http://altdevblogaday.com/wp-content/uploads/2011/09/chart3a.png" alt="" width="585" height="294" /><p class="wp-caption-text">Round Tripping</p></div>
<p>With a build of the game ready to run and the export script in place, this is made possible.Typically, the designer will have the level editor still open after exporting the data. Running the game will load the data and serve as a preview. If the game runs on a browser its only a matter of refreshing it to reload new data. Now all that is left to do is follow the sequence:  Edit &#8211;  Export &#8211;  Preview, rinse and repeat.</p>
</div>
<div>
<h4>Extending the Loader</h4>
<p>The described method works well for simple game objects that are based on a single asset. However, not all objects are simple. Some object are logical only with no assets, but we still want to be able to place them and get visual indication. Such are trigger boxes, waypoints and spawn points. For these we can create simple shapes (like a semi transparent box or some icon) and use proxy assets.<br />
There is also the option of putting more data into each object. As long as there is a convenient way of entering meta-data in the editor, it can be exported with the object. An example of that would be a ‘fail zone’ &#8211; an invisible trigger box that triggers a level fail when touched by the player object. So a generic trigger box can be extended with a message code (integer or string) that will be specified in the editor &#8211; opening the possibility for the designer to control simple game logic in different scenarios.<br />
These type of ‘programmable’ object not only save time, but also frees the designer to experiment and quickly try out different layouts and configurations without requiring further coding.</p>
<h4>Conclusion</h4>
<p>Sophisticated level editing should not be an exclusive perk of heavy platforms the likes of Unreal3. With little effort, even small indie developers can come up with decent tools to help take control over content pipeline and speed up game content creation.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexwiz.net/make-your-own-level-editor-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

