<?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; Actionscript 2</title>
	<atom:link href="http://jaycsantos.com/category/flash/actionscript2/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 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>Experiment: How to determine if allowNetworking parameter is enabled</title>
		<link>http://jaycsantos.com/flash/experiment-how-to-determine-if-allownetworking-parameter-is-enabled/</link>
		<comments>http://jaycsantos.com/flash/experiment-how-to-determine-if-allownetworking-parameter-is-enabled/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 17:06:42 +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[research]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=276</guid>
		<description><![CDATA[I&#8217;ve been fond of experimenting on things and so this time with what I do and work on most. I&#8217;ll try to make some research and test based on what I have made. These posts won&#8217;t be recognized as 100% accurate and correct on all situations although I aim to be at the most. See [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been fond of experimenting on things and so this time with what I do and work on most. I&#8217;ll try to make some research and test based on what I have made. These posts won&#8217;t be recognized as 100% accurate and correct on all situations although I aim to be at the most. See <a href="http://jaycsantos.com/disclaimer/" target="_blank">disclaimer </a>before implementing any theories and/or statements described in. So let me start with an quite old issue with disabling external links upon embedding an swf in a web page.</p>
<p>Since flash player 9, site owners were allowed to take use of the additional security. You can control a SWF file&#8217;s access to network functionality by setting the <code>allowNetworking</code> parameter in the <code>&lt;object&gt;</code> and <code>&lt;embed&gt;</code> tags in the HTML page that contains the SWF content. It can be included by having:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">object</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">data</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sample.swf&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;application/x-shockwave-flash&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sampleswf&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;allowNetworking&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;internal&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;src&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sample.swf&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">param</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sampleswf&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">object</span>&gt;</span></pre></div></div>

<p>See your help file about this or view the livedocs (<a href="http://livedocs.adobe.com/flash/9.0/main/00001079.html" target="_blank">as2</a> or <a href="http://livedocs.adobe.com/flash/9.0/main/00000351.html" target="_blank">as3</a>) discussing this added security.  Basically if this ever happens to a flash game, the developer and/or sponsor are neglected of having click backs to their own sites/portals, which is not a good thing. So I made a little research and test if actionscript can determine if  <code>allowNetworking</code> was enabled or not. This test will try to determine if external links are allowed, not exactly return the value of the <code>allowNetworking</code> parameter.<span id="more-276"></span> I made a <a href="http://jaycsantos.com/uploads/2008/10/allowNetworking-test/allowNetworking-test.html" target="_blank">sample page here</a> having 2 swfs (as2 &amp; as3) embedded in different ways: using the basic embed-object tags, AC_RunActiveContent.js and swfobject class. As said in that page, the swfs on the left uses <code>allowNetworking</code> set to &#8220;<code>internal</code>&#8221; so it should say &#8220;External links disabled&#8221; and the swfs on the right doesn&#8217;t so it should say &#8220;External links ok&#8221;. Then try clicking on the links on each swf and determine if the message displayed respectively complies (either <em>disabled</em> or <em>OK</em>), if so then it pass if not then a fail.  And these are the results I had (all with winXP):</p>
<table class="tabol" style="text-align:center; font-size:0.95em;" border="0">
<tbody>
<tr class="b">
<td rowspan="2"></td>
<td style="text-align: center;" colspan="2">basic Embed</td>
<td colspan="2">AC_RunActiveContent.js</td>
<td colspan="2">swfobject class</td>
</tr>
<tr class="b">
<td>AS2</td>
<td>AS3</td>
<td>AS2</td>
<td>AS3</td>
<td>AS2</td>
<td>AS3</td>
</tr>
<tr>
<td class="b">Firefox 3.03</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
<tr>
<td class="b">Opera 9.5</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
<tr>
<td class="b">Safari 3.1.2</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
<tr>
<td class="b">Chrome 0.2.149.30</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
<tr>
<td class="b">IE 7.0.5730</td>
<td style="color:red;">fail</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
<tr>
<td class="b">IE 6</td>
<td style="color:red;">fail</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
<tr>
<td class="b">IE 5.5</td>
<td style="color:red;">fail</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
<td>pass</td>
</tr>
</tbody>
</table>
<p><strong>In conclusion</strong>: I wasn&#8217;t successful with AS2, and to mention that the AS2 code that I used is just a trick and not legally proven in anyway. Also if you try to make a trap-like-function with your game/project/swf you should also be aware that opening external links are disabled with standalone flash players by default (which I think implements allowNetworking = &#8220;internal&#8221; with the standalone player&#8217;s sandbox). So the when the AS3 sample is ran using the standalone player you can catch the error number which will be error #0.  <strong>Note</strong>: If you are using another browser which I haven&#8217;t tested or another OS, you are very much encouraged to comment out if my <a href="http://jaycsantos.com/uploads/2008/10/allowNetworking-test/allowNetworking-test.html" target="_blank">sample page</a> worked properly or not with what you have.</p>
<hr />So how did I made those results? Here are the codes that I used:</p>
<blockquote><p>Please read the <a href="http://jaycsantos.com/disclaimer/" target="_blank">DISCLAIMER</a> first before you do anything with what you&#8217;ll see and learn.</p></blockquote>
<p><strong>Actionscript 2:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">external</span>.<span style="color: #006600;">ExternalInterface</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> allowNetworking;
<span style="color: #000000; font-weight: bold;">var</span> str:<span style="color: #0066CC;">String</span>;
&nbsp;
<span style="color: #0066CC;">try</span><span style="color: #66cc66;">&#123;</span>
	allowNetworking = ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
	str = allowNetworking +<span style="color: #ff0000;">''</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> str == <span style="color: #ff0000;">'null'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Error&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">test</span>.<span style="color: #0066CC;">htmlText</span> = <span style="color: #ff0000;">'external links OKn&quot;'</span>+ str +<span style="color: #ff0000;">'&quot;'</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span> $e <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">test</span>.<span style="color: #0066CC;">htmlText</span> = <span style="color: #ff0000;">'external links DISABLEDn &quot;'</span>+ str +<span style="color: #ff0000;">'&quot;'</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
btn.<span style="color: #0066CC;">onRelease</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: #0066CC;">getURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://jaycsantos.com&quot;</span>,<span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In AS2, if the <code>ExternalInterface.call()</code> failed it doesn&#8217;t throw an error unlike in AS3 so I have to make some workarounds with AS2 to get a return value from any possible function call and <code>ExternalInterface.call()</code> is my only chance. <code>ExternalInterface.call(null)</code> was expected to return a <code>null</code> value when <code>allowNetworking</code> is set to either &#8220;<code>internal</code>&#8221; or &#8220;<code>none</code>&#8221; and was supposed to be <code>undefined</code> when not. It was almost a passing code trick but IE has failed once again making the whole AS2 test a failure.</p>
<p><strong>Actionscript 3:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.external</span>.<span style="color: #004993;">ExternalInterface</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> allowNetworking<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">try</span><span style="color: #000000;">&#123;</span>
	allowNetworking = <span style="color: #004993;">ExternalInterface</span>.<span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #004993;">test</span>.<span style="color: #004993;">htmlText</span> = <span style="color: #990000;">'external links OK'</span>;
<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">test</span>.<span style="color: #004993;">htmlText</span> = <span style="color: #990000;">'external links DISABLEDn err #'</span><span style="color: #000000; font-weight: bold;">+</span> $e.<span style="color: #004993;">errorID</span> <span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">''</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> mouseClickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">navigateToURL</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://jaycsantos.com/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
btn.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, mouseClickHandler<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Practically I used <code>ExternalInterface.call(null)</code> instead of trying to instantiate <code>URLRequest</code> because if it succeeds it would open up an external link and might just be blocked by a popup-blocker since the code is ran without user interaction. Unlike AS2, it will simply throw in an error when the network method is restricted.</p>
<blockquote><p>Please read the <a href="http://jaycsantos.com/disclaimer/" target="_blank">DISCLAIMER</a> first before you do anything with what you&#8217;ve seen and learned.</p></blockquote>
<p>I would not recommend using any of this in a professional project or until further tests are made. But it somehow shows logic that it should work especially with the AS3 code, so <strong>use at your own risk</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/experiment-how-to-determine-if-allownetworking-parameter-is-enabled/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Custom Cursor Class in AS2</title>
		<link>http://jaycsantos.com/flash/custom-cursor-class-in-as2/</link>
		<comments>http://jaycsantos.com/flash/custom-cursor-class-in-as2/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 21:04:06 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[as2]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[custom something]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=212</guid>
		<description><![CDATA[I have made a simple yet quite useful custom cursor class in this case is in AS2. All that is needed is a movieclip that will take use of it. This also doesn&#8217;t make the mouse flicker with constant Mouse.hide() calls, it stays hidden only when on focus with the swf. If you know how [...]]]></description>
			<content:encoded><![CDATA[<p>I have made a simple yet quite useful custom cursor class in this case is in AS2. All that is needed is a movieclip that will take use of it. This also doesn&#8217;t make the mouse flicker with constant Mouse.hide() calls, it stays hidden only when on focus with the swf. If you know how to extend a movieclip this would look very very simple and easy.</p>
<div class="wp-caption aligncenter" style="width: 310px"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="300" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="quality" value="high" /><param name="src" value="http://jaycsantos.com/wp-content/uploads/2008/09/custom-cursor_secure.swf" /><embed type="application/x-shockwave-flash" width="300" height="100" src="http://jaycsantos.com/wp-content/uploads/2008/09/custom-cursor_secure.swf" quality="high"></embed></object><p class="wp-caption-text">Custom Cursor Class - sample swf</p></div>
<p>The class extends the movieclip class therefore inherits all of a movieclip&#8217;s properties, methods and events. With a cursor all we would practically need is the onMouseMove event. See this custom cursor on the work from my game, <a href="http://jaycsantos.com/games/puttbase/?play" target="_blank">Puttbase</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MCCursor <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> cur:MCCursor;
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> frame:<span style="color: #0066CC;">Object</span>;
	<span style="color: #000000; font-weight: bold;">var</span> onMouseMove2:<span style="color: #000000; font-weight: bold;">Function</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> MCCursor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">Mouse</span>.<span style="color: #0066CC;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> cur <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			cur.<span style="color: #0066CC;">removeMovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">// singleton class</span>
		cur = <span style="color: #0066CC;">this</span>;
		<span style="color: #808080; font-style: italic;">// swap to a very high but still safe depth</span>
		<span style="color: #0066CC;">swapDepths</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1048031</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">onMouseMove</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">_x</span> += <span style="color: #0066CC;">_xmouse</span>;
		<span style="color: #0066CC;">_y</span> += <span style="color: #0066CC;">_ymouse</span>;
		onMouseMove2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Basically this is all we would need. The class is singleton (restricted to only one instance) so the instanced movieclip is stored on the static variable <em>cur</em>. First create a movieclip, name it whatever you want, set a linkage identifier and<span id="more-212"></span>, most importantly, have &#8220;MCCursor&#8221; as its class (see figure below). The class may vary if you&#8217;re using a separate folder to contain all your classes but I&#8217;ll leave explaining that on another time.</p>
<div class="wp-caption aligncenter" style="width: 573px"><img title="Figure screenshot" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-22-%5B14-53-52%5D.png" alt="Figure screenshot" width="563" height="304" /><p class="wp-caption-text">Figure screenshot</p></div>
<p>To instantiate, either leave the cursor movieclip on stage during design time, where the class is instanced when the movieclip appears on stage, or attach them during run time like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> cur = <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">attachMovie</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'mcCursor'</span>, <span style="color: #ff0000;">'cur'</span>, <span style="color: #0066CC;">_root</span>.<span style="color: #0066CC;">getNextHighestDepth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Now to add some other functions to make some jobs and our life easier:</p>
<p>This hides the mouse pionter again when the swf regains focus:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> onMouseDown2:<span style="color: #000000; font-weight: bold;">Function</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">onMouseDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">Mouse</span>.<span style="color: #0066CC;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">/**
	* force update position, not totally
	* necessary but might be useful somehow
	* */</span>
	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">onMouseMove</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	onMouseDown2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you want to have your own onMouseDown event function you can use onMouseDown2 instead, so to not overwrite this method, do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">MCCursor.<span style="color: #006600;">cur</span>.<span style="color: #006600;">onMouseDown2</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;">//do what you want</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>To handle a better gotoAndStop function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> frame:<span style="color: #0066CC;">Object</span>;
<span style="color: #808080; font-style: italic;">/**
* f parameter can be a number or a string (frame label),
* it will be stored on frame variable regardless
* if it is a valid frame from the movieclip or not
* */</span>
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span> f:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span> f <span style="color: #66cc66;">&#41;</span>;
	frame = f;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">/**
* f is a numeric value or string of the made frame label
* */</span>
<span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> gotoFrame<span style="color: #66cc66;">&#40;</span> f:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>cur <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>;
	cur.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span> f <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">/**
* set getFrameNumber parameter to true to force returning the
* actual numberic value of the current frame of the movieclip
* */</span>
<span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getFrame<span style="color: #66cc66;">&#40;</span> getFrameNumber:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>cur <span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> getFrameNumber <span style="color: #66cc66;">&#41;</span>
		<span style="color: #b1b100;">return</span> cur.<span style="color: #0066CC;">_currentframe</span>;
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">return</span> cur.<span style="color: #006600;">frame</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>For convenience, use the this static function to set the frame of the cursor and can be accessed anywhere (MCCursor class must be imported on the class to recognize it):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">MCCursor.<span style="color: #006600;">gotoFrame</span><span style="color: #66cc66;">&#40;</span> f <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>&#8230;have these on the class then it&#8217;s good to be used.</p>
<p>Grab the actual <a href="http://jaycsantos.com/downloads/MCCursor.as">Class AS file here</a> which also quite commented.</p>
<p>This is a v0.1 so it is a simple but is really expandable class. I&#8217;ll update this with new methods and features when something comes up and is necessary. An AS3 version will also be made soon.</p>
<hr />
<h3>Other Keywords:</h3>
<ul>
<li>Change Actionscript 2 mouse cursor</li>
<li>Flash mouse pointer</li>
<li>Custom mouse pointer</li>
<li>Personal mouse cursor</li>
<li>Replace default cursor with custom mouse pointer</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/custom-cursor-class-in-as2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Frame Based vs Time Based using enterFrame -AS2</title>
		<link>http://jaycsantos.com/flash/frame-based-vs-time-based-using-enterframe-as2/</link>
		<comments>http://jaycsantos.com/flash/frame-based-vs-time-based-using-enterframe-as2/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 13:51:23 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=220</guid>
		<description><![CDATA[Ever experience sudden frame rate drop or frame boost on your game, having sudden lag spikes and performance issues, did it affect the game play tremendously like it slowed down or made everything faster. It is either you really code awfully or your game is so affected with the FPS rate changes from various browsers [...]]]></description>
			<content:encoded><![CDATA[<p>Ever experience sudden frame rate drop or frame boost on your game, having sudden lag spikes and performance issues, did it affect the game play tremendously like it slowed down or made everything faster. It is either you really code awfully or your game is so affected with the FPS rate changes from various browsers and the user’s machine capability.</p>
<p>This is where time based coding takes place. By default, most of us amateurs went and started with frame based not knowing what it really is. When we say frame based the code and/or animation is dependent solely on how fast the FPS was set to, with a steady code, reducing the FPS rate will slow down the game and increasing it will hasten it. Time based coding can manage this quite well since everything can be dependent on time so frame rate doesn’t affect it that much. This problem is not only for games but for any development.</p>
<p><strong>Code:</strong><br />
Let’s get started, I will discuss this with the sense of OOP approach where there is only a centralized loop function called to handle everything. I will not be actually using setInterval but the usual onEnterFrame event function.<br />
This code is assumed to be part of a class, most cases shall be used in the main engine class but can also be used inside other classes too as long as it is correctly structured. This also assumes that the buffer movieclip is already instantiated.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> frameSpd:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> frameTime:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> buffer:<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> startEngine<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;">// some more codes here</span>
	buffer.<span style="color: #0066CC;">onEnterFrame</span> = Delegate.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">this</span>, loop <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// some more codes here</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> loop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	frameTime = <span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">getTimer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - frameTime <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">1000</span>;
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">fpstxt</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">/</span> frameTime <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">10</span> +<span style="color: #ff0000;">'fps<span style="color: #000099; font-weight: bold;">\n</span> '</span>+ frameTime<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">1000</span> +<span style="color: #ff0000;">'ms'</span>;
	frameSpd = frameTime <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">0.030</span>;
	frameSpd = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">min</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2.5</span>, frameSpd <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> t:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">getTimer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// more and more codes are called from here</span>
&nbsp;
	frameTime  = <span style="color: #0066CC;">getTimer</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">fpstxt</span> += <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>+ <span style="color: #66cc66;">&#40;</span> frameTime - t <span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">'ms'</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-220"></span></p>
<p><strong>Lines 1</strong>: made <em>frameSpd</em> a static variable so it can be easily accessed from any other location or class.</p>
<p><strong>Line 11 &amp; 19</strong>: <em>frameTime </em> is the time difference from when the loop was last called and ended.</p>
<p><strong>Line 12 &amp; 20</strong>: returns the frame’s FPS, last frame’s difference speed and actionscript process time.</p>
<p><strong>Line 13</strong>: <em>frameSpd</em> is a variable that determines as a multiplier based on <em>frameTime</em> and the default FPS rate setting (in this case I use 30 as the movie&#8217;s FPS so 30/1000 = 0.030)</p>
<p><strong>Line 14</strong>: makes a tolerance on how slow the frame rate has been and how much frameSpd has to catch up.</p>
<p><strong>Implementations:</strong><br />
I&#8217;ll write some possible implementations in a pseudo code way using AS2 syntax and assuming that Engine is the name of the class where loop is located.</p>
<p>For basic movements, something like this on you movement function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> spd:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">speed</span> <span style="color: #66cc66;">*</span> Engine.<span style="color: #006600;">frameSpd</span>;
<span style="color: #808080; font-style: italic;">// going left</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> dirx <span style="color: #66cc66;">&amp;</span>lt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	_pos.<span style="color: #006600;">x</span> -= spd;
<span style="color: #808080; font-style: italic;">// going right</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> dirx <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	_pos.<span style="color: #006600;">x</span> += spd;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">// more and more code possible here</span></pre></td></tr></table></div>

<p>Having gravity or other accelerating factor is quite tricky but not really too complex, something like this on your affect gravity functions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// store the var locally to improve lookup speed since it is used more than once</span>
<span style="color: #000000; font-weight: bold;">var</span> frameSpd:<span style="color: #0066CC;">Number</span> = Engine.<span style="color: #006600;">frameSpd</span>
<span style="color: #808080; font-style: italic;">// apply gravity or any factor with acceleration</span>
jumpspeed += Engine.<span style="color: #006600;">gravity</span> <span style="color: #66cc66;">*</span> frameSpd;
<span style="color: #808080; font-style: italic;">// apply the speed</span>
<span style="color: #000000; font-weight: bold;">var</span> spd:<span style="color: #0066CC;">Number</span> = jumpspeed <span style="color: #66cc66;">*</span> frameSpd;
_pos.<span style="color: #006600;">y</span> += spd;</pre></td></tr></table></div>

<p>And my favorite, handling animation speed (this one is for characters usually)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> frameSkip:<span style="color: #0066CC;">Number</span> = Main.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">*</span> Engine.<span style="color: #006600;">frameSpd</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span>;
<span style="color: #808080; font-style: italic;">// see what current frame I am on</span>
<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span> _cframe <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>: <span style="color: #808080; font-style: italic;">// stand and idle</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">&#41;</span> _ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">100</span>
		<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
		<span style="color: #b1b100;">break</span>;
	<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span>: <span style="color: #808080; font-style: italic;">// run</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">80</span> <span style="color: #66cc66;">&#41;</span> _ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">80</span>
		<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
		<span style="color: #b1b100;">break</span>;
	<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">5</span>: <span style="color: #808080; font-style: italic;">// jump</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">65</span> <span style="color: #66cc66;">&#41;</span> _ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">65</span> + <span style="color: #cc66cc;">25</span>;
		<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
	<span style="color: #b1b100;">break</span>;
	<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">7</span>: <span style="color: #808080; font-style: italic;">// double jump</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">50</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			_ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">50</span> + <span style="color: #cc66cc;">25</span>;
			_cframe = <span style="color: #cc66cc;">5</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> _ccframe += frameSkip;
	<span style="color: #b1b100;">break</span>;
	<span style="color: #000000; font-weight: bold;">default</span>: <span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><em>_cframe</em> is the current animation frame of the character and <em>_ccframe</em> acts like a play head for the animation itself. If you have questions about this, my animation controller is <a href="http://jaycsantos.com/flash/using-a-frame-controller-to-do-animations-as2/">discussed well enough on this post</a>.</p>
<p>So basically having time based coding is a really powerful feature, you can even force multiply the frameSpd to a certain speed either on purposely speeding up your game or making it slow motion (matrix style). From the loop code from the top most edit it with this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">frameSpd = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">min</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2.5</span>, frameSpd <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> forceSpd;</pre></td></tr></table></div>

<p>Just make sure <em>frameSpd </em> maintains a tolerable value; having 10 would make it intensely fast and 0.1 would make it gruesomely slow. By default it has value of 1, 2 to make it twice and 0.5 to make half the speed, same goes if you&#8217;ll use a <em>forceSpd</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/frame-based-vs-time-based-using-enterframe-as2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using a Frame Controller to do animations -AS2</title>
		<link>http://jaycsantos.com/flash/using-a-frame-controller-to-do-animations-as2/</link>
		<comments>http://jaycsantos.com/flash/using-a-frame-controller-to-do-animations-as2/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 13:50:07 +0000</pubDate>
		<dc:creator>Jayc Santos</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Arts n Visuals]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[animation]]></category>

		<guid isPermaLink="false">http://jaycsantos.com/?p=206</guid>
		<description><![CDATA[This is how I do my frame controller basically in logical means. Features: dependent on code, not on the swf&#8217;s framing very flexible and expandable reusable for any other animation that require distinct control can cheat key framing really simple Structure: Basically the primary movieclip of the character shall contain a few designated frames for [...]]]></description>
			<content:encoded><![CDATA[<p>This is how I do my frame controller basically in logical means.</p>
<ul><strong>Features:</strong></p>
<li>dependent on code, not on the swf&#8217;s framing</li>
<li>very flexible and expandable</li>
<li>reusable for any other animation that require distinct control</li>
<li>can cheat key framing</li>
<li>really simple</li>
</ul>
<p><strong>Structure:</strong><br />
Basically the primary movieclip of the character shall contain a few designated frames for each possible animation it shall have. For this case I have frame 1, 3, 5 and 7 each allocated as stand/idle, running, jump and double jump animations respectively and all have &#8220;mc&#8221; as their instance names. This is also shown on these images:</p>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-41-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-41-52%5D_png" title="Stand MovieClip, frame 1 &amp; 2" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-41-52%5D.png" alt="Stand MovieClip, frame 1 &amp; 2" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-41-52%5D.png" /></a><p class="wp-caption-text">Stand MovieClip, frame 1 &amp; 2</p></div>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-42-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-42-52%5D_png" title="run MovieClip, frame 3 &amp; 4" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-42-52%5D.png" alt="run MovieClip, frame 3 &amp; 4" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-42-52%5D.png" /></a><p class="wp-caption-text">run MovieClip, frame 3 &amp; 4</p></div>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-43-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-43-52%5D_png" title="Jump MovieClip - frame 5 &amp; 6" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-43-52%5D.png" alt="Jump MovieClip - frame 5 &amp; 6" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-43-52%5D.png" /></a><p class="wp-caption-text">Jump MovieClip - frame 5 &amp; 6</p></div>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-44-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-44-52%5D_png" title="Double Jump MovieClip - frame 7 &amp; 8" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-44-52%5D.png" alt="Double Jump MovieClip - frame 7 &amp; 8" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-44-52%5D.png" /></a><p class="wp-caption-text">Double Jump MovieClip - frame 7 &amp; 8</p></div>
<p><span id="more-206"></span></p>
<p>Then each animation respectively has the animation sequence tween. And take note: that each animation is not key framed with one&#8217;s or two&#8217;s yet they set on a five&#8217;s key framing. This is because our frame controller must be able to tolerate slight FPS reductions based on time (<a href="http://jaycsantos.com/flash/frame-based-vs-time-based-using-enterframe-as2/" target="_blank">detailed on this post</a>). So when the animation is played from the timeline during design time it would appear to be quite slower than expected.</p>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-51-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-51-52%5D_png" title="Stand MovieClip - 100 frames" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-51-52%5D.png" alt="Stand MovieClip - 100 frames" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-51-52%5D.png" /></a><p class="wp-caption-text">Stand MovieClip - 100 frames</p></div>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-52-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-52-52%5D_png" title="Run MovieClip - 80 frames" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-52-52%5D.png" alt="Run MovieClip - 80 frames" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-52-52%5D.png" /></a><p class="wp-caption-text">Run MovieClip - 80 frames</p></div>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-53-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-53-52%5D_png" title="Jump MovieClip - 65 frames" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-53-52%5D.png" alt="Jump MovieClip - 65 frames" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-53-52%5D.png" /></a><p class="wp-caption-text">Jump MovieClip - 65 frames</p></div>
<div class="wp-caption aligncenter" style="width: 283px"><a href="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-54-52%5D.png"  onclick="return false"><img id="img_206_sshot-2008-09-23-%5B00-54-52%5D_png" title="Double Jump MovieClip - 50 frames" src="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-54-52%5D.png" alt="Double Jump MovieClip - 50 frames" width="273" height="100"onclick="enlarge(this)" longdesc="http://jaycsantos.com/wp-content/uploads/2008/09/sshot-2008-09-23-%5B00-54-52%5D.png" /></a><p class="wp-caption-text">Double Jump MovieClip - 50 frames</p></div>
<p>Also take note of each of the animation&#8217;s total frames; they&#8217;ll be used when playing through them. After setting up the structures here is where the controller comes in.</p>
<p><strong>Code:</strong><br />
This is assumed to be within your character&#8217;s class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> _cframe:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> _ccframe:<span style="color: #0066CC;">Number</span>;
<span style="color: #000000; font-weight: bold;">var</span> mc:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// animation movieclip child</span>
<span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
	controlFrame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// some more codes here</span>
	<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span> _cframe <span style="color: #66cc66;">&#41;</span>;
	mc.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span> _ccframe <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> controlFrame<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> frameSkip:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">*</span> Engine.<span style="color: #006600;">frameSpd</span> <span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">1</span>;
&nbsp;
	<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span> _cframe <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>: <span style="color: #808080; font-style: italic;">// stand/idle</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">&#41;</span> _ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">100</span>
			<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span>: <span style="color: #808080; font-style: italic;">// run</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">80</span> <span style="color: #66cc66;">&#41;</span> _ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">80</span>
			<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">5</span>: <span style="color: #808080; font-style: italic;">// jump</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">65</span> <span style="color: #66cc66;">&#41;</span> _ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">65</span> + <span style="color: #cc66cc;">25</span>;
			<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">7</span>: <span style="color: #808080; font-style: italic;">// double jump</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">50</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				_ccframe = <span style="color: #66cc66;">&#40;</span> _ccframe + frameSkip <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">50</span> + <span style="color: #cc66cc;">25</span>;
				_cframe = <span style="color: #cc66cc;">5</span>;
			<span style="color: #66cc66;">&#125;</span>	<span style="color: #b1b100;">else</span> _ccframe += frameSkip;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #000000; font-weight: bold;">default</span>: <span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><em>_cframe</em> is the current animation frame of the character and <em>_ccframe</em> acts like a play head for the <em>mc</em> animation itself.</p>
<p><strong>Line 11</strong>: You can see why we used five&#8217;s key framing because our <em>frameSkip</em> is 5 by default and is then altered by the <em>frameSpd</em>. So if <em>frameSpd</em> is not used, then the controller will basically be frame based.</p>
<p><strong>Line 15 &amp; 19</strong>: Check if the sum of <em>frameSkip</em> &amp; <em>_ccframe</em> is greater than the total frames then use modulus (%) to reset.</p>
<p><strong>Line 16, 20, 24 &amp; 30</strong>: Else just add the <em>frameSkip</em> to <em>_ccframe</em></p>
<p><strong>Line 23 &amp; 28</strong>: Check if the sum of <em>frameSkip</em> &amp; <em>_ccframe</em> is greater than the total frames then use modulus (%) to reset and add a special offset specific with respect to the animation intended.</p>
<p><strong>Line 29</strong>: Transfers the animation then from double jump to jump with the same <em>_ccframe</em></p>
<p><strong>Line 7 &amp; 8</strong>: The update will them take care of updating the right movieclips based on what <em>_cframe</em> &amp; <em>_ccframe</em> was changed into.</p>
<div class="wp-caption aligncenter" style="width: 490px"><object width="480" height="300" data="http://jaycsantos.com/wp-content/uploads/2008/09/frame-controller-sample_secure.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="src" value="http://jaycsantos.com/wp-content/uploads/2008/09/frame-controller-sample_secure.swf" /></object><p class="wp-caption-text">A sample which uses this frame controller. Arrows keys to move.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://jaycsantos.com/flash/using-a-frame-controller-to-do-animations-as2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

