<?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>Hybrid Mind Studios &#187; Tutorials</title>
	<atom:link href="http://hybridmind.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://hybridmind.com</link>
	<description>the adventures of</description>
	<lastBuildDate>Mon, 16 Jan 2012 18:19:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Playtomic Analytics In Your Flash Games</title>
		<link>http://hybridmind.com/tutorials/using-playtomic-analytics-in-your-flash-games/</link>
		<comments>http://hybridmind.com/tutorials/using-playtomic-analytics-in-your-flash-games/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 18:24:35 +0000</pubDate>
		<dc:creator>Dave "HybridMind" Evans</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[analytics]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[leaderboards]]></category>
		<category><![CDATA[level]]></category>
		<category><![CDATA[playtomic]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[sharing]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[tracking]]></category>

		<guid isPermaLink="false">http://hybridmind.com/?p=858</guid>
		<description><![CDATA[Playtomic is a free game analytics service I have been using in my Flash games for almost two years now. It provides a real time tracking platform not only for Flash games, but also for games made with HTML5, iOS, and Unity. Playtomic is a great way to understand how playertesters are playing your games [...]]]></description>
			<content:encoded><![CDATA[<p><em><a href="http://hybridmind.com/wp-content/uploads/2011/07/Playtomic1.png"><img class="alignleft size-full wp-image-866" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Playtomic Game Analytics and Services for Casual Games" src="http://hybridmind.com/wp-content/uploads/2011/07/Playtomic1.png" alt="Playtomic Game Analytics and Services for Casual Games" width="198" height="198" /></a></em><em><a title="Playtomic Game Analytics and Services for Casual Games" href="http://playtomic.com/" target="_blank">Playtomic </a></em>is a <strong>free</strong> game analytics service I have been using in my <em>Flash</em> games for almost two years now. It provides a real time tracking platform not only for <em>Flash</em> games, but also for games made with <em>HTML5</em>, <em>iOS</em>, and <em>Unity</em>.</p>
<p><em>Playtomic</em> is a great way to understand how playertesters are playing your games during the development phases of your game and then to further study how your game is being played and spread around the world once you release it.  Beyond the real time analytics tools it also offers global leaderboards, heatmaps, custom game data, and level sharing APIs.</p>
<p>I can&#8217;t recommend <em>Playtomic</em> enough as it has really had a large and beneficial impact on the way I approach my game development, playtesting, and publishing. I&#8217;m not the only one enjoying <em>Playtomic</em> either&#8211;over 2,000 developers are currently using the service to track over 3,000 individual games, and logging an amazing billion combined real time events per day!</p>
<div style="text-align: center;"><strong>Playtomic Analytics Tutorial Demo</strong></div>
<div style="text-align: center;"><object id="ViewPort" width="550" height="400" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://hybridmind.com/PlaytomicTestGame.swf" /><param name="quality" value="high" /><embed id="ViewPort" width="550" height="400" type="application/x-shockwave-flash" src="http://hybridmind.com/PlaytomicTestGame.swf" quality="high" /></object></div>
<p><span style="text-decoration: underline;"><strong>TUTORIAL</strong></span></p>
<p>This tutorial will focus on the basics of getting your account setup with <em>Playtomic</em> and getting the basics of the analytics working in your game SWF.  I&#8217;ll be using the latest <a title="Playtomic API for ActionScript 3.0" href="http://playtomic.com/api/as3" target="_blank"><em>Playtomic</em> API version 3.15 for <em>ActionScript</em> 3.0</a> (though <em>Playtomic</em> does <a title="Playtomic API for ActionScript 2.0" href="http://playtomic.com/api/as2" target="_blank">support <em>ActionScript</em> 2.0</a> as well.) I&#8217;ll also be using the Flash IDE to create the game project but the code I&#8217;ll be demonstrating will work basically the same if you&#8217;re not using <em>Flash</em>. I&#8217;ll <a href="http://hybridmind.com/PlaytomicTestGameTutorialv1.zip">provide the source for the demo</a> above saved as a <em>Flash</em> CS4 project in case you haven&#8217;t upgraded to the latest version yet.</p>
<p><span style="text-decoration: underline;"><strong> REGISTER FOR YOUR ACCOUNT AND CREATE YOUR FIRST GAME</strong></span></p>
<p><a href="http://hybridmind.com/wp-content/uploads/2011/07/Register.png"><img class="alignleft size-full wp-image-875" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Register for Playtomic" src="http://hybridmind.com/wp-content/uploads/2011/07/Register.png" alt="Register for Playtomic" width="200" height="114" /></a><strong>Register:</strong></p>
<p>First step is to simply <a title="Register for a Playtomic account" href="http://playtomic.com/register" target="_blank">register your account on <em>Playtomic</em></a>. This is pretty basic and you don&#8217;t even have to wait for an email for your account to activate.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="http://hybridmind.com/wp-content/uploads/2011/07/NewGame.png"><img class="alignleft size-full wp-image-877" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Create Game Screen" src="http://hybridmind.com/wp-content/uploads/2011/07/NewGame.png" alt="Create Game Screen" width="200" height="110" /></a><strong>Create First Game:</strong></p>
<p>The next step is to create your first game in the system. I&#8217;m just using a &#8216;Test Game&#8217; for the purposes of this tutorial.  The important part is to hit the two radio buttons. Select &#8216;Casual/mobile game&#8217; for &#8216;Game Type&#8217; and &#8216;Web&#8217; under the &#8216;Flash&#8217;  platform area. Then just click the &#8216;Create Game&#8217; button and the system will bring you to the game overview screen for API setup.</p>
<p>&nbsp;</p>
<p><strong><a href="http://hybridmind.com/wp-content/uploads/2011/07/NewGameOverview1.png"><img class="alignleft size-full wp-image-889" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="New Game API Overview" src="http://hybridmind.com/wp-content/uploads/2011/07/NewGameOverview1.png" alt="New Game API Overview" width="200" height="144" /></a>First Game API Overview:</strong></p>
<p>You should now be looking at the API information for the game you just created. The most important information here is the game credentials area that lists your <em><strong>SWFID</strong></em> and <em><strong>GUID</strong></em>.  You should keep these numbers private since they are the way the API will communicate from your game to the <em>Playtomic</em> system.</p>
<p>The other important areas are the documentation and downloads areas. I&#8217;ve marked everything on the screenshot to the left that you can click to enlarge. You can get to this screen by clicking on &#8216;API Setup&#8217; in the vertical nav menu at the left when you are logged in on the Playtomic website.</p>
<p><strong>Download the Tutorial:</strong></p>
<p>Normally you&#8217;ll click on the download link for the latest <em>Playtomic</em> API zip file on the <em>Playtomic</em> website. You&#8217;ll then extract the contents of the zip and find the folder called &#8216;Playtomic&#8217; inside the folder called &#8216;gameapi-as3&#8242;.  You&#8217;ll want to copy the &#8216;Playtomic&#8217; folder into the root folder of your game project so that the folder is at the same directory level as your FLA file.</p>
<p>This time though I&#8217;ve already included the latest <em>Playtomic</em> API within the tutorial download. At the time this tutorial was written that was version 3.15 for ActionScript 3.0.</p>
<p><a href="http://hybridmind.com/PlaytomicTestGameTutorialv1.zip">You can download the tutorial here</a>. I saved the FLA as a <em>Flash</em> CS4 FLA for people using older versions of <em>Flash</em> still.</p>
<p>Extract the tutorial zip and then open up both TestGame.fla and TestGame.as in the <em>Flash</em> IDE. </p>
<p>I&#8217;m only going to point out the most important parts from the source. The tutorial TestGame.as source file itself is heavily commented and should be fairly self-explanatory.  This tutorial also assumes you know the basics of ActionScript 3.0.</p>
<p>In order for everything to work you are going to need to have already setup your new game on the <em>Playtomic</em> website from the instructions further above.  If not go ahead and do that now because you&#8217;ll need your unique SWFID and GUID to plug into the TestGame.as config area.</p>
<pre><code>
// Playtomic API constants
// NOTE: CHANGE THESE TO MATCH YOURS OR IT WON'T WORK!
//
// SWFID from Playtomic API Setup screen
private static const PLAYTOMIC_SWFID:String = "xxxxxxxxxxxxxxxxx";

// GUID from Playtomic API Setup screen
private static const PLAYTOMIC_GUID:int = xxxx;
</pre>
<p></code></p>
<p>Before you can use the API you'll need to import the <em>Playtomic</em> classes. You'll see I do that near the top of the source file with the other standard Flash imports.</p>
<pre><code>
// import the Playtomic API
import Playtomic.*;
</code></pre>
<p><a href="http://hybridmind.com/wp-content/uploads/2011/07/CustomMetrics.png"><img class="alignleft size-full wp-image-928" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Custom Metrics" src="http://hybridmind.com/wp-content/uploads/2011/07/CustomMetrics.png" alt="Custom Metrics" width="200" height="132" /></a></p>
<p>You need to init the <em>Playtomic</em> API as soon as possible in your game code and definitely before you call any of the <em>Playtomic</em> services.  I usually do this with an init function of some type called during my FLA Document class constructor phase.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre><code>
// Init the Playtomic API as soon as possible and
//   before you call any other Playtomic services!
private function init():void {
  // Initialize the API by logging the view
  //   this registers a 'view' on the server
  Log.View(PLAYTOMIC_GUID, PLAYTOMIC_SWFID, root.loaderInfo.loaderURL);
}
</code></pre>
<p>While we have now logged a 'view' above we want to track how many times the game will actually be played during that view session.  In other words there should be more 'plays' of your game than 'views'.  If not, the view to play ratio is a great indication that your game could use some improvement!</p>
<p>I typically track the 'play' event when my gameplay assets are being instantiated.</p>
<pre><code>
// Play the game
private function playGame():void {
  // Registers a 'play' event on the server. There can be multiple 'play' events per 'view'
  Log.Play();
  // do one time setup for game
  setupGame();
}
</code></pre>
<p><a href="http://hybridmind.com/wp-content/uploads/2011/07/CustomMetrics.png"><img class="alignleft size-full wp-image-928" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Custom Metrics" src="http://hybridmind.com/wp-content/uploads/2011/07/CustomMetrics.png" alt="Custom Metrics" width="200" height="132" /></a></p>
<p>Another very easy and powerful thing to do with Playtomic is to track custom counter metrics. I have included an example in the tutorial.  Whenever the player clicks on the 'Credits' button on the main menu we track a 'ViewedCredits' event.  You can use these events to track all sorts of handy data. I usually track common things like every time the credits are viewed, the instructions are viewed, the sound or music is toggled on or off, how many times the game is restarted or paused.  Basically any handy data point you'd like to gather to start understand how players interact with your game or the menus surrounding your game.</p>
<pre><code>
// Listens for mouse click on credits button
private function creditsButtonClickListener(event:MouseEvent):void {
  // Track a custom metric on the server so we know a
  // player viewed the credits screen
  Log.CustomMetric("ViewedCredits"); // metric, names must be alphanumeric
</code></pre>
<p><a href="http://hybridmind.com/wp-content/uploads/2011/07/LevelMetrics.png"><img class="alignleft size-full wp-image-928" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Level Metrics" src="http://hybridmind.com/wp-content/uploads/2011/07/LevelMetrics.png" alt="Level Metrics" width="200" height="132" /></a></p>
<p>There are many other types of metrics you can track too. I like putting in tracking for average score so that even if players aren't submitting a score I can see how players are doing with the game.  You can do this with a level average metric call.  When your game doesn't have individual levels you can just feed in the string 'Game' (or whatever you want) that represents the entire game and not a specific level.</p>
<p>In the tutorial I track a level average metric when the player hits the 'Quit' button. </p>
<pre><code>
// Quit game button click listener
private function quitGameButtonClickListener(event:MouseEvent):void {
  // Track the current score as a level average metric event
  //   You can track averages for specific levels or the entire game.
  //   Here the string 'Game' represents the entire game not a
  //   particular level string.
  Log.LevelAverageMetric("Score", "Game", score);
</code></pre>
<p>That's about it! The <em>Playtomic</em> website documentation is pretty decent and the community forums on the site are starting to mature a bit so that you can often get help from a question posted up there before too long. Once you integrate <em>Playtomic</em> into your own game you'll start thinking up all kinds of great uses for it. I've used the global leaderboards and level sharing APIs and will start experimenting with the heatmaps soon. I will use this base tutorial code to write up a few followup tutorials on <em>Playtomic</em> soon.</p>
<p>Let me know if I can improve this tutorial and if you have any questions at all!</p>
<p>[<a href="http://hybridmind.com/PlaytomicTestGameTutorialv1.zip">DOWNLOAD FLASH CS4 VERSION OF TUTORIAL</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://hybridmind.com/tutorials/using-playtomic-analytics-in-your-flash-games/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Flash Game Polish Tips: Audio</title>
		<link>http://hybridmind.com/tutorials/flash-game-polish-tips-audio/</link>
		<comments>http://hybridmind.com/tutorials/flash-game-polish-tips-audio/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 13:48:16 +0000</pubDate>
		<dc:creator>Dave "HybridMind" Evans</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[best]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Game Business]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[licensing]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[polish]]></category>
		<category><![CDATA[practices]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[sponsorship]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[volume]]></category>

		<guid isPermaLink="false">http://hybridmind.com/?p=824</guid>
		<description><![CDATA[I spend a lot of time thinking about things that will improve my Flash games. If you use sponsorship or licensing models in the Flash game business you are really marketing to two audiences&#8211;your players and your potential sponsors. I want to provide the best experience I can to my players within a reasonable amount of [...]]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-844 alignleft" style="margin-left: 10px; margin-right: 10px; border-width: 3px; border-color: black; border-style: solid;" title="Headphones" src="http://hybridmind.com/wp-content/uploads/2011/07/Headphones.png" alt="Headphones" width="200" height="200" />I spend a lot of time thinking about things that will improve my <a title="Hybrid Mind Studios Flash Games" href="http://hybridmind.com/games/" target="_blank">Flash games</a>. If you use <a title="Flash Game Sponsorship and Licensing" href="http://www.flashgamelicense.com/view_library.php?page=what-is-a-sponsorship" target="_blank">sponsorship or licensing models</a> in the Flash game business you are really marketing to two audiences&#8211;your players and your potential sponsors.</p>
<p>I want to provide the best experience I can to my players within a reasonable amount of effort while at the same time I also want to make sure my game is going to provide as little friction as possible to potential sponsorship or licensing.</p>
<p>To that end I have 5 tips I wanted to share for dealing with audio in your Flash games that I&#8217;ve found helpful:</p>
<p><span style="text-decoration: underline;"><strong>Tip #1 &#8211; Normalize your music and sound effects</strong></span></p>
<p>This is a very simple tip but it is too often overlooked. <a title="Audio normalization" href="http://en.wikipedia.org/wiki/Audio_normalization" target="_blank">Normalizing</a> is basically just making sure that the loudest volume levels of all your sound effects and music are at a consistent and good target peak.</p>
<p>Normalizing your audio assets <em>before</em> you import them into your Flash library is a good way to improve your workflow. You know that all your audio is within a consistent peak range and you can balance further with your software volume settings.</p>
<p>You can use a free program like <em><a title="Audacity" href="http://audacity.sourceforge.net/" target="_blank">Audacity</a> </em>to provide normalization. I&#8217;ve found the default setting of normalizing to -3dB works well.  If you are using Windows 7 you&#8217;ll want to get the <em>beta</em> for <em>Audacity.</em></p>
<p><span style="text-decoration: underline;"><strong>Tip #2 &#8211; Individual controls for sound effects and music</strong></span></p>
<p>Why provide two controls? A nice thing about having independent controls for sound effects and music is that it can give the player more freedom over the experience of your game. It can allow them to keep your sound effects playing but still jam out to their own music. Another reason is sometimes the player doesn&#8217;t mind the sound effects but maybe the music is starting to get repetitive the 100th time they&#8217;ve played your chain reaction game. I look for any little tips to keep players happy and in the game and I&#8217;ve had many positive player comments from implementing this feature.</p>
<p><span style="text-decoration: underline;"><strong>Tip #3 &#8211; Provide common key bindings for audio control toggles</strong></span></p>
<p>Setup a keyboard listener in Flash to respond to some common keys. I like to bind the &#8216;S&#8217; key to toggle the sound effects and the &#8216;M&#8217; key to toggle the music.  This is a real simple touch that allows your players to quickly mute your game when they can&#8217;t find the onscreen audio controls. Remember to provide text somewhere to let your players know these settings controls!</p>
<p><span style="text-decoration: underline;"><strong>Tip #4 &#8211; Use volume sliders for sound effects and music</strong></span></p>
<p>The good thing about providing volume sliders over a simple mute/un-mute toggle is that your players can mix the volume to suit their individual speakers or headphones. It can be incredibly hard to master and mix your game volumes correctly so that it will sound decent and in balance across the many audio systems it will be played through. Volume sliders give the power to the players to tweak one up or down to get that balance that is pleasing to their ears.</p>
<p><span style="text-decoration: underline;"><strong>Tip #5 &#8211; Save sound and music settings in a shared object</strong></span></p>
<p>This last tip I have implemented most recently based on some player feedback I got. I really like this tip because it never occurred to me until I received a story from a player. The player got busted by their boss for playing one of my games in the office because they were surprised when they loaded it up again and the audio of both the preloader intro movies and the main menu music was still enabled.  They had just assumed that since they had muted the game audio the last time they had played that it would have remembered!</p>
<p>Well I had never really thought of this feature before but it was quite easy to implement and has been really awesome. You can use a <a title="Flash Shared Object" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html" target="_blank">Shared Object</a> in Flash to track any changes to the audio controls.  You might be using one already to record local highscore data and you can just record your audio settings to this object.  Then when your game loads you can check the Shared Object to see what the audio settings were (if any) the last time they played. If there isn&#8217;t any data then you can just enable it as as default. This allows you to mute the preloader intro movies and menu music when they return if they had disabled it in a previous gameplay session!</p>
<p><em><strong>Got any other good audio related tips? Share them below in the comments!</strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://hybridmind.com/tutorials/flash-game-polish-tips-audio/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple AS3 Viewport Tutorial</title>
		<link>http://hybridmind.com/tutorials/simple-as3-viewport-tutorial/</link>
		<comments>http://hybridmind.com/tutorials/simple-as3-viewport-tutorial/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 16:21:40 +0000</pubDate>
		<dc:creator>Dave "HybridMind" Evans</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[circles]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[gamedev]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[swf]]></category>
		<category><![CDATA[teaching]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[viewport]]></category>

		<guid isPermaLink="false">http://hybridmind.com/?p=433</guid>
		<description><![CDATA[Simple Flash AS3 Viewport Demo Click inside and use arrow keys to move viewport This is a little demo I made this morning as a way to get a viewport concept working for the latest game I am developing.  I&#8217;ve done a few viewport type projects in other languages but had not implemented one in [...]]]></description>
			<content:encoded><![CDATA[<div style="text-align: center;"><strong>Simple Flash AS3 Viewport Demo</strong></div>
<div style="text-align: center;"><object id="ViewPort" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="350" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="name" value="ViewPort" /><param name="bgcolor" value="#000000" /><param name="src" value="http://hybridmind.com/ViewPort.swf" /><param name="quality" value="high" /><embed id="ViewPort" type="application/x-shockwave-flash" width="350" height="300" src="http://hybridmind.com/ViewPort.swf" quality="high" bgcolor="#000000" name="ViewPort"></embed></object></div>
<div style="text-align: center;"><em>Click inside and use arrow keys to move viewport</em></div>
<p>This is a little demo I made this morning as a way to get a viewport concept working for the latest game I am developing.  I&#8217;ve done a few viewport type projects in other languages but had not implemented one in Flash AS3 yet.</p>
<p>This demo creates 300 random balls and sets them in motion inside the world.  The world&#8217;s dimensions are four times larger than the viewport above.</p>
<p>You can click in the window above and use the arrow keys to move the viewport around the world.  The source files for this tutorial are available at the end of the article.</p>
<p><span id="more-433"></span></p>
<h4>BACKGROUND<strong> </strong></h4>
<p>I needed to be able to have world coordinates for my latest game along with a viewport which follows the player.  All the objects in the world must move according to various physics and be able to be mapped into the current viewport for display purposes.  After hacking around for a while unsuccessfully last night on the game code I realized this morning that it would just be better to start fresh on a little proof of concept demo until I had it all working.</p>
<p>I thought I&#8217;d share some of the concepts in case it could help someone else as well as get any code or design feedback from anyone who may have implemented these in a different or more efficient way.  I can already think of a ton of improvements my rudimentary solution could use but I&#8217;ll add them as necessary.</p>
<h4>BASIC DOCUMENT CLASS VARS AND CONSTANTS:</h4>
<pre><code>
// Viewport related constants:
private static const VIEWPORT_WIDTH:Number = 350;
private static const VIEWPORT_HEIGHT:Number = 300;
private static const VIEW_STEP:int = 25;

// World related constants:
public static const WORLD_WIDTH:Number = 1400;
public static const WORLD_HEIGHT:Number = 1200;

// Current viewport coords:
private var viewportX:Number;
private var viewportY:Number;
</code></pre>
<h4>INITIAL SETUP OF VIEWPORT AND CREATING CIRCLES WITHIN WORLD:</h4>
<p>This snippet is occurring inside an init() function that is called from the document class constructor.  This is all one time setup.</p>
<pre><code>
// set view port coords to middle of world:
viewportX = WORLD_WIDTH / 2;
viewportY = WORLD_HEIGHT / 2;

// create a bunch of random circles scattered across the world:
circles = new Array();
for (var i:int = 0; i &lt; NUM_CIRCLES; i++) {
var circle:Circle = new Circle();

// grab random x that falls within world space:
var wX:Number = GameUtils.randRange(0 + (circle.width / 2), WORLD_WIDTH - (circle.width / 2));

// grab random y that falls within world space:
var wY:Number = GameUtils.randRange(0 + (circle.height / 2), WORLD_HEIGHT - (circle.height / 2));

// set the coordinates for our circle:
circle.placeInWorld(wX, wY);

// add our circle to the display list:
addChild(circle);

// push our circle onto our array of circles:
circles.push(circle);
</code></pre>
<h4>SNIPPET OF HANDLING KEYBOARD INPUT TO MOVE THE VIEWPORT:</h4>
<p>This snippet is occurring inside a keyDown listener function which listens for the user hitting the arrow keys.  Each time the keys are hit it moves the viewport in the appropriate direction while performing range checking with the world borders.</p>
<pre><code>
if (e.keyCode == 38) {            // UP
 viewportY -= VIEW_STEP;
 if (viewportY &lt; 0) { viewportY = 0; }
}
</code></pre>
<h4>MAIN PROGRAM LOOP:</h4>
<p>This is a simple enterFrame listener that loops through all the active circles and calls their update method.  After the update method we alter the coordinates of the parent display object (in this case the instance of our viewport) inversely with the viewport coordinates.  This allows Flash to automatically handle updating all the positions of the circles which are children of our viewport instance.  <em>Thanks to <a href="http://www.draknek.org" target="_blank">draknek</a> for this elegant suggestion!</em></p>
<pre><code>
private function enterFrameListener(e:Event):void {
	for (var i:int = 0; i &lt; circles.length; i++) {
		circles[i].update();
	}

	// pan the viewport
	x = -viewportX;
	y = -viewportY;
}
</code></pre>
<h4>CIRCLE CLASS VARS:</h4>
<p>Here are the variables that define my circle class.  This class is pretty basic and nothing too special has to happen here with regards to the viewport.  The class will have to perform range checking with the borders of the world.</p>
<pre><code>
// Velocity:
private var velX:Number;
private var velY:Number;
</code></pre>
<h4>CIRCLE UPDATE METHOD:</h4>
<p>Here is a snippet from the update method that gives the basic idea of how to adjust the coords while range checking against the larger world borders.</p>
<pre><code>
// Adjust the x coord by current x velocity:
x += velX;

// Perform boundary checking with world borders and adjust
// position and velocity direction accordingly:
if (x &lt; (width / 2)) {
	x = (width / 2);
	velX = -velX;
} else if (x &gt; ViewPort.WORLD_WIDTH - (width / 2)) {
	x = ViewPort.WORLD_WIDTH - (width / 2);
	velX = -velX;
}
</code></pre>
<p>So that is my concept of one way to do a basic viewport in AS3.  I am happy to get any feedback on this code and the way I solved this problem.  I know there are many ways to implement these things but this is what I am going to run with for now as it seems to be working pretty good!</p>
<p><em>EDIT: Thank you to <a href="http://www.draknek.org" target="_blank">Draknek</a> (see comments below) for an elegant refinement to my code that allowed me to no longer have to track world coordinates on the child objects.  Now I can just let Flash handle updating all the positions for me by moving the parent display object instead of manually doing that.</em></p>
<p><a href="/ViewportTutorial.zip">Download the v0.2 source files and fla for this tutorial</a></p>
]]></content:encoded>
			<wfw:commentRss>http://hybridmind.com/tutorials/simple-as3-viewport-tutorial/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

