<?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>Jayc Santos &#187; Flash</title>
	<atom:link href="http://jaycsantos.com/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://jaycsantos.com</link>
	<description>Games, Development, Blog</description>
	<lastBuildDate>Thu, 10 May 2012 12:15:59 +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>Play Putt More Base</title>
		<link>http://jaycsantos.com/games/play-putt-more-base/</link>
		<comments>http://jaycsantos.com/games/play-putt-more-base/#comments</comments>
		<pubDate>Thu, 10 May 2012 12:15:59 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Putt More Base]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[flash game license]]></category>
		<category><![CDATA[game update]]></category>
		<category><![CDATA[golf]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=588</guid>
		<description><![CDATA[The game has been playable for a week now.. It&#8217;s been doing good and bugs/glitches has been fixed. Help take the game out of judgement Play it over at NEWGROUNDS! Play it over at KONGREGATE! Spoiler and a could be contest after&#8230; I&#8217;m thinking of how soon to release a walk through, hit the comments [...]]]></description>
			<content:encoded><![CDATA[<p>The game has been playable for a week now.. It&#8217;s been doing good and bugs/glitches has been fixed.</p>
<div id="attachment_589" class="wp-caption alignnone" style="width: 335px"><a href="http://jaycsantos.com/uploads/2012/05/puttmorebase-Pro-4-1249020120301-2014-result.png"  onclick="return false"><img id="img_588_puttmorebase-Pro-4-1249020120301-2014-result_png" class=" wp-image-589  " title="puttmorebase-Pro-4-12490[20120301-2014-result]" src="http://jaycsantos.com/uploads/2012/05/puttmorebase-Pro-4-1249020120301-2014-result.png" alt="" width="325" height="200"onclick="enlarge(this)" longdesc="http://jaycsantos.com/uploads/2012/05/puttmorebase-Pro-4-1249020120301-2014-result.png" /></a><p class="wp-caption-text">Hole - Pro 4</p></div>
<blockquote>
<h3>Help take the game out of judgement</h3>
<p><strong>Play it over at <a href="http://www.newgrounds.com/portal/view/595263" target="_blank">NEWGROUNDS</a>!</strong><br />
<strong> Play it over at <a href="http://www.kongregate.com/games/jaycsantos/putt-more-base" target="_blank">KONGREGATE</a>!</strong></p></blockquote>
<p>Spoiler and a could be contest after&#8230;</p>
<p><span id="more-588"></span></p>
<p><div id="attachment_590" class="wp-caption alignright" style="width: 150px"><a href="http://jaycsantos.com/uploads/2012/05/puttmorebase-Hole-18-1199020120302-1535-solution.png"  onclick="return false"><img id="img_588_puttmorebase-Hole-18-1199020120302-1535-solution_png" class="size-thumbnail wp-image-590" title="puttmorebase-Hole-18-11990[20120302-1535-solution]" src="http://jaycsantos.com/uploads/2012/05/puttmorebase-Hole-18-1199020120302-1535-solution-140x105.png" alt="" width="140" height="105"onclick="enlarge(this)" longdesc="http://jaycsantos.com/uploads/2012/05/puttmorebase-Hole-18-1199020120302-1535-solution.png" /></a><p class="wp-caption-text">Hole 18 - under par score! Guess how many shots it took?</p></div>I&#8217;m thinking of how soon to release a walk through, hit the comments below if you want it sooner!</p>
<p>Also, create, share and post your custom levels here <a title="Putt More Base" href="http://jaycsantos.com/games/putt-more-base/">or here</a>, who knows I might put a small contest for submitted custom levels. <img src='http://jaycsantos.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p>For sponsors/publishers, the game is available for site-locked non-exclusive licenses, <a href="http://jaycsantos.com/contact/#subject/Interested in licensing Putt More Base">contact me</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/games/play-putt-more-base/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Putt More Better Screens or not?</title>
		<link>http://jaycsantos.com/games/putt-more-better-screen-or-not/</link>
		<comments>http://jaycsantos.com/games/putt-more-better-screen-or-not/#comments</comments>
		<pubDate>Wed, 29 Feb 2012 13:34:57 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Arts n Visuals]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Putt More Base]]></category>
		<category><![CDATA[art]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=560</guid>
		<description><![CDATA[Just sharing a couple of renewed designs for the success and select level screen. Also the success screen now has fancy animations upon popping up. What do you think, an improvement or not? Its not final-final either way.]]></description>
			<content:encoded><![CDATA[<div id="attachment_561" class="wp-caption alignnone" style="width: 490px"><img class="size-full wp-image-561 " title="old-new designs" src="http://jaycsantos.com/uploads/2012/02/puttbase.update.20120229.jpg" alt="" width="480" height="296" /><p class="wp-caption-text">from the old (left) to the newer (right) designs</p></div>
<p>Just sharing a couple of renewed designs for the success and select level screen. Also the success screen now has fancy animations upon popping up.</p>
<p>What do you think, an improvement or not? Its not final-final either way. <img src='http://jaycsantos.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/games/putt-more-better-screen-or-not/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Putt More Update</title>
		<link>http://jaycsantos.com/games/putt-more-update/</link>
		<comments>http://jaycsantos.com/games/putt-more-update/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 21:26:49 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Putt More Base]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game update]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=557</guid>
		<description><![CDATA[We have great progress on the new game that it&#8217;ll be having beta testings within the succeeding days. Everything is as solid as it could be, so far all known bugs are squished to nothingness. Only a few more hard levels to be designed for 99% completion &#8211; the remaining 1% is not to be [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_558" class="wp-caption alignnone" style="width: 470px"><img class="size-full wp-image-558  " title="sshot-2012-02-23-[1]" src="http://jaycsantos.com/uploads/2012/02/sshot-2012-02-23-1.jpg" alt="" width="460" height="283" /><p class="wp-caption-text">main menu - yes, its interactive!</p></div>We have great progress on the new game that it&#8217;ll be having beta testings within the succeeding days. Everything is as solid as it could be, so far all known bugs are squished to nothingness.</p>
<p>Only a few more hard levels to be designed for 99% completion &#8211; the remaining 1% is not to be questioned.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/games/putt-more-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bump Bump</title>
		<link>http://jaycsantos.com/games/bump-bump/</link>
		<comments>http://jaycsantos.com/games/bump-bump/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 07:18:14 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Putt More Base]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[game update]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=554</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<div><div id="attachment_555" class="wp-caption alignnone" style="width: 470px"><img class="size-full wp-image-555 " title="sshot-2011-10-16-[2]" src="http://jaycsantos.com/uploads/2012/02/sshot-2011-10-16-2.jpg" alt="Putt MORE Base" width="460" height="280" /><p class="wp-caption-text">..incoming..!</p></div></div>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/games/bump-bump/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The 2nd Come2Play Competition &#8211; $10,000 in Prizes</title>
		<link>http://jaycsantos.com/flash/the-2nd-games-that-challenge-the-world-contest-10000/</link>
		<comments>http://jaycsantos.com/flash/the-2nd-games-that-challenge-the-world-contest-10000/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 05:55:14 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[multiplayer]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=551</guid>
		<description><![CDATA[Making an awesome game? why not join the &#8216;2nd Games that Challenge the World Contest!&#8217; Come2Play with LinkedIn Flash Game Developers Group has launched the 2nd multiplayer flash game contest, and this time with prizes totaling $10,000! Last years contest had only a handful of entries, so chances of winning the $5,000 pot for first [...]]]></description>
			<content:encoded><![CDATA[<p>Making an awesome game? why not join the &#8216;<a href="http://bit.ly/GCWContest" target="_blank">2nd Games that Challenge the World Contest</a>!&#8217;</p>
<div id="attachment_553" class="wp-caption aligncenter" style="width: 310px"><a href="http://bit.ly/GCWContest"><img class="size-full wp-image-553 " title="2nd Games that Challenge the World Contest" src="http://jaycsantos.com/uploads/2011/02/come2play.contest21.jpg" alt="2nd Games that Challenge the World Contest" width="300" height="188" /></a><p class="wp-caption-text"> </p></div>
<p><a href="http://come2play.com/" target="_blank">Come2Play</a> with <a href="http://www.linkedin.com/groups?mostPopular=&amp;gid=121017&amp;trk=myg_ugrp_ovr">LinkedIn Flash Game Developers Group</a> has launched the 2nd multiplayer flash game contest, and this time with <strong>prizes totaling $10,000</strong>! Last years contest had only a handful of entries, so chances of winning the $5,000 pot for first prize this time is very high. Check out the winners games and other entries of the 1st &#8216;<a href="http://www.come2play.com/dev_inner.asp?f=1&amp;newsid=337" target="_blank">Games that Challenge the World Contest</a>&#8216;.</p>
<ul>
<li><strong>1st place</strong> &#8211; <strong>$5,000</strong></li>
<li>2nd place &#8211; $3,000</li>
<li>3rd place &#8211; $1,250</li>
<li>4th place &#8211; $750</li>
</ul>
<p>Flash games developers are welcome to join and take the pot.  Create awesome head-to-head turned based multiplayer games, use  come2play&#8217;s multiplayer API, submit and win! Prizes will be given out to the game which will have the best replayability both for new and return players, game design, concept and execution.</p>
<p>Developers keep all rights to the submitted games and even previously published games can be retrofitted to enter. Game entries does not have to be exclusive, so pre-released games are also welcome.</p>
<p>&#8220;<em>So I&#8217;m interested, but only have a single player game</em>&#8221; &#8211; Not a problem, see the developer tips on how to evolve your single player game into an interactive multiplayer game.</p>
<p>For complete contest info <a href="http://bit.ly/GCWContest" target="_blank"><strong>check out the official contest page</strong></a>.</p>
<blockquote><p><em>Edit:</em> Deadline has been extended to April 15th!</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/the-2nd-games-that-challenge-the-world-contest-10000/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Trick to Using SharedObject</title>
		<link>http://jaycsantos.com/flash/the-trick-to-using-sharedobject/</link>
		<comments>http://jaycsantos.com/flash/the-trick-to-using-sharedobject/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 12:30:18 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[code bits]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=470</guid>
		<description><![CDATA[Having a SAVE feature in a project/game is one of the common things that could lead users to be more attracted and increase replay value for potentially recovering what was done in a previous visit. Using the SharedObject class in actionscript project/games is sometimes a requirement or at least a need, may it be AS1 [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_472" class="wp-caption alignleft" style="width: 210px"><img class="size-full wp-image-472" title="SharedObjects" src="http://jaycsantos.com/uploads/2009/04/sharedobject.gif" alt="Shared Objects ~= Browser Cookies" width="200" height="90" /><p class="wp-caption-text"> </p></div>
<p>Having a <em>SAVE</em> feature in a project/game is one of the common things that could lead users to be more attracted and increase replay value for potentially recovering what was done in a previous visit. Using the SharedObject class in actionscript project/games is sometimes a requirement or at least a need, may it be AS1 (FP6), 2 or 3. It can be used to store valuable data like progress, settings, statistics, achievements and a lot more without dedicating storage on the server. Basically, SO or shared objects are like the browser cookies for flash, it is used to store some amount of data on the user machine, therefore it also has some limitations on its use (for security purposes really).</p>
<p>By reading the documentations or the livedocs, either for <a rel="external nofollow" href="http://livedocs.adobe.com/flash/9.0/main/00002118.html" target="_blank">AS2</a> or <a rel="external nofollow" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html" target="_blank">AS3</a> (it&#8217;s really the same), you might have a gut start of what key points to remember.</p>
<ul>
<li>created shared objects can only be stored/retrieved on the same domain</li>
<li>shared objects cannot be shared across domains, meaning a shared objects from &#8220;examplesite.com&#8221; cannot read shared objects created by another domain &#8220;mywebsite.com&#8221;</li>
<li>users can disallow local storage per domain or opt-out globaly, then the object would not be saved locally</li>
<li>data size to be stored is limited by which the user sets it (none, 10kb, 100kb, 1mb, 10mb, unlimited), although a dialog box can request an increase in storage if what is set is exceeded</li>
<li>if the user sets a limit smaller than the shared object, the shared object would be deleted</li>
<li>shared objects are stored on the user&#8217;s local machine not on the server, something shared over another machine cannot be accessed by another machine</li>
</ul>
<p>The most essential part when using SharedObject is the <code>SharedObject.getLocal()</code> method, which is what we will be discussing more through out. Basically, AS1/2 and AS3 syntax haven&#8217;t really changed (although a few more rules were imposed in as3).</p>
<blockquote class="white"><p><strong><span style="color: #000080;">getLocal</span></strong>(name:<span style="color: #000080;">String</span>, localPath:<span style="color: #000080;">String </span>= null, secure:<span style="color: #000080;">Boolean </span>= false):<span style="color: #000080;">SharedObject</span><br />
[static] Returns a reference to a locally persistent shared object that is only available to the current client.</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> mySo:<span style="color: #0066CC;">SharedObject</span> = <span style="color: #0066CC;">SharedObject</span>.<span style="color: #0066CC;">getLocal</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'mySaveName'</span>, localPath, secure <span style="color: #66cc66;">&#41;</span>;
mySo.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">thingtoSave</span> = myVariable; <span style="color: #808080; font-style: italic;">// your variables</span>
mySo.<span style="color: #0066CC;">flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// stores data into disk</span></pre></div></div>

<hr />
<h3>Avoiding Known Issues</h3>
<p>It might seem that using shared objects could be the simplest when you are only storing a couple of variables and objects like statistics or even could be the more complex when saving a game progress/state together with the players preferences. The bigger the system, more likely, the more complex your variables/objects are (same for games), and this is where possible issues during saving/loading/handling the shared objects.<br />
<span id="more-470"></span></p>
<p>Some common things to remember when using shared objects with your games:</p>
<ol>
<li><a href="#tip1">Don&#8217;t use direct reference to objects and use the game&#8217;s data only not the actual game objects</a>.</li>
<li><a href="#tip2">Use a localPath param on the <code>getLocal()</code> method</a>.</li>
<li><a href="#tip3">Taking advantage of unique names for your shared object name</a>.</li>
<li><a href="#tip4">Prompt and request for shared object size limit increase when your game needs it</a>.</li>
</ol>
<blockquote><p>While I only have these that I can explain, you are free to share more if you have one or more. We can discuss and make the lives of other actionscript writers a little better.</p></blockquote>
<p><br id="tip1" /></p>
<hr />
<h3>Don&#8217;t use direct reference to objects and use the game&#8217;s data only not the actual game objects.</h3>
<p>Why do I say this? Basically it is fine to store various data types such as objects and arrays on SO (shared objects), same goes for strings, numbers (int/uint), booleans and bytearray (as3). If you store references to local game objects on your shared objects, one way or another, your in for a later problem.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> mySo:<span style="color: #0066CC;">SharedObject</span> = <span style="color: #0066CC;">SharedObject</span>.<span style="color: #0066CC;">getLocal</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;mySaveName&quot;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// world.gameObject contains further objects like settings, player, etc</span>
mySo.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">gamedata</span> = world.<span style="color: #006600;">gameObject</span>;
mySo.<span style="color: #0066CC;">flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// --</span></pre></div></div>

<p>When you do something like the above, instead of storing the needed values only, you are referencing the local game object (w/c also contains references to other local game objects) in which Flash defines this as a copy of the local object but is really just a reference. Any change through out the game values/data would <strong>update</strong> the local object (which the shared object references) and Flash Player would write that into the shared object file when <code>flush()</code> is called or as soon as the shared object session ends &#8211; that is then swf file is closed.</p>
<p>The worst case here is that when you clear, reset or even delete your local game objects, the shared object which references to it will be affected and updated (as session has ended).</p>
<p>Also, even if don&#8217;t experience the possibility of issues when you do the above there, a problem might still arise when you load the shared object in later time. This case might not be so common but is possible to experience if loading the shared object is sloppily made. You can have inconsistent object references where the object contained within loaded shared object is conflicting, a duplicate, or not referencing the correct object.</p>
<p>A better and safest way of using shared objects (something like):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mySo.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">gamedata</span> = <span style="color: #66cc66;">&#123;</span>
  player:<span style="color: #66cc66;">&#123;</span>
    score: world.<span style="color: #006600;">gameObject</span>.<span style="color: #006600;">player</span>.<span style="color: #006600;">score</span>,
    health: world.<span style="color: #006600;">gameObject</span>.<span style="color: #006600;">player</span>.<span style="color: #006600;">health</span>,
    currentLevel: <span style="color: #66cc66;">&lt;</span>code<span style="color: #66cc66;">&gt;</span>world.<span style="color: #006600;">gameObject</span>.<span style="color: #006600;">player</span>.<span style="color: #006600;">currentLevel</span>
  <span style="color: #66cc66;">&#125;</span>,
  settings:<span style="color: #66cc66;">&#123;</span>
    mute: world.<span style="color: #006600;">gameObject</span>.<span style="color: #006600;">settings</span>.<span style="color: #006600;">mute</span>,
    <span style="color: #0066CC;">quality</span>: world.<span style="color: #006600;">gameObject</span>.<span style="color: #006600;">settings</span>.<span style="color: #0066CC;">quality</span>,
    particles: world.<span style="color: #006600;">gameObject</span>.<span style="color: #006600;">settings</span>.<span style="color: #006600;">particles</span>,
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* well this is just a shortcut on containing them into objects,
 * but the point here is that score, health, currentLevel, etc
 * are all basic data types like number/int/uint, string, bool, etc&lt;/code&gt;</span></pre></div></div>

<p>Now you see that we store each valuable data only. We now transfer only the value of the variable not it&#8217;s reference. So while it&#8217;s fine to use objects and arrays on your shared objects, keeping be wary of how you do it, be careful.</p>
<p>You probably wouldn&#8217;t want to directly store instances of display objects, bitmapdata, functions, etc on share objects.<br />
<br id="tip2" /></p>
<hr />
<h3>Use a localPath param on the <code>getLocal()</code> method</h3>
<p>While passing the localPath parameter upon calling<code> getLocal()</code> is optional, although I would have to recommend using it by this time. In the case where you are building an independent system, prototypes, does not consider expansion/addons and will never be a part of a larger system, you can probably opt-out the localPath and never mind it.</p>
<p>So whats the issue of using and not using the localPath? By default, if you leave localPath by it&#8217;s default (null), it would use the the full path of the swf file (w/c includes the swf file itself) to store your shared objects.</p>
<blockquote class="white"><p>#SharedObjects/<span style="color: #808000;">&lt;random code&gt;</span>/<span style="color: #808000;">&lt;domain&gt;</span>/<span style="color: #808000;">&lt;path&gt;</span>/<span style="color: #808000;">&lt;swf filename&gt;</span>.swf/<span style="color: #808000;">&lt;object name&gt;</span>.sol</p></blockquote>
<blockquote>
<ul>
<li><em>Windows XP and Vista:</em>
<ul>
<li>For Web sites: %APPDATA%\Macromedia\Flash Player\#SharedObjects\&lt;random code&gt;\&lt;domain&gt;\&lt;path&gt;\&lt;object name&gt;.sol</li>
<li>For AIR Applications, %APPDATA%\&lt;AIR Application Reverse Domain Name&gt;\Local Store\#SharedObjects\&lt;flash filename&gt;.swf\&lt;object name&gt;.sol</li>
</ul>
</li>
</ul>
<ul>
<li><em>Mac OS X:</em>
<ul>
<li>For Web sites, ~/Library/Preferences/Macromedia/Flash Player/#SharedObjects/&lt;random code&gt;/&lt;domain&gt;/&lt;path from webserver&gt;/&lt;object name&gt;.sol</li>
<li>For AIR Applications, ~/Library/Preferences/&lt;AIR Application Name&gt;/Local Store/#SharedObjects/&lt;flash filename&gt;.swf/&lt;object name&gt;.sol</li>
</ul>
</li>
<li><em>Linux/Unix:</em>
<ul>
<li>~/.macromedia/Flash_Player/#SharedObjects/&lt;random id&gt;/&lt;domain&gt;/&lt;path&gt;/&lt;flash filename&gt;.swf/&lt;object name&gt;.sol</li>
</ul>
</li>
</ul>
<p style="padding-left: 30px;"><em>-taken from <a rel="external nofollow" href="http://en.wikipedia.org/wiki/Local_Shared_Object#File_locations" target="_blank">wiki</a></em></p>
</blockquote>
<p>For example, I have a swf  at <code>http://example.com/games/uploads/supergame.swf</code>, and when it creates a shared object (<code>SharedObject.getLocal("supergameData");</code>), the SO can be found at <code>#SharedObjects/</code><span style="color: #808000;">&lt;random code&gt;</span><code>/example.com/games/uploads/supergame.swf/supergameData.sol</code>.</p>
<p>While it works well and fine without the localPath parameter, there are certain sites/APIs/services that change the game&#8217;s file name by using their automated uploads, feeds, etc as the file name is used in the path. When the developer tries to upload a bug fixed build (v1.0.1), the swf might have another file name hence a problem appears where the previous build&#8217;s (v1.0.0) shared object won&#8217;t be accessible from the new build due to security reasons.</p>
<p>Although your shared object would be secured against any another swf so it can&#8217;t read it, by setting a localPath parameter like &#8220;/games&#8221; or simply &#8220;/&#8221;, the shared object can now be read by another swf file within the same domain. Using &#8220;/games&#8221; would make the SO accessible for any swf within the game directory, while having &#8220;/&#8221; would make it accessible from any swf within the domain.</p>
<p>Again from the same example but using <code>SharedObject.getLocal("supergameData","/");</code>, makes the SO at <code>#SharedObjects/</code><span style="color: #808000;">&lt;random code&gt;</span><code>/example.com/supergameData.sol</code>. This would make it not dependant on the swf&#8217;s file name.</p>
<blockquote><p><strong>Note</strong>: If you set a localPath in which the path doesn&#8217;t physically exists as a (sub) directory of the domain the shared object won&#8217;t be stored. Luckily with AS3 you&#8217;ll be prompted with an Error #2134 when the SharedObject can&#8217;t be created, but for AS2, you have to be well aware of this.</p></blockquote>
<p><br id="tip3" /></p>
<hr />
<h3>Taking advantage of unique names for your shared object name</h3>
<p>This is one is something that I have just learned through experimentation and will soon incorporate on my next projects. While setting the localPath parameter to &#8220;/&#8221; makes the SO accessible to other swfs, there is also a risk that your SO name might be the same with another swf. We wouldn&#8217;t want another swf to mess up or overwrite our SO file just because it both uses general and/or generic SO names like &#8220;<em>saveData</em>&#8221; or &#8220;<em>gameData</em>&#8220;.</p>
<p><strong>The ultimate tip</strong>: Since the SO names allow certain special characters like / (slash), we take advantage of that! Together with the localPath &#8220;/&#8221;, what I recommend is do a standard setup for your SO names something like:</p>
<blockquote class="white"><p><span style="color: #808000;">&lt;company/developer name&gt;</span>/<span style="color: #808000;">&lt;game name&gt;</span>/<span style="color: #808000;">&lt;data name&gt;</span></p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> mySo:<span style="color: #0066CC;">SharedObject</span> = <span style="color: #0066CC;">SharedObject</span>.<span style="color: #0066CC;">getLocal</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;jaycsantos/neolithic/preferences&quot;</span>, <span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// -- and/or something else like</span>
<span style="color: #000000; font-weight: bold;">var</span> mySo:<span style="color: #0066CC;">SharedObject</span> = <span style="color: #0066CC;">SharedObject</span>.<span style="color: #0066CC;">getLocal</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;jaycsantos/neolithic/gameData1&quot;</span>, <span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// --</span></pre></div></div>

<p>&#8230;will store your SO at something like: <code>#SharedObjects/</code><span style="color: #808000;">&lt;random code&gt;</span><code>/</code><span style="color: #808000;">&lt;domain&gt;</span><code>/#jaycsantos/neolithic/gameData.sol</code></p>
<blockquote><p>Although I still have to confirm this with Mac OS and Linux machines. Drop me a line if you find it working with those.</p></blockquote>
<p>Now ain&#8217;t that a little more organized than having all SO on the &#8220;/&#8221; of the domain!<br />
<br id="tip4" /></p>
<hr />
<h3>Prompt and request for shared object size limit increase when your game needs it</h3>
<div id="attachment_471" class="wp-caption aligncenter" style="width: 240px"><img class="size-full wp-image-471" title="Local Storage" src="http://jaycsantos.com/uploads/2009/04/localstorage.gif" alt="Local Storage" width="230" height="150" /><p class="wp-caption-text"> </p></div>
<p>There are cases when your shared object has a lot of data in it and it could exceed the default 100kb limit of a user. If this happens, it may cause your shared objects to be written but not read. I have read quite a few times where issues of reading/writing of share objects were resolved by prompting a request to increase local storage limit. This is where the <code>flush() </code>method takes it stand.</p>
<blockquote class="white"><p>Immediately writes a locally persistent  shared object to a local file.<br />
<span style="color: #888888;"><em>AS3</em></span><span style="color: #000080;"><strong><br />
flush</strong></span>(<code>minDiskSpace</code>:<span style="color: #000080;">int</span> = 0):<span style="color: #000080;">String</span></p>
<div style="padding-left: 30px;"><code>minDiskSpace</code><code>:</code><span style="color: #000080;">int</span> (default = <code>0</code>) —  The minimum disk space, in bytes, that must be allotted for this object.</div>
<p><span style="color: #888888;"><em>AS2</em></span><br />
<span style="color: #000080;"><strong><span class="signatureLink">flush</span></strong></span><code>([minDiskSpace:</code><span style="color: #000080;">Number</span><code>])</code>:Object</p>
<div style="padding-left: 30px;"><code>minDiskSpace:</code><span style="color: #000080;">Number</span> [optional] &#8211; An integer specifying  the number of bytes that must be allotted for this object. The default value is  0.</div>
</blockquote>
<p>With AS3, the <code>flush()</code> method basically does the same thing as with AS2 including their parameters, the only difference made was their return values. AS3 returns a string constant but AS2 returns an object; still their purpose is the same, only the data types have changed.</p>
<div class="wp_syntax">
<div><span style="color: #888888;"><em>AS3</em></span>:<br />
<strong>Returns</strong></p>
<div style="padding-left: 30px;"><span style="color: #000080;">String </span>— Either of the following values:</div>
<ul>
<li><code>SharedObjectFlushStatus.PENDING</code>: The user has permitted local information storage for objects from this domain, but the amount of space allotted is not sufficient to store the object. Flash Player prompts the user to allow more space. To allow space for the shared object to grow when it is saved, thus avoiding a SharedObjectFlushStatus.PENDING return value, pass a value for minDiskSpace.</li>
<li><code>SharedObjectFlushStatus.FLUSHED</code>: The shared object has been successfully written to a file on the local disk.</li>
</ul>
<p><em><span style="color: #888888;">AS2</span></em>:<br />
<strong>Returns</strong></p>
<div style="padding-left: 30px;"><span style="color: #000080;">Object </span>- A Boolean value: <code>true </code>or <code>false</code>; or a string value of &#8220;<code>pending</code>&#8220;, as described in the following list:</div>
<ul>
<li>If the user has permitted local information storage for objects from this domain, and the amount of space allotted is sufficient to store the object, this method returns true. (If you have passed a value for <code>minimumDiskSpace</code>, the amount of space allotted must be at least equal to that value for <code>true </code>to be returned).</li>
<li>If the user has permitted local information storage for objects from this domain, but the amount of space allotted is not sufficient to store the object, this method returns &#8220;<code>pending</code>&#8220;.</li>
<li>If the user has permanently denied local information storage for objects from this domain, or if Flash cannot save the object for any reason, this method returns <code>false</code>.</li>
</ul>
<div style="padding-left: 30px;"><em>- taken from livedocs (<a rel="external nofollow" href="http://livedocs.adobe.com/flash/9.0/main/00002121.html" target="_blank">as2</a> &amp; <a rel="external nofollow" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html#flush()" target="_blank">as3</a>)<br />
</em></div>
</div>
</div>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> mySo:<span style="color: #0066CC;">SharedObject</span> = <span style="color: #0066CC;">SharedObject</span>.<span style="color: #0066CC;">getLocal</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'mySaveName'</span>, localPath, secure  <span style="color: #66cc66;">&#41;</span>;
mySo.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">thingtoSave</span> = myVariable; <span style="color: #808080; font-style: italic;">// your variables</span>
<span style="color: #808080; font-style: italic;">/* stores data into disk and require 10kb of min space
 * if the allocated disk space for local storage is
 * less than 10bk which most probaly be 1kb, then it
 * would prompt a dialog box requesting for an increase
 */</span>
<span style="color: #000000; font-weight: bold;">var</span> fushResult:<span style="color: #0066CC;">Object</span> = mySo.<span style="color: #0066CC;">flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> fushResult <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>So saying simply, if you know your game&#8217;s shared object will be greater than a few kbs, you should consider using <code>flush(minDiskSpace)</code> with as your expect maximum size for your data. Why maximum? &#8211; think of it as your allowance. Now don&#8217;t even consider 2mb or more as an allowance if it&#8217;ll never even reach it, play your game at it&#8217;s fullest and see what is the size of the shared object using the <code>getSize()</code> method (as2) and the property <code>size</code> (as3) then add around 5-10% of it&#8217;s size for an allowance.</p>
<h3>Conclusion</h3>
<p>After all I have read about using SharedObject is as simple as it could be, in reality it can be a whole lot more than spinning circles for all of what you would need. Well yes, storing a string and recovering it is a piece of icing but storing a complete game state/progress is a new kind of cake to bake. Basically what I have written here are tips and suggestions that I have experienced and with what I have experimented. Either it be accurate or I know not, using SharedObjects has its own work around and is not as simple as most people have said like building a chair, while yes it is not the most complicated but it could be a big question mark on the head for someone new or not familiar to its purpose. I might even consider making a SaveManager or the like but that would be when I get to it. Well, that&#8217;s it, this one is quite long but I know it&#8217;ll help!</p>
<h3>Other Keywords</h3>
<ul>
<li>Working with Flash Actionscript Shared objects.</li>
<li>Issues and techniques with Flash Actionscript shared objects.</li>
<li>Saving data with action script using shared objects.</li>
<li>Actionscript tips and tricks, shared objects.</li>
<li>Shared object help, actionscript 2 &amp; actionscript 3.</li>
<li>Flash actionscript saving &amp; loading locally.</li>
<li>Flash Actionscript proper way of use when Shared Object does not work.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/the-trick-to-using-sharedobject/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Things to Initialize on your AS2 Game/Project</title>
		<link>http://jaycsantos.com/flash/things-to-initialize-on-your-as2-gameproject/</link>
		<comments>http://jaycsantos.com/flash/things-to-initialize-on-your-as2-gameproject/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 06:43:45 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[code bits]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=466</guid>
		<description><![CDATA[While transitioning to AS3 is a massive must as people have regarded, some developers (mostly new) just want to or is forced to get stuck with AS2. So if you&#8217;re working on a AS2 project or lower, here some common things you should consider checking at the start of your project. Some common but usually [...]]]></description>
			<content:encoded><![CDATA[<p>While transitioning to AS3 is a massive must as people have regarded, some developers (mostly new) just want to or is forced to get stuck with AS2. So if you&#8217;re working on a AS2 project or lower, here some common things you should consider checking at the start of your project.</p>
<p>Some common but usually neglected things you might not want but your game might have:</p>
<ul>
<li>The everlasting suggestion of &#8220;avoid directly referencing <code>_root</code>&#8220;.</li>
<li>Yellow boxes appear (on buttons/movieclips) when pressing <em>Tab</em>.</li>
<li>Cheating by pressing <em>Space</em> when the yellow boxes focus on buttons/movieclips.</li>
<li>Cheating by right clicking (shows the context menu, a cheat for some game genres).</li>
<li>Cheating/altering game play/screens by pressing forward, back, etc from the context menu.</li>
</ul>
<h3>_root or no _root</h3>
<div id="attachment_467" class="wp-caption alignleft" style="width: 210px"><img class="size-full wp-image-467" title="root" src="http://jaycsantos.com/uploads/2009/04/root.jpg" alt="_root?" width="200" height="180" /><p class="wp-caption-text">_root ?</p></div>
<p>As you might have read a dozen or countless times, it is highly recommended to not directly call <code>_root</code> within the project, although using <code>_root</code> is not necessarily wrong. These developers come to use <code>_root</code> as a hack way of referencing an absolute path to their objects. If you are building an independent system, prototypes, does not consider expansion/addons and will never be a part of a larger system then it would be fine to use <code>_root</code> as a hacky way. But if your work is part of a larger system or the like consider having to add in various (most of the time, 3rd party) extensions, plugins, services, APIs (application programming interfaces), SDKs (software development kits), or anything similar in it&#8217;s purpose then you&#8217;ll be in for some serious headaches. Most notable of these are those that wrap your game within another swf file. You might be able to face serious issues and problems varying from sounds not playing/missing, disposition world objects, localtoGlobal/globaltoLocal miscalculations, undefined object property, sprites (movieclips) not following the expected position and behavior, wrongly positioned custom cursors and a lot more.<span id="more-466"></span></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// somewhere in the timeline...</span>
<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mySuperFunction</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// doing some super things</span>
  <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'my super function worked!'</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// somewhere else in the game...</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">_root</span>.<span style="color: #006600;">mySuperFunction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// before</span>
  <span style="color: #808080; font-style: italic;">// outputs: my super  function worked!</span>
<span style="color: #808080; font-style: italic;">// after some integration and browser testing...</span>
  <span style="color: #808080; font-style: italic;">// outputs:         (its really has no output)</span>
<span style="color: #808080; font-style: italic;">// nooooo!!! it isn't working now! but it used to work before,</span>
<span style="color: #808080; font-style: italic;">// maybe this thing wrecks my game when I use it!? I didn't do anything else.</span></pre></div></div>

<p>Although I won&#8217;t be discussing fixes for the issues mentioned above, we can start by doing what is recommended, to not directly reference from the <code>_root</code>. The most starter&#8217;s way from first look at the documentations and other sources, is using <code>_lockroot</code>. Basically it could prove to be a good way to fix and keep things the same but for some cases it might not be enough. For more assurance and better practice I would recommend referencing <code>_root</code> from a global variable or static variable from a class (most recommended).</p>
<p><em>Why do I have to use a variable?</em> So what we reference <strong>can be dynamic</strong> &#8211; there are some cases where the <code>_root</code> is not technically the parent most, where it can be a specific game container or something similar, this way you don&#8217;t have to shift through using <code>_root</code> and updating all your codes when needed. And the sole reason is for code reusability.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// probably on frame1 before your preloader code</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_lockroot</span> = <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #808080; font-style: italic;">// where this = root actually</span>
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mySuperFunction</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// doing some super things</span>
  <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'my super function worked!'</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// for those who do timeline coding or the like</span>
<span style="color: #000000; font-weight: bold;">_global</span>.<span style="color: #006600;">gameroot</span> = <span style="color: #0066CC;">this</span>; <span style="color: #808080; font-style: italic;">// where this = root actually</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// somewhere else through the game...</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">_global</span>.<span style="color: #006600;">gameroot</span>.<span style="color: #006600;">mySuperFunction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// output: my super function worked!</span>
<span style="color: #808080; font-style: italic;">// hoorah! its working well!</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// and for those who use classes (highly recommended),</span>
<span style="color: #808080; font-style: italic;">// simply pass it on your main class during instantiation</span>
<span style="color: #808080; font-style: italic;">// to be referenced on a static variable</span>
<span style="color: #000000; font-weight: bold;">var</span> maingame = <span style="color: #000000; font-weight: bold;">new</span> MainGame<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// where this = root actually</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// on your MainGame class or the like</span>
<span style="color: #000000; font-weight: bold;">class</span> MainGame<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> GameRoot:<span style="color: #0066CC;">MovieClip</span>;
  <span style="color: #808080; font-style: italic;">// constructor</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainGame<span style="color: #66cc66;">&#40;</span> root:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    GameRoot = root; <span style="color: #808080; font-style: italic;">// stores the root mc in static var</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// somewhere else through the game...</span>
<span style="color: #808080; font-style: italic;">// now simply reference from static variable MainGame.GameRoot instead of _root</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> MainGame.<span style="color: #006600;">GameRoot</span>.<span style="color: #006600;">mySuperFunction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;  <span style="color: #808080; font-style: italic;">// output: my super function worked!</span></pre></div></div>

<h3>Magical Weirdness of Yellow Boxes</h3>
<div id="attachment_469" class="wp-caption alignright" style="width: 204px"><img class="size-full wp-image-469" title="Yellow Box" src="http://jaycsantos.com/uploads/2009/04/yellowbox.jpg" alt="Yellow Box" width="194" height="117" /><p class="wp-caption-text">Yellow Box</p></div>
<p>Some games that incorporate interactive buttons (or movieclips that acts as buttons) are not wary of this feature. Basically flash has the ability to take use of tabbing on GUI, but since we are game developers there are high chances we would not want this as it might look ugly. While most games might not be affected by this feature there are certain circumstances that the game play can be cheated one way or another. Good examples are those games that use mouse interactivity using mouse over and mouse click events, where as the player can cheat things by combining tabbing, pressing space (to select) and the use of mouse. When a yellow box appears over a button its <code>rollOver</code> event is triggered, and when space is pressed its <code>onPress</code> event is, and that is where some cheating may occur on some combinations and timing. It is fairly a simple piece of code you have to declare at the beginning to disable all of the magical yellow boxes.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// probably on frame1 before your preloader code</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">tabEnabled</span> = <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #808080; font-style: italic;">// disable tabs for the whole movieclip</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">tabChildren</span> = <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #808080; font-style: italic;">// disables tabs for the rest movieclip's childrens</span></pre></div></div>

<h3>Right clicks and Context Menus</h3>
<div id="attachment_468" class="wp-caption alignright" style="width: 226px"><img class="size-full wp-image-468" title="Context Menu" src="http://jaycsantos.com/uploads/2009/04/contextmenu.jpg" alt="Which frame to move?" width="216" height="152" /><p class="wp-caption-text">Which frame to move?</p></div>
<p>While the context menus from right clicks (control-click for Macs) on flash can&#8217;t be removed, we can modify the menus and also strip down unnecessary movie controls like forward, back, play, etc. If your game/project is AS2 powered and is not really well structured (timeline coding and uses <code>gotoAndStop()</code> methods ) then you might encounter glitches and frame skips once the player right clicks and select any movie controls. They could end up skipping levels to scoring indefinitely and would make things less fun and more cheating for them.</p>
<p>If all you want is to simplify the menu:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// probably on frame1 before your preloader code</span>
<span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">showMenu</span> = <span style="color: #000000; font-weight: bold;">false</span>;</pre></div></div>

<p>But if you also would like to customize and add your own menus then:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// probably on frame1 before your preloader code</span>
<span style="color: #808080; font-style: italic;">// instantiate a new contextmenu</span>
<span style="color: #000000; font-weight: bold;">var</span> my_cm:<span style="color: #0066CC;">ContextMenu</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">ContextMenu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
my_cm.<span style="color: #0066CC;">hideBuiltInItems</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// hides the built in items, duh</span>
my_cm.<span style="color: #0066CC;">customItems</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">ContextMenuItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;(c) Jayc Santos&quot;</span>, gotoJayc<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// creates a new menu</span>
<span style="color: #000000; font-weight: bold;">function</span> gotoJayc<span style="color: #66cc66;">&#40;</span>obj, item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">getURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://jaycsantos.com/&quot;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">menu</span> = my_cm; <span style="color: #808080; font-style: italic;">// overwrites the default menu and where this = _root (most of the time)</span></pre></div></div>

<p>There are also some cases where simply right clicking would create issues for the game play and/or cheat things. A good way to determine is to use the <code>onSelect</code> event. You can also use the <code>onSelect</code> event for more customizations of your context menus (<a title="onSelect (ContextMenu.onSelect handler)" rel="none" href="http://livedocs.adobe.com/flash/9.0/main/00001554.html" target="_blank">see livedocs sample</a>).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> my_cm:<span style="color: #0066CC;">ContextMenu</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">ContextMenu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">function</span> menuHandler<span style="color: #66cc66;">&#40;</span>obj:<span style="color: #0066CC;">Object</span>, <span style="color: #0066CC;">menu</span>:<span style="color: #0066CC;">ContextMenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;right click context menu was invoked&quot;</span> <span style="color: #66cc66;">&#41;</span>;
  <span style="color: #808080; font-style: italic;">// do awesome things</span>
<span style="color: #66cc66;">&#125;</span>
my_cm.<span style="color: #0066CC;">onSelect</span> = menuHandler;
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">menu</span> = my_cm;</pre></div></div>

<h3>Conclusion</h3>
<p>So overall you might probably want to have something like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// probably on frame1 before your preloader code</span>
<span style="color: #0066CC;">Stage</span>.<span style="color: #0066CC;">showMenu</span> = <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #808080; font-style: italic;">// hides the built in items on context menu</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_lockroot</span> = <span style="color: #000000; font-weight: bold;">true</span>; <span style="color: #808080; font-style: italic;">// locks the root, duh</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">tabEnabled</span> = <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #808080; font-style: italic;">// disables tabs</span>
<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">tabChildren</span> = <span style="color: #000000; font-weight: bold;">false</span>; <span style="color: #808080; font-style: italic;">// disables tabs for it's children</span></pre></div></div>

<p>for timeline coders</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">_global</span>.<span style="color: #006600;">gameroot</span> = <span style="color: #0066CC;">this</span>; <span style="color: #808080; font-style: italic;">// where this = root actually</span>
<span style="color: #808080; font-style: italic;">//  when &quot;this&quot; is traced it ouputs &quot;_level0&quot; most of the time unless altered</span></pre></div></div>

<p>and for those who use a little more OOP approach</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> maingame = <span style="color: #000000; font-weight: bold;">new</span> MainGame<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span> <span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// where this = root actually</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// on your MainGame class or the like</span>
<span style="color: #000000; font-weight: bold;">class</span> MainGame<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> GameRoot:<span style="color: #0066CC;">MovieClip</span>;
  <span style="color: #808080; font-style: italic;">// constructor</span>
  <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainGame<span style="color: #66cc66;">&#40;</span> root:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    GameRoot = root; <span style="color: #808080; font-style: italic;">// stores the root mc in static var</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, your actionscript 2 coding life will be a little better with these in mind.</p>
<hr />
<h3>Other Keywords</h3>
<ul>
<li>Starting flash with actionscript 2 (AS2).</li>
<li>Frame 1 of actionscript 2 project/game.</li>
<li>Preloader code actionscript 2 (AS2).</li>
<li>Why to avoid or why not use _root/root with actionscript.</li>
<li>Getting rid of yellow boxes when tabbed.</li>
<li>Flash actionscript tabs.</li>
<li>Flash actionscript context menus.</li>
<li>Flash actionscript important points especially for games.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/things-to-initialize-on-your-as2-gameproject/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cheating AI from Hardcourt &#8211; Code the Plans</title>
		<link>http://jaycsantos.com/flash/cheating-ai-from-hardcourt-code-the-plans/</link>
		<comments>http://jaycsantos.com/flash/cheating-ai-from-hardcourt-code-the-plans/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 14:09:06 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Hardcourt]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=384</guid>
		<description><![CDATA[So I have introduced how to plan out simple AI behavior from my previous post &#8211; Cheating AI from Hardcourt &#8211; Plan it out &#8211; if you haven&#8217;t, read that one first before this. Our next step would be writing the data charts into codes. It might look quite easy when you see the simple [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_465" class="wp-caption alignleft" style="width: 260px"><img class="size-full wp-image-465" title="Plan your behavior" src="http://jaycsantos.com/uploads/2009/04/screen3.jpg" alt="Behavior" width="250" height="130" /><p class="wp-caption-text">Plan your behavior</p></div>
<p>So I have introduced how to plan out simple AI behavior from my previous post &#8211;  <a href="http://jaycsantos.com/games/cheating-ai-from-hardcourt-plan-it-out">Cheating AI from Hardcourt &#8211; Plan it out</a> &#8211; if you haven&#8217;t, read that one first before this.</p>
<p>Our next step would be writing the data charts into codes. It might look quite easy when you see the simple flow charts but remember that the AI must process this through out the game with a character&#8217;s lifespan.</p>
<p>What I&#8217;m about to share is simply a psuedo codish (partly actionscript syntax) of the AI&#8217;s logic. Eventually you can port it into which ever approach you are comfortable with, like OOP or the old school timeline.</p>
<p>Remember that this is my first published game where as I still used AS2 using prototypes and functions but I&#8217;ll discuss this in a little of close objective approach. First of we prepare a few world declarations &amp; preparation in a quick manner.</p>
<p><span id="more-384"></span></p>
<h3>Prepare the world</h3>
<p>In the game world object or whichever the game&#8217;s entities reside, we&#8217;ll be having a <code>calculateDistance</code> method that pre-calculates distances of each of the characters then stores each of their enemies distance on an <code>enemyDist</code> container object.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// from the game world</span>
 <span style="color: #009900; font-style: italic;">// called within the main loop</span>
<span style="color: #339966; font-weight: bold;">function</span> calculateDistances<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900; font-style: italic;">// pre-calculate distances of:</span>
  <span style="color: #009900; font-style: italic;">//   ball to char - store distance to the character's ballDist variable</span>
  <span style="color: #009900; font-style: italic;">//   char to char - store all other character's (enemy's) distance to character's enemyDist container variable</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The code below acts as base methods for all characters. This is primarily an advantage from inheritance whereas properties and methods of the base class is inherited by the class that extends it. Basically all the characters will be capable of methods such as moving (<code>moveChar</code>), getting the ball (<code>getBall</code>), shooting it (<code>shoot</code>) and punching their enemies (<code>punch</code>).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">  <span style="color: #009900; font-style: italic;">// used to store the ball object also do determine if the we have the ball; set to null if we not</span>
<span style="color: #6699cc; font-weight: bold;">var</span> myBall<span style="color: #000066; font-weight: bold;">:</span>Ball<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #009900; font-style: italic;">// the distance of the ball from character in Vector2D (x, y)</span>
<span style="color: #6699cc; font-weight: bold;">var</span> ballDist<span style="color: #000066; font-weight: bold;">:</span>Vector2D<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #009900; font-style: italic;">// contains enemies distances</span>
<span style="color: #6699cc; font-weight: bold;">var</span> enemyDist<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #009900; font-style: italic;">// assuming that 25 is the range where the character can punch it's enemies</span>
<span style="color: #6699cc; font-weight: bold;">var</span> punchRange<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">25</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
 <span style="color: #009900; font-style: italic;">// moves the character</span>
<span style="color: #339966; font-weight: bold;">function</span> moveChar<span style="color: #000000;">&#40;</span> xdir<span style="color: #000066; font-weight: bold;">,</span> ydir <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900; font-style: italic;">//  xdir - move left (-1) or right (1)</span>
  <span style="color: #009900; font-style: italic;">//  ydir - move up (-1) or down (1)</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #009900; font-style: italic;">// determines if we have the ball else if the ball is close &amp;amp; not owned (by enemy) then get it</span>
<span style="color: #339966; font-weight: bold;">function</span> getBall<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900; font-style: italic;">//  use ballDist for the calculated distance;</span>
  <span style="color: #009900; font-style: italic;">//  returns: ball object - if our char already has it</span>
  <span style="color: #009900; font-style: italic;">// 	      null - if the ball is nowhere near</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #009900; font-style: italic;">// shoots the ball</span>
<span style="color: #339966; font-weight: bold;">function</span> shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900; font-style: italic;">// release the ball towards the ring</span>
  <span style="color: #009900; font-style: italic;">// set myBall to null</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #009900; font-style: italic;">// attacks the enemy</span>
 <span style="color: #009900; font-style: italic;">//  params: enemy:Object - the enemy instance to punch</span>
<span style="color: #339966; font-weight: bold;">function</span> punch<span style="color: #000000;">&#40;</span> enemy<span style="color: #000066; font-weight: bold;">:</span>BaseChar <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900; font-style: italic;">//  returns: true - if successful hit</span>
  <span style="color: #009900; font-style: italic;">//           false - else not</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note: for the <a title="More info about 2D Vector" href="http://google.com/search?q=actionscript+vector+2d" target="_blank">Vector2D</a>, some might be wondering what it is, well it is a class whose purpose is to calculate Vector operations in two dimensions.</p>
<p>Now we have prepared the initial methods, let&#8217;s go with the proposed coding of the flow charts.</p>
<h3>Basic Behaviors</h3>
<p>The AI controlled character behavior:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">  <span style="color: #009900; font-style: italic;">// runs with the main loop for each AI character</span>
<span style="color: #339966; font-weight: bold;">function</span> thinkAI<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> xdir = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> ydir = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// if we have the ball</span>
  <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> getBall<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900; font-style: italic;">// then shoot it!!</span>
    shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #009900; font-style: italic;">// if not</span>
  <span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900; font-style: italic;">// get the direction of the ball</span>
    xdir = ballDist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">?</span> ballDist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> ballDist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
    ydir = ballDist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">?</span> ballDist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> ballDist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #009900; font-style: italic;">// and move towards it</span>
    moveChar<span style="color: #000000;">&#40;</span> xdir<span style="color: #000066; font-weight: bold;">,</span> ydir <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<div style="overflow:hidden">
<div id="attachment_380" class="wp-caption alignleft" style="width: 160px"><a href="http://jaycsantos.com/uploads/2009/03/ai_chart0.gif"  onclick="return false"><img id="img_384_ai_chart0_gif" class="size-thumbnail wp-image-380" title="Basic Chart - Get &amp;  shoot " src="http://jaycsantos.com/uploads/2009/03/ai_chart0-150x118.gif" alt="Basic Chart - Get &amp; shoot" width="150" height="118"onclick="enlarge(this)" longdesc="http://jaycsantos.com/uploads/2009/03/ai_chart0.gif" /></a><p class="wp-caption-text">Basic Chart - Get &amp; shoot</p></div>
<p>We used the method <code>getBall()</code> to determine if we have the ball or not, if so shoot it. Else, we get the distance from <code>ballDist</code> (which contains the distance in x &amp; y values), have it&#8217;s polarity from it&#8217;s the absolute value and move towards it.</p>
<p>This is only the first chart (Basic Chart), our AI only considers that in the game world there is only the character himself and the ball, so whenever he gets the ball he shoots it immediately and repeats the process.</p></div>
<h3>Expanding for more Interactions</h3>
<p>What we do next is implement the second chart (Interactive Chart) and consider situations with the enemy and interact with it.</p>
<p>So improving the thinkAI() method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> thinkAI<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> xdir = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> ydir = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// if we have the ball</span>
  <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> getBall<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900; font-style: italic;">// then shoot it!!</span>
    shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #009900; font-style: italic;">// if not,</span>
  <span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> ball = GameWorld<span style="color: #000066; font-weight: bold;">.</span>ball<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000066; font-weight: bold;">:</span>Vector2D<span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #009900; font-style: italic;">// check if another char has the ball, since definitely it isn't this char</span>
    <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// store the enemy who has the ball in a local var</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> targetEnemy = enemyDist<span style="color: #000000;">&#91;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner<span style="color: #000066; font-weight: bold;">.</span>id <span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #009900; font-style: italic;">// this is the distance of the enemy char with respect to this char</span>
      dist = targetEnemy<span style="color: #000066; font-weight: bold;">.</span>dist<span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #009900; font-style: italic;">// if this enemy is within punching range</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> punchRange <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        punch<span style="color: #000000;">&#40;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
          <span style="color: #009900; font-style: italic;">// no need to do the processes below...</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: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
      dist = ballDist<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
      <span style="color: #009900; font-style: italic;">// get the direction</span>
    xdir = dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">?</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
    ydir = dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">?</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #009900; font-style: italic;">// and move towards it</span>
    moveChar<span style="color: #000000;">&#40;</span> xdir<span style="color: #000066; font-weight: bold;">,</span> ydir <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<div style="overflow:hidden">
<div id="attachment_381" class="wp-caption alignright" style="width: 160px"><a href="http://jaycsantos.com/uploads/2009/03/ai_chart1.gif"  onclick="return false"><img id="img_384_ai_chart1_gif" class="size-thumbnail wp-image-381" title="Interactive Chart - Steal the ball" src="http://jaycsantos.com/uploads/2009/03/ai_chart1-150x105.gif" alt="Interactive Chart - Steal the ball" width="150" height="105"onclick="enlarge(this)" longdesc="http://jaycsantos.com/uploads/2009/03/ai_chart1.gif" /></a><p class="wp-caption-text">Interactive Chart - Steal the ball</p></div>
<p>After determining that the ball is not in our character&#8217;s possession, we check if the ball is owned by another character. As it is owned, it certainly is not our character who has it as we already tested. So from the <code>enemyDist</code> container object (this has the pre-calculated distances of the character&#8217;s enemies) we get the enemies distance. See if the enemy is within stealing (ok, punching) range and punch if so, else move towards the enemy.</p>
<p>Now we have considered enemies too, if they have possession of the ball, our AI char simply goes after him and if he is within range we steal/punch.</p>
<blockquote><p>Take note that when a character is punched he losses the ball so I also call punching stealing the ball. <img src='http://jaycsantos.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p></blockquote>
</div>
<p>Then we take this into the 2nd chart.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> thinkAI<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> xdir = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> ydir = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// if we have the ball</span>
  <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> getBall<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #009900; font-style: italic;">// then shoot it!!</span>
    shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #009900; font-style: italic;">// if not,</span>
  <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> ball = GameWorld<span style="color: #000066; font-weight: bold;">.</span>ball<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000066; font-weight: bold;">:</span>Vector2D<span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #009900; font-style: italic;">// check if another char has the ball, since definitely it isn't this char</span>
    <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// store the enemy who has the ball in a local var</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> targetEnemy = enemyDist<span style="color: #000000;">&#91;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner<span style="color: #000066; font-weight: bold;">.</span>id <span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #009900; font-style: italic;">// targetEnemy.dist is the distance of the enemy char with respect to this char</span>
      dist = targetEnemy<span style="color: #000066; font-weight: bold;">.</span>dist<span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #009900; font-style: italic;">// if this enemy is within punching range</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> punchRange <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        punch<span style="color: #000000;">&#40;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
          <span style="color: #009900; font-style: italic;">// no need to do the processes below...</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: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// default the distance from the ball</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> dist = ballDist<span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> ds<span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #009900; font-style: italic;">// loop through enemies</span>
      <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #004993;">c</span> <span style="color: #0033ff; font-weight: bold;">in</span> enemyDist <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        ds = enemyDist<span style="color: #000000;">&#91;</span> <span style="color: #004993;">c</span> <span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>dist<span style="color: #000066; font-weight: bold;">;</span>
          <span style="color: #009900; font-style: italic;">// see if they are closer</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> ds<span style="color: #000066; font-weight: bold;">.</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> dist<span style="color: #000066; font-weight: bold;">.</span>len <span style="color: #000000;">&#41;</span>
          dist = ds<span style="color: #000066; font-weight: bold;">;</span>
          <span style="color: #009900; font-style: italic;">// it seems that this enemy is already in range, no need to check the others</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> ds<span style="color: #000066; font-weight: bold;">.</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> punchRange <span style="color: #000000;">&#41;</span>
          <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #000000;">&#125;</span>
        <span style="color: #009900; font-style: italic;">// if this is not the ball distance then it could only be the enemy</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist <span style="color: #000066; font-weight: bold;">!</span>= ballDist <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #009900; font-style: italic;">// if this enemy is within punching range</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> punchRange <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #009900; font-style: italic;">// attack!!</span>
          punch<span style="color: #000000;">&#40;</span> ball<span style="color: #000066; font-weight: bold;">.</span>owner <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #009900; font-style: italic;">// no need to do the processes below...</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: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
      <span style="color: #009900; font-style: italic;">// get the direction</span>
    xdir = dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">?</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
    ydir = dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">?</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
      <span style="color: #009900; font-style: italic;">// and move towards it</span>
    moveChar<span style="color: #000000;">&#40;</span> xdir<span style="color: #000066; font-weight: bold;">,</span> ydir <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<div style="overflow:hidden">
<div id="attachment_382" class="wp-caption alignleft" style="width: 160px"><a href="http://jaycsantos.com/uploads/2009/03/ai_chart2.gif"  onclick="return false"><img id="img_384_ai_chart2_gif" class="size-thumbnail wp-image-382" title="Interactive Chart 2 - Go offensive, attack!" src="http://jaycsantos.com/uploads/2009/03/ai_chart2-150x95.gif" alt="Interactive Chart 2 - Go offensive, attack!" width="150" height="95"onclick="enlarge(this)" longdesc="http://jaycsantos.com/uploads/2009/03/ai_chart2.gif" /></a><p class="wp-caption-text">Interactive Chart 2 - Go offensive, attack!</p></div>
<p>What I added here is for our AI character to consider not only the ball itself and it&#8217;s possible owner, but also nearby enemies. Checking through all enemies and determining if they are close enough or within range, therefore stopping them with a punch.</p>
<p>You can also see clearly the advantage of pre-calculating the distances, as  I used the enemy and ball distances enough to avoid recalculating them again and again when needed.</p></div>
<p>Now we have our competitive AI ready! Or not. Feeling like something is missing? Yes, its &#8220;<em>difficulty</em>!&#8221; Basically as you might have suspected that this AI behavior has the perfect reflex (well not so perfect really).  It does what it has to depending on exact situations at the exact time on the next loop.</p>
<blockquote><p><strong>Example this:</strong></p>
<p>Just at the same moment you grab the ball the enemy would instantly steal the ball or, simply saying, you can&#8217;t even run away, your character&#8217;s very change in position and angle is updated and read by the AI at the same time or yet exactly at the next frame therefore it knows where exactly you are same way the moment you move anywhere.</p>
<p>And another problem with the perfect reflex is same way we planned it, the moment the AI character grabs the ball he just shoots it at once.</p></blockquote>
<p>For this problem, a good fix I did was based around with numbers. Yes, &#8220;numbers&#8221; will help with everything, you can imagine setting a level of intelligence for the AI and how much well it executes it&#8217;s task depends on the numbers assigned. But I&#8217;ll leave that to another post to discuss about it more&#8230;</p>
<p>What do you think? <a href="../games/hardcourt/?play">Play hardcourt now!</a> to experience the AI in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/cheating-ai-from-hardcourt-code-the-plans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

