<?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>Tue, 01 Mar 2011 11:27:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>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[as3]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[Games]]></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[as2]]></category>
		<category><![CDATA[as3]]></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>23</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[as2]]></category>
		<category><![CDATA[cheat]]></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;">// from the game world</span>
 <span style="color: #009900;">// 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;">// pre-calculate distances of:</span>
  <span style="color: #009900;">//   ball to char - store distance to the character's ballDist variable</span>
  <span style="color: #009900;">//   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;">// 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: #000000; font-weight: bold;">:</span>Ball;
  <span style="color: #009900;">// 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: #000000; font-weight: bold;">:</span>Vector2D;
  <span style="color: #009900;">// contains enemies distances</span>
<span style="color: #6699cc; font-weight: bold;">var</span> enemyDist<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
  <span style="color: #009900;">// 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: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">25</span>;
&nbsp;
 <span style="color: #009900;">// moves the character</span>
<span style="color: #339966; font-weight: bold;">function</span> moveChar<span style="color: #000000;">&#40;</span> xdir, ydir <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900;">//  xdir - move left (-1) or right (1)</span>
  <span style="color: #009900;">//  ydir - move up (-1) or down (1)</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #009900;">// 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: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900;">//  use ballDist for the calculated distance;</span>
  <span style="color: #009900;">//  returns: ball object - if our char already has it</span>
  <span style="color: #009900;">// 	      null - if the ball is nowhere near</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #009900;">// 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;">// release the ball towards the ring</span>
  <span style="color: #009900;">// set myBall to null</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #009900;">// attacks the enemy</span>
 <span style="color: #009900;">//  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: #000000; font-weight: bold;">:</span>BaseChar <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900;">//  returns: true - if successful hit</span>
  <span style="color: #009900;">//           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;">// 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: #6699cc; font-weight: bold;">var</span> ydir = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
    <span style="color: #009900;">// 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;">// then shoot it!!</span>
    shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #009900;">// 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;">// get the direction of the ball</span>
    xdir = ballDist.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">?</span> ballDist.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> ballDist.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span>;
    ydir = ballDist.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">?</span> ballDist.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> ballDist.<span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span>;
      <span style="color: #009900;">// and move towards it</span>
    moveChar<span style="color: #000000;">&#40;</span> xdir, ydir <span style="color: #000000;">&#41;</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: #6699cc; font-weight: bold;">var</span> ydir = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
    <span style="color: #009900;">// 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;">// then shoot it!!</span>
    shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #009900;">// 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.ball;
    <span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000000; font-weight: bold;">:</span>Vector2D;
      <span style="color: #009900;">// 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.owner <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #009900;">// 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.owner.id <span style="color: #000000;">&#93;</span>;
        <span style="color: #009900;">// this is the distance of the enemy char with respect to this char</span>
      dist = targetEnemy.dist;
        <span style="color: #009900;">// if this enemy is within punching range</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist.len <span style="color: #000000; 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.owner <span style="color: #000000;">&#41;</span>;
          <span style="color: #009900;">// no need to do the processes below...</span>
        <span style="color: #0033ff; font-weight: bold;">return</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: #000000;">&#125;</span>
      <span style="color: #009900;">// get the direction</span>
    xdir = dist.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">?</span> dist.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span>;
    ydir = dist.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">?</span> dist.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist.<span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span>;
      <span style="color: #009900;">// and move towards it</span>
    moveChar<span style="color: #000000;">&#40;</span> xdir, ydir <span style="color: #000000;">&#41;</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: #6699cc; font-weight: bold;">var</span> ydir = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
    <span style="color: #009900;">// 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;">// then shoot it!!</span>
    shoot<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #009900;">// 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.ball;
    <span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000000; font-weight: bold;">:</span>Vector2D;
      <span style="color: #009900;">// 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.owner <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #009900;">// 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.owner.id <span style="color: #000000;">&#93;</span>;
        <span style="color: #009900;">// targetEnemy.dist is the distance of the enemy char with respect to this char</span>
      dist = targetEnemy.dist;
        <span style="color: #009900;">// if this enemy is within punching range</span>
      <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist.len <span style="color: #000000; 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.owner <span style="color: #000000;">&#41;</span>;
          <span style="color: #009900;">// no need to do the processes below...</span>
        <span style="color: #0033ff; font-weight: bold;">return</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;">// default the distance from the ball</span>
      <span style="color: #6699cc; font-weight: bold;">var</span> dist = ballDist;
      <span style="color: #6699cc; font-weight: bold;">var</span> ds;
        <span style="color: #009900;">// 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>.dist;
          <span style="color: #009900;">// see if they are closer</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> ds.len <span style="color: #000000; font-weight: bold;">&lt;</span> dist.len <span style="color: #000000;">&#41;</span>
          dist = ds;
          <span style="color: #009900;">// 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.len <span style="color: #000000; font-weight: bold;">&lt;</span> punchRange <span style="color: #000000;">&#41;</span>
          <span style="color: #0033ff; font-weight: bold;">break</span>;
      <span style="color: #000000;">&#125;</span>
        <span style="color: #009900;">// 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: #000000; font-weight: bold;">!</span>= ballDist <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #009900;">// if this enemy is within punching range</span>
        <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dist.len <span style="color: #000000; font-weight: bold;">&lt;</span> punchRange <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #009900;">// attack!!</span>
          punch<span style="color: #000000;">&#40;</span> ball.owner <span style="color: #000000;">&#41;</span>;
            <span style="color: #009900;">// no need to do the processes below...</span>
          <span style="color: #0033ff; font-weight: bold;">return</span>;
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
      <span style="color: #009900;">// get the direction</span>
    xdir = dist.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">?</span> dist.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span>;
    ydir = dist.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">?</span> dist.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span> dist.<span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span>;
      <span style="color: #009900;">// and move towards it</span>
    moveChar<span style="color: #000000;">&#40;</span> xdir, ydir <span style="color: #000000;">&#41;</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>
		<item>
		<title>V2 Redesign</title>
		<link>http://jaycsantos.com/news/v2-redesign/</link>
		<comments>http://jaycsantos.com/news/v2-redesign/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 03:45:52 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Arts n Visuals]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Web Dev]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[website]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=392</guid>
		<description><![CDATA[I&#8217;ve had my current theme for more than half a year, and I&#8217;m currently working on the next version during leisure time. &#8220;But the current one is good, simple and plain&#8221; &#8211; Yes it is, but it lacks things that I didn&#8217;t knew yet when I first made this. Actually v1 is my first full [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_394" class="wp-caption alignleft" style="width: 210px"><img class="size-full wp-image-394" title="JaycSantos.com v2 logo" src="http://jaycsantos.com/uploads/2009/03/designv2logo.jpg" alt="v2 logo" width="200" height="95" /><p class="wp-caption-text">v2 logo</p></div>
<p>I&#8217;ve had my current theme for more than half a year, and I&#8217;m currently working on the next version during leisure time. &#8220;<em>But the current one is good, simple and plain</em>&#8221; &#8211; Yes it is, but it lacks things that I didn&#8217;t knew yet when I first made this. Actually v1 is my first full WP template. So basically, the current theme (v1) doesn&#8217;t support <a title="WP Widgets" href="http://codex.wordpress.org/Plugins/WordPress_Widgets" target="_blank">wordpress widgets</a>. Oh yes, widgets, also known as the &#8220;sidebar accessories&#8221; which are plugins that can be integrated into the sidebar.<span id="more-392"></span></p>
<p>So taking feature and content design into consideration, I decided to redesign my theme and catch up. And now presenting a sneak peak into the version changes. (also click on image below to enlarge)</p>
<div id="attachment_393" class="wp-caption alignright" style="width: 160px"><a href="http://jaycsantos.com/uploads/2009/03/designv2.jpg"  onclick="return false"><img id="img_392_designv2_jpg" class="size-thumbnail wp-image-393" title="JaycSantos.com v2 design" src="http://jaycsantos.com/uploads/2009/03/designv2-150x88.jpg" alt="JaycSantos.com v2 design" width="150" height="88"onclick="enlarge(this)" longdesc="http://jaycsantos.com/uploads/2009/03/designv2.jpg" /></a><p class="wp-caption-text">v2 design</p></div>
<ul>
<li>I&#8217;ve redesigned the logo to be brighter and sharper.</li>
<li>Kept the color scheme (I still like my colors) and some box styles yet made the design livelier.</li>
<li>Sided with more personalized design than corporate look but still as sleek as it is.</li>
<li>Widget support! And it&#8217;s not for sidebars only. <img src='http://jaycsantos.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
<li>More content/game/portfolio exposure.</li>
<li>Maximized content width yet still support 1024&#215;768 user screens.</li>
<li>Removed the news section (from v1).</li>
</ul>
<p>As of this post&#8217;s time, sliced it up into html/css then the next step is converting into a wordpress theme. Watch out, I&#8217;ll be uploading v2 in a week or three.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/news/v2-redesign/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Cheating AI from Hardcourt &#8211; Plan it out</title>
		<link>http://jaycsantos.com/flash/cheating-ai-from-hardcourt-plan-it-out/</link>
		<comments>http://jaycsantos.com/flash/cheating-ai-from-hardcourt-plan-it-out/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 04:24:01 +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=372</guid>
		<description><![CDATA[Working with AI is a real pain, but I say, it most likely depends on what your game needs, what you want it to do. Most developers who are new would quickly ask the magical word “how?” or “how to start?”. If you haven’t had experience or good experience with AI patterns and behaviors, hear [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_379" class="wp-caption alignleft" style="width: 249px"><a href="http://jaycsantos.com/games/hardcourt/"><img class="size-full wp-image-379" title="Hardcourt" src="http://jaycsantos.com/uploads/2009/03/screen1.jpg" alt="Hardcourt" width="239" height="167" /></a><p class="wp-caption-text">Hardcourt</p></div>
<p>Working with AI is a real pain, but I say, it most likely depends on what your game needs, what you want it to do. Most developers who are new would quickly ask the magical word “how?” or “how to start?”. If you haven’t had experience or good experience with AI patterns and behaviors, hear me out on how I did it with this game.</p>
<p>With my first game, <a href="http://jaycsantos.com/games/hardcourt/">Hardcourt</a>, it was also my first time to encounter programming (<em>or should I say cheating</em>) artificial intelligence (AI). I myself didn’t have proper educational background with AI patterns and behaviors else haven’t even read anything about it when I made that game (I still had dial-up before, so no online tutorials too). With the first build of Hardcourt there were no AI so no playing with CPU, it was just a 2 player on a single PC game. After some time I decided to make it into a much complete game and had been finalized as it is now. I’ve had received good reviews with how the AI performs. It can totally beat the players up (esp with quick play, hard mode).</p>
<p>AI was the most troubling part as I thought; I didn’t even have an idea where to begin with. Now let me share a little knowledge on how I have dealt with this so called “artificial intelligence” and cheated my way through it. This shall be some multi-part discussion on the step by step methods when I dealt with them.</p>
<p><span id="more-372"></span><br />
<strong><span style="color: #ff0000;">Note</span></strong>: Starting here, we reference the character AI (which is the CPU controlled enemies) as our player or the primary character; hence the human player will be referenced as the enemy. So don’t confuse yourselves.</p>
<h3>Charts:</h3>
<p>Flow charts were the first logical programming I had; it can clearly display your problem’s logic through proper data flow. So basically, what I wanted was the AI to do all the same things that a human can and would do (just the common and usual behaviors):</p>
<ol>
<li>go after the ball</li>
<li>shoot</li>
<li>steal the ball</li>
<li>go offensive (attack)</li>
</ol>
<p>… it does look simple, but where do we start?</p>
<h3>Basic Behaviors</h3>
<p>First thing to do is look for actions which least require external forces/object interaction but executes the aim of the game.</p>
<p>These steps are primarily available for those “race to finish” games where the primary aim is achieved without requiring going after opponents. This would be good for certain sports games, racing, who-is-faster, etc games. For hardcourt, since the game is basketball, the primary aim is to shoot into the hoops. So that would include <em>going after the ball</em> and <em>shooting the ball</em>.</p>
<div id="attachment_380" class="wp-caption aligncenter" style="width: 315px"><img class="size-full wp-image-380" title="Basic Chart - Get, move shoot " src="http://jaycsantos.com/uploads/2009/03/ai_chart0.gif" alt="Basic Chart - Get, move shoot" width="305" height="241" /><p class="wp-caption-text">Basic Chart - Get, move shoot</p></div>
<p>To explain the chart through words:</p>
<ol>
<li>See if our character has the ball or not,</li>
<li>if not, get the ball and check again if the ball was acquired (»1)</li>
<li>else if yes, shoot the ball,</li>
<li>then check if the ball was acquired again (»1)</li>
</ol>
<p>It&#8217;s actually a very simple loop but provides the main action required to fulfill the game’s primary purpose. Upon testing this basic behavior, the character shall go after &amp; get the ball, shoot, do get the ball again and repeat the process.</p>
<h3>Expanding for more Interactions</h3>
<p>The next step is to consider the external factors. This is where the character considers the status or actions of another character. It can either be to support allies or to go after enemies depending of certain conditions.</p>
<blockquote><p><em>Case</em>: What if the enemy has possession of the ball? &#8211; Then <em>steal the ball</em>.</p></blockquote>
<div id="attachment_381" class="wp-caption aligncenter" style="width: 384px"><img class="size-full wp-image-381" title="Interactive Chart - Steal the ball" src="http://jaycsantos.com/uploads/2009/03/ai_chart1.gif" alt="Interactive Chart - Steal the ball" width="374" height="263" /><p class="wp-caption-text">Interactive Chart - Steal the ball</p></div>
<p>From this case after knowing that the ball is not in the character’s possession we have to determine where the ball specifically is.</p>
<ol>
<li>So the enemy has the ball:</li>
<li>if so, we have to steal it</li>
<li>else if not, we just go after and get the ball.</li>
<li>Either way we loop again with checking if we have the ball’s possession.</li>
</ol>
<p>So basically we determine the ball’s condition, our character doesn’t just chase it, and he has to steal it if it’s in the enemy&#8217;s possession. In case you’re wondering why we did not consider getting the ball directly after stealing the ball is because there will chances that stealing the ball would simply fail and the enemy still has its possession.</p>
<blockquote><p><em>Case</em>: The enemy doesn’t have the ball and we don’t want the enemy to get a hold of it. &#8211; <em>go offensive, attack!</em></p></blockquote>
<p>On this next expansion, we decide to make our character more aggressive and attempt to stop the enemy to from gaining the ball.</p>
<div id="attachment_382" class="wp-caption aligncenter" style="width: 487px"><img class="size-full wp-image-382" title="Interactive Chart 2 - Go offensive, attack!" src="http://jaycsantos.com/uploads/2009/03/ai_chart2.gif" alt="Interactive Chart 2 - Go offensive, attack!" width="477" height="303" /><p class="wp-caption-text">Interactive Chart 2 - Go offensive, attack!</p></div>
<p>Now we made it clearer that for stealing the ball we attack the enemy. For this, before going after the ball, we determine if the enemy is closer than the ball is.</p>
<ol>
<li>Check if enemy is closer than the ball,</li>
<li>if so, attack him</li>
<li>else if not, we go after ball first.</li>
<li>Either way we loop again with checking if we have the ball’s possession.</li>
</ol>
<p>With this chart expansion, our AI now considers the enemy position compared to the ball’s position and determines which action to do. It might not be the wisest decision making but it would comprehend that fact that the enemy is offensive in a way.</p>
<p>Now our AI behavior chart is complete enough to be competitive… or at the least it does what it has to. We can even add some more like evading the enemy when taking a shot, even simply evading enemy attacks or other more complex actions/reactions from various events and circumstances. But with hardcourt, this is all that I would have needed. Although if you’ll consider more decision making for your AI with more specific situations, you’ll be making a better AI and might even be more intelligent than the human.</p>
<p>You want to see these behaviors in action? &#8211; analyze and see for yourself: <a href="../games/hardcourt/?play">play hardcourt now</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/cheating-ai-from-hardcourt-plan-it-out/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Applying cel-shading with Flash</title>
		<link>http://jaycsantos.com/flash/applying-cel-shading-with-flash/</link>
		<comments>http://jaycsantos.com/flash/applying-cel-shading-with-flash/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 04:42:18 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Arts n Visuals]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[cel-shading]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=331</guid>
		<description><![CDATA[I&#8217;ve introduce cel-shading &#8211; my personal preference on art style (and its currently the best I have ) &#8211; in a previous post Using Cel-shading. And now I have something to share on basically how to apply them in flat drawings using flash. This is based on my experience from how I started drawing it [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_332" class="wp-caption alignleft" style="width: 180px"><img class="size-full wp-image-332" title="Cel-shading" src="http://jaycsantos.com/uploads/2009/03/thumb.jpg" alt="Cel-shading" width="170" height="120" /><p class="wp-caption-text">Cel-shading</p></div>
<p>I&#8217;ve introduce cel-shading &#8211; my personal preference on art style (and its currently the best I have <img src='http://jaycsantos.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) &#8211; in a previous post <a href="http://jaycsantos.com/flash/using-cel-shading">Using Cel-shading</a>. And now I have something to share on basically how to apply them in flat drawings using flash. This is based on my experience from how I started drawing it to how I do it these days.</p>
<p>With tools provided with flash we can achieve a cel-shaded effect in quite different ways and yet achieve similar (if not so the same) results. As for this, we&#8217;ll be using the rectangle tool, line tool and brush, but of course all with the help of the selection tool and paint bucket tool for our color fill needs.  Just remember the base rule, to have consistent light source when adding the shades.</p>
<div id="attachment_357" class="wp-caption aligncenter" style="width: 242px"><img class="size-full wp-image-357" title="cel-shading-0-char" src="http://jaycsantos.com/uploads/2009/03/cel-shading-0-char.jpg" alt="Our sample character" width="232" height="140" /><p class="wp-caption-text">Our sample character</p></div>
<p>I&#8217;ve prepared a hand drawn character where we will be applying shading. Now, don&#8217;t tell me that my drawing doesn&#8217;t look any good, it&#8217;s all that I can do. :-p</p>
<h4>Rectangle Tool</h4>
<p>First off the bat, is the rectangle tool. Why the rectangle, not the cirle? Hence the edge points will be needed to snap on other line&#8217;s edges. The rectangle tool is only good for simple shapes with not so much detail, so for this example I can only see the nose to be simple enough.<span id="more-331"></span></p>
<table border="0" cellspacing="0" cellpadding="0" width="90%">
<tbody>
<tr>
<td>
<div id="attachment_334" class="wp-caption alignnone" style="width: 139px"><img class="size-full wp-image-334" title="cel-shading-1-nose" src="http://jaycsantos.com/uploads/2009/03/cel-shading-1-nose.jpg" alt="Nose - Simple shape" width="129" height="115" /><p class="wp-caption-text">Nose - Simple shape</p></div>
<p>1. Start with a basic shape from your drawing where the shade would not be too complicated (so don&#8217;t choose the ears, unless your ears is all round and flat to begin with).</td>
<td>
<div id="attachment_335" class="wp-caption alignnone" style="width: 116px"><img class="size-full wp-image-335" title="cel-shading-2-color" src="http://jaycsantos.com/uploads/2009/03/cel-shading-2-color.jpg" alt="Select a color" width="106" height="141" /><p class="wp-caption-text"> </p></div>
<p>2. Select a skin color which is slightly darker to the skin color. Take note the darkness or lightness of the shade would depend on the imagined light intensity.</td>
<td>
<div id="attachment_337" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-337" title="cel-shading-3" src="http://jaycsantos.com/uploads/2009/03/cel-shading-3.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>3. Using the rectangle tool, draw a small circle.</td>
</tr>
<tr>
<td>
<div id="attachment_338" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-338" title="cel-shading-4" src="http://jaycsantos.com/uploads/2009/03/cel-shading-4.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>4. Make sure to enable <em>snap to objects</em> to connect points easier. Use the selection tool to drap the corner to it one of the border edges.</td>
<td>
<div id="attachment_344" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-344" title="cel-shading-5" src="http://jaycsantos.com/uploads/2009/03/cel-shading-5.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>5. Continue dragging the corners to the borders. Then use the paint bucket tool to color fill the space created .</td>
<td>
<div id="attachment_345" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-345" title="cel-shading-6" src="http://jaycsantos.com/uploads/2009/03/cel-shading-6.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>6. Drag the other sides to form a curve. This will be the shade for the nose curves.</td>
</tr>
<tr>
<td>
<div id="attachment_346" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-346" title="cel-shading-7" src="http://jaycsantos.com/uploads/2009/03/cel-shading-7.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>7. Continue dragging the remaining sides and curve them to make the round nose appear more round.</td>
<td>
<div id="attachment_347" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-347" title="cel-shading-8" src="http://jaycsantos.com/uploads/2009/03/cel-shading-8.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>8. Now from the point where both points meets (a corner) drag it to make a smooth curve.</td>
<td>
<div id="attachment_348" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-348" title="cel-shading-9" src="http://jaycsantos.com/uploads/2009/03/cel-shading-9.jpg" alt=" " width="170" height="120" /><p class="wp-caption-text"> </p></div>
<p>9. And we have the shading for the nose.</td>
</tr>
</tbody>
</table>
<p>Well it obviously took long enough just to have that shade using the rectangle tool which will prove that it isn&#8217;t that effective for this task.</p>
<h4>Line Tool</h4>
<p>So next we will be using the line tool. Now things would be much quicker to do hopefully compared to the above or maybe not, since we&#8217;ll be shading the whole face now (with the ears).</p>
<table border="0" cellspacing="0" cellpadding="0" width="90%">
<tbody>
<tr>
<td>
<div id="attachment_350" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-350" title="cel-shading-line-11" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-11.jpg" alt=" " width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>10. Using the line tool, create a line above the jaw then curve it a little. We will have connected lines instead of having just one so that we can have more control over the curves.</td>
<td>
<div id="attachment_351" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-351" title="cel-shading-line-12" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-12.jpg" alt=" " width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>11. The reason we curved the line is so that we won&#8217;t hit the mouth lines. Now extend it just above the chin. Curve it to your liking and to follow the jaw lines.</td>
</tr>
<tr>
<td>
<div id="attachment_352" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-352" title="cel-shading-line-13" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-13.jpg" alt=" " width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>12. Continue with creating lines and curve them to show how the face is (note: straight lines = flat faces).</td>
<td>
<div id="attachment_353" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-353" title="cel-shading-line-14" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-14.jpg" alt=" " width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>13. Continue lining and curving until you close the line for the shaded part.</td>
</tr>
<tr>
<td>
<div id="attachment_354" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-354" title="cel-shading-line-15" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-15.jpg" alt="cel-shading-line-15" width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>14. Since the ears are a little huge (okay, not little but really huge!), we won&#8217;t completely shade it. Create lines and curve them respective to how the ears are popped-out.</td>
<td>
<div id="attachment_355" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-355" title="cel-shading-line-16" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-16.jpg" alt=" " width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>15. After lining and curving, we can simply use the paint bucket tool to fill the shade color.</td>
</tr>
<tr>
<td>
<div id="attachment_356" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-356" title="cel-shading-line-17" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-17.jpg" alt=" " width="200" height="200" /><p class="wp-caption-text"> </p></div>
<p>16. When everything is looking okay (lines and curves are good enough), we have to delete all the lines (unless you&#8217;ll make them necessary).</td>
<td>
<div id="attachment_358" class="wp-caption alignnone" style="width: 210px"><img class="size-full wp-image-358" title="cel-shading-line-18" src="http://jaycsantos.com/uploads/2009/03/cel-shading-line-18.jpg" alt=" " width="200" height="281" /><p class="wp-caption-text"> </p></div>
<p>17. And look now, we have our character shaded quite well.</td>
</tr>
</tbody>
</table>
<p>Now that we have our character&#8217;s face shaded, the only thing left is the shirt.</p>
<h4>Brush Tool</h4>
<p>For this last one, we&#8217;ll be using the brush tool and expect this one to be the quickest of them all. Although the outcome might not be well straight and curved unlike from lines but who care anyway, we just want it to look better and do it fast. By the way I&#8217;m using my tablet but this shading can still be achieved by using a mouse.</p>
<table border="0" cellspacing="0" cellpadding="0" width="90%">
<tbody>
<tr>
<td>
<div id="attachment_360" class="wp-caption alignnone" style="width: 180px"><img class="size-medium wp-image-360" title="cel-shading-brush-19" src="http://jaycsantos.com/uploads/2009/03/cel-shading-brush-19.jpg" alt=" " width="170" height="190" /><p class="wp-caption-text"> </p></div>
<p>18. With the Brush tool, we have to set the brush mode to Paint Selection.</td>
<td>
<div id="attachment_361" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-361" title="cel-shading-brush-20" src="http://jaycsantos.com/uploads/2009/03/cel-shading-brush-20.jpg" alt=" " width="170" height="190" /><p class="wp-caption-text"> </p></div>
<p>19. Select the fill for the shirt using Selection tool.</td>
<td>
<div id="attachment_362" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-362" title="cel-shading-brush-21" src="http://jaycsantos.com/uploads/2009/03/cel-shading-brush-21.jpg" alt=" " width="170" height="190" /><p class="wp-caption-text"> </p></div>
<p>20. With Brush mode set to Paint Selection, only the selected parts will be painted, so go on and brush the upper part of the shirt which is supposed to be shadowed from the character&#8217;s head and don&#8217;t worry about paint going off the selection, it won&#8217;t.</td>
</tr>
<tr>
<td>
<div id="attachment_363" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-363" title="cel-shading-brush-22" src="http://jaycsantos.com/uploads/2009/03/cel-shading-brush-22.jpg" alt=" " width="170" height="190" /><p class="wp-caption-text"> </p></div>
<p>21. Then do the same for the part bellow the arms.</td>
<td>
<div id="attachment_364" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-364" title="cel-shading-brush-23" src="http://jaycsantos.com/uploads/2009/03/cel-shading-brush-23.jpg" alt=" " width="170" height="190" /><p class="wp-caption-text"> </p></div>
<p>22. And also with the sleeves.</td>
<td>
<div id="attachment_359" class="wp-caption alignnone" style="width: 180px"><img class="size-full wp-image-359" title="cel-shading-brush-24" src="http://jaycsantos.com/uploads/2009/03/cel-shading-brush-24.jpg" alt=" " width="170" height="190" /><p class="wp-caption-text"> </p></div>
<p>23. And do the same for the arm too. And we&#8217;re finished!</td>
</tr>
</tbody>
</table>
<p>I supposed you would not like to use the rectangle tool, but I just wanted to share that since I once used it before. The line tool has it&#8217;s own purpose on certain circumstances that using the brush tool might be troubling.</p>
<div id="attachment_365" class="wp-caption aligncenter" style="width: 116px"><img class="size-full wp-image-365" title="Sample character with shading" src="http://jaycsantos.com/uploads/2009/03/cel-shading1.jpg" alt="Sample character with shading" width="106" height="141" /><p class="wp-caption-text">Sample character with shading</p></div>
<p>So maybe now, you have an idea on how you&#8217;ll be able to cel-shade your drawings from my example here. Of course hand draw styles are not meant to be with perfect lines, curves and shades, it would all come to a matter of preference and personal style. Learning this drawing style might take a while for you to get used to but it will be well deserved when all comes to place after a lot of practice. Now, go! go! go and draw your toons&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/applying-cel-shading-with-flash/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Do you know ActionScript? AS3 Stage</title>
		<link>http://jaycsantos.com/flash/do-you-know-actionscript-as3-stage/</link>
		<comments>http://jaycsantos.com/flash/do-you-know-actionscript-as3-stage/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 07:38:47 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[code bits]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=366</guid>
		<description><![CDATA[Before, the Stage class in AS2 is treated as a class whose properties are all static, but with AS3 the new and better Stage class is now a display object. Both making the stage class the main drawing area and where flash content is shown. Basically, just think of it as a practical stage, all [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_369" class="wp-caption alignleft" style="width: 270px"><img class="size-full wp-image-369" title="stage" src="http://jaycsantos.com/uploads/2009/03/stage.jpg" alt="Stage" width="260" height="170" /><p class="wp-caption-text">Where are my display objects?</p></div>
<p>Before, the Stage class in AS2 is treated as a class whose properties are all static, but with AS3 the new and better Stage class is now a display object. Both making the stage class the main drawing area and where flash content is shown. Basically, just think of it as a practical stage, all actors and props are all objects on display and those who are backstage are objects which are simply not included in the stage yet.</p>
<p>The Stage is a singleton class whereas there can only be one instance of it (imagine watching a play where there are 2 or more stages, its just impossible). Even if you add multiple instances of display objects to the display list, the stage property of each display object still refers to the same Stage object (that goes for loaded SWF files too).</p>
<blockquote><p>Did you know that the stage is not globally accessible &#8211; you can&#8217;t call it or reference it from anywhere (unless manually referenced in a variable of course). The simplest way to do so is to use the <code>stage</code> property from any of your display objects which were added to the display list.</p></blockquote>
<p>Yes, you heard it right from &#8220;any&#8221; display object, but take note, only if it is already added to the display list. It doesn&#8217;t matter if your display object is a child of another display object or how far the display heirarchy it is, as long as it is included in the display list.</p>
<blockquote><p>Don&#8217;t confuse yourself with &#8220;Stage&#8221; (with capital &#8220;S&#8221;) and &#8220;stage&#8221; (with small caps). &#8220;Stage&#8221; refers to the class flash.display.Stage and &#8220;stage&#8221; is it&#8217;s instance.</p>
<p>Just remember, from a common coding convention: classes begin with capital letters and variables don&#8217;t.</p></blockquote>
<p>If the display object is removed or not yet added from the display list the stage property is just set to null.</p>
<p><span id="more-366"></span></p>
<hr />
<h4>Example:</h4>
<p>Create a Square.as. The doStuff function just tries to see if the stage is available or not from its own scope.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Graphics</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Square extends <span style="color: #004993;">Sprite</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Square<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #009900;">// just some drawing</span>
            <span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2</span>, 0x666666 <span style="color: #000000;">&#41;</span>;
            <span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">48</span> <span style="color: #000000;">&#41;</span>;
            <span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span>;
            <span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>;
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> doStuff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">stage</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span> <span style="color: #009900;">// if stage is available</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">'I see the stage! '</span><span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">stage</span> <span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #009900;">// stage is not available</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">'I am not on stage... :( '</span> <span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now create MyStage.as and a new Flash File (actioscript 3.0) then set MyStage as your Document class (just on your properties panel).</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyStage extends <span style="color: #004993;">MovieClip</span>
    <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MyStage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #6699cc; font-weight: bold;">var</span> square<span style="color: #000000; font-weight: bold;">:</span>Square = <span style="color: #0033ff; font-weight: bold;">new</span> Square<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// instanciate a Square</span>
            square.doStuff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> square <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// add the square instance into display list</span>
            square.doStuff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When you test your movie the output panel should display something like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">I am not on stage... :(
I see the stage! [object Stage]</pre></div></div>

<hr />
<h4>Neat Trick: code snippet</h4>
<p>So based on this you can have a neat little trick to determine if your display objects are on stage and added to the display list.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> isDisplayed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">Boolean</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">stage</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p>I hope you understand a little bit more with using display objects and the stage from what I have shared.</p>
<hr />
<h3>Other Keywords</h3>
<ul>
<li>Working with Actionscript 3 Stage.</li>
<li>Knowing Flash Actionscript 3 Stage.</li>
<li>Stage &amp; stage difference in actionscript 3.</li>
<li>Proper use of Flash Actionscript 3 Stage.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/do-you-know-actionscript-as3-stage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

