<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.2" -->
<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/"
	>

<channel>
	<title>David Stiller's blog</title>
	<link>http://www.quip.net/blog</link>
	<description>Luck is the residue of good design.</description>
	<pubDate>Tue, 24 Jun 2008 15:08:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>
	<language>en</language>
			<item>
		<title>Building Click-and-Rotate Content in Flash</title>
		<link>http://www.quip.net/blog/2008/flash/building-click-and-rotate-content</link>
		<comments>http://www.quip.net/blog/2008/flash/building-click-and-rotate-content#comments</comments>
		<pubDate>Tue, 24 Jun 2008 15:05:07 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>Flash</category>
	<category>ActionScript 2.0</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/building-click-and-rotate-content</guid>
		<description><![CDATA[


In one implementation of the QuickTime VR format, known as QTVR Object Movies, the user can click-and-drag an image to seemingly rotate it, as if spinning the real-life object on a lazy Susan. &#160;This simulated 3D interactivity can improve multimedia curb appeal, and makes for a nifty way to showcase merchandise. &#160;But it doesn&#8217;t stop [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/" title="Flash"><img src="/blog/wp-images/icons/topic_flash.gif" align="left" width="18" height="18" alt="Flash" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p>In one implementation of the QuickTime VR format, known as <a href="http://developer.apple.com/documentation/QuickTime/InsideQT_QTVR/2Chap/chapter_3_section_3.html#//apple_ref/doc/uid/TP40000944-CH206-BAJGIHHE" class="external" target="_blank">QTVR Object Movies</a>, the user can click-and-drag an image to seemingly rotate it, as if spinning the real-life object on a lazy Susan. &nbsp;This simulated 3D interactivity can improve multimedia curb appeal, and makes for a nifty way to showcase merchandise. &nbsp;But it doesn&rsquo;t stop there: &nbsp;the same basic principle can also bring click-and-drag responsiveness to short video sequences and even user input widgets, such as the click-and-scrub input fields of numerous Adobe dialog boxes.&nbsp; <a id="more-169"></a></p>
<p>If you&rsquo;re a Flash developer and interested in click-and-drag functionality, you might be interested in a new tutorial series I&rsquo;m working on at <a href="http://www.communitymx.com/" class="external" target="_blank">CommunitMX.com</a>.&nbsp; CMX content is generally subscriber-based, but this intro article is free.&nbsp; It does take you completely through the steps of creating basic click-and-rotate content (in other words, it&rsquo;s not a hook), though future articles will delve into variations on the theme.&nbsp; The bulk of the article is written in terms of ActionScript 2.0, but the same general principles apply in ActionScript 3.0, and an AS3 summary is provided.</p>
<p><a href="http://www.communitymx.com/content/article.cfm?cid=8F0CA" class="external" target="_blank">http://www.communitymx.com/content/article.cfm?cid=8F0CA</a></p>
<p>As a quick note to regular readers of this blog, I&rsquo;ve been waaaaay behind &mdash; as you know! &mdash; in my usual activity.&nbsp; The simple reason is that I&rsquo;ve been working on two new books in addition to my normal client workload.&nbsp; One of these is the <a href="http://www.amazon.com/gp/product/0596517351?ie=UTF8&amp;tag=davistilsblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596517351" class="external" target="_blank">O&rsquo;Reilly title</a> I mentioned <a href="http://www.quip.net/blog/2008/flash/new-oreilly-title-coming-soon" target="_blank">back in March</a>.&nbsp; Although my chapters were done at the time, a handful of challenges tipped O&rsquo;Reilly into delaying the original publication date.&nbsp; Given the change, we were faced with the opportunity of rewriting the book for the <em>next</em> version of Flash (post-CS3), and we took it.&nbsp; <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp; That meant dumping some chapters, combining others, splitting yet others, and writing new content altogether.&nbsp; Ultimately, <em>ActionScript 3.0:&nbsp; The Quick Answer Guide for Flash Professionals</em> is a better book for all the additional input and reworking, and I&rsquo;m thankful for that.&nbsp; The other book an update to Tom&rsquo;s and my <a href="http://www.amazon.com/gp/product/159059861X?ie=UTF8&amp;tag=davistilsblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=159059861X" class="external" target="_blank"><em>Foundation Flash CS3 for Designers</em></a>, which we&rsquo;re working on as I type this.</p>
<p>When the dust settles, I&rsquo;ll be a on a no-new-books kick for a while.&nbsp; And I&rsquo;ll back to blogging.&nbsp; <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp; I miss it.</p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/building-click-and-rotate-content/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>How to Position Dynamically Loaded Content Based on Browser Resizing</title>
		<link>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-position-dynamically-loaded-content-browser-resizing</link>
		<comments>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-position-dynamically-loaded-content-browser-resizing#comments</comments>
		<pubDate>Mon, 21 Apr 2008 02:20:27 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>ActionScript 2.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/actionscript-20/how-to-position-dynamically-loaded-content</guid>
		<description><![CDATA[
In one of the recent comments to &#8220;How to Position Movie Clips Based on Browser Resizing,&#8221; a look at the ActionScript 2.0 Stage.onResize event, reader Eddy asked about adjusting an image loaded at runtime; particularly, about fading in an image set to scale and reposition itself based on the size of the browser.&#160; I was [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<p>In one of the recent comments to &ldquo;<a href="http://www.quip.net/blog/2006/flash/how-to-position-movie-clips-browser-resizing" target="_blank">How to Position Movie Clips Based on Browser Resizing</a>,&rdquo; a look at the ActionScript 2.0 <code>Stage.onResize</code> event, reader Eddy asked about adjusting an image loaded at runtime; particularly, about fading in an image set to scale and reposition itself based on the size of the browser.&nbsp; I was originally going to reply to his comment directly, but this seems to me like something that would make a decent blog entry of its own, so here&rsquo;s one particular stab at it.&nbsp; <a id="more-168"></a></p>
<h3>Using a two container approach</h3>
<p>The trick to fading in one item over another is to change the alpha transparency of the upper item.&nbsp; If you&rsquo;re fading in (as opposed to fading out), that means changing the alpha from zero to 100 (we&rsquo;re talking AS2, by the way; in AS3, that would be zero to one).&nbsp; If you&rsquo;re fading in something that&rsquo;s loaded from the outside, you have to wait until the external item &mdash; here, it&rsquo;ll be a JPG &mdash; has fully loaded.&nbsp; If you&rsquo;re publishing to Flash Player 7 or higher, one of your options is the <code>MovieClipLoader</code> class, so we&rsquo;ll use that for this walkthrough.&nbsp; If you&rsquo;ve read &ldquo;<a href="http://www.quip.net/blog/2006/flash/how-to-position-movie-clips-browser-resizing" target="_blank">How to Position Movie Clips Based on Browser Resizing</a>,&rdquo; then some of this will look familiar.</p>
<p>Here&rsquo;s the first chunk of ActionScript 2.0:</p>
<pre><code>import mx.transitions.Tween;
import mx.transitions.easing.*;

Stage.scaleMode = &quot;noScale&quot;;
Stage.align = &quot;TL&quot;;

var container:MovieClip = this.createEmptyMovieClip(&quot;mcContainer&quot;, 0);
var pictA:MovieClip = container.createEmptyMovieClip(&quot;mcPictA&quot;, 0);
var pictB:MovieClip = container.createEmptyMovieClip(&quot;mcPictB&quot;, 1);</code></pre>
<p>The first two lines import the <code>Tween</code> class and all the tween-related easing classes in the <code>transitions.easing</code> package.&nbsp; After that, the <code>Stage</code> class&rsquo;s <code>scaleMode</code> and <code>align</code> properties are specifically set to values that allow for programmatic response to changes in the SWF&rsquo;s dimensions &mdash; such as when the user resizes the browser &mdash; when the SWF is embedded at 100% width and height in the HTML.</p>
<p>Finally an arbitrarily named variable, <code>container</code>, is declared and set to an instance of the <code>MovieClip</code> class.&nbsp; In AS2, this happens by way of the <code>MovieClip.createEmptyMovieClip()</code> method, as invoked on the global <code>this</code> property.&nbsp; In this context, the term <code>this</code> refers to the main timeline, because that&rsquo;s where this code is located.&nbsp; The second parameter, 0, refers to the depth of this movie clip, which is the lowest possible depth a dynamically created movie clip can hold.</p>
<p>The new movie clip is given the instance name <code>mcContainer</code>, but the variable <code>container</code> makes an equally valid reference.&nbsp; Inside this container clip, two additional containers are created in the same way.&nbsp; Their references are <code>pictA</code> and <code>pictB</code>, respectively, and they&rsquo;re set to depths 0 and 1 inside <code>container</code>.</p>
<p>We need a bit more prep work before we can dive into the fun stuff, so add the following new ActionScript:</p>
<pre><code>var root:MovieClip = this;
var firstRun:Boolean = true;
var currentPict:String = &quot;A&quot;;
var tw:Tween;

var listener:Object = new Object();
listener.onResize = positionContent;
listener.onLoadInit = loadHandler;</code></pre>
<p>In order to make the main timeline easier to reference in later code, a variable named <code>root</code> is declared and set to the term <code>this</code> (again, a reference to the main timeline).&nbsp; A <code>firstRun</code> variable holds the Boolean value <code>true</code> and will act as a signpost to determine whether or not code is being executed on the user&rsquo;s first arrival.&nbsp; The <code>currentPict</code> variable currently refers to the string &ldquo;A&rdquo; (yup, just the letter A) and will be used to toggle back and forth between the inner containers <code>pictA</code> and <code>pictB</code>. &nbsp;A <code>Tween</code> instance (<code>tw</code>) is declared, but not yet set to anything; and finally, a <code>listener</code> variable is declared and set to an instance of the <code>Object</code> class.&nbsp; Every one of these variables is arbitrarily named.</p>
<p>The <code>listener</code> variable will act on behalf of the <code>Stage</code> class and the <code>MovieClipLoader</code> instance (whose creation is your next step).&nbsp; Two properties are assigned to listener:&nbsp; <code>onResize</code> (that is, the <code>Stage.onResize</code> event) and <code>onLoadInit</code> (that is, <code>MovieClipLoader.onLoadInit</code>).&nbsp; Each of these properties stores a reference to a custom function &mdash; <code>positionContent()</code> and <code>loadhandler()</code> &mdash; that will be executed as the events occur.&nbsp; You&rsquo;ll see in a moment how the <code>MovieClipLoader</code> instance and the <code>Stage</code> class will add the <code>listener</code> variable to their VIP lists.</p>
<p>When the user resizes the browser, which in turn resizes the SWF, a <code>Stage.onResize</code> event will be dispatched.&nbsp; The <code>listener</code> variable&rsquo;s <code>onResize</code> property will trigger the <code>positionContent()</code> function, which will move around the desired content.&nbsp; Same idea for the <code>MovieClipLoader</code> instance, which keeps track of loaded movie clips and image files.</p>
<p>Let let&rsquo;s get to it.&nbsp; Here&rsquo;s some more code:</p>
<pre><code>var mcl:MovieClipLoader = new MovieClipLoader();
mcl.loadClip(&quot;firstImage.jpg&quot;, pictA);
mcl.addListener(listener);</code></pre>
<p>Here, an arbitrarily named variable, <code>mcl</code>, is declared and set to an instance of the <code>MovieClipLoader</code> class.&nbsp; The <code>MovieClipLoader.loadClip()</code> method is invoked on <code>mcl</code>, which is instructed to load a file named firstImage.jpg into the <code>pictA</code> inner container clip.&nbsp; In the very next line, the <code>MovieClipLoader.addListener()</code> method is invoked on <code>mcl</code>, which causes mcl to keep tabs on the <code>listener</code> object.&nbsp; That means we need to write a <code>loadHandler()</code> function, so that Flash knows what to do when firstImage.jpg arrives.&nbsp; Here&rsquo;s that function:</p>
<pre><code>function loadHandler():Void {
  if (firstRun) {
    Stage.addListener(listener);
    positionContent();
    firstRun = false;
  }
  pictA.swapDepths(pictB);
  tw = new Tween(root[&quot;pict&quot; + currentPict], &quot;_alpha&quot;, Strong.easeOut, 0, 100, 2, true);
  togglePicts();
}</code></pre>
<p>This gets a tad knotty, so let&rsquo;s step through it carefully.&nbsp; Remember that <code>firstRun</code> variable?&nbsp; Here&rsquo;s where it comes into play.&nbsp; If this is the first time the <code>loadHandler()</code> function is being executed &mdash; that is, <code>firstRun</code> is <code>true</code> &mdash; then the <code>Stage.addListener()</code> method tells the <code>Stage</code> class to keep track of <code>listener</code>, just like <code>mcl</code> is doing.&nbsp; In effect, this means that the <code>positionContent()</code> function will now be executed whenever the SWF resizes.&nbsp; (Don&rsquo;t worry, <code>positionContent()</code> hasn&rsquo;t been shown yet, but it&rsquo;s coming in a few paragraphs.)&nbsp; For good measure, the <code>positionContent()</code> function is called at this point, too, just to position the desired movie clip(s) even if the browser hasn&rsquo;t been resized.&nbsp; Then <code>firstRun</code> is set to <code>false</code>, because the previous two lines only need to be run during the visitor&rsquo;s first encounter with the SWF.</p>
<p>After that <code>if</code> statement, the <code>MovieClip.swapDepths()</code> method is invoked on <code>pictA</code>, swapping that inner container with its sibling, <code>pictB</code>.&nbsp; If you scroll up a bit, you&rsquo;ll see that <code>pictA</code> was created first, at a depth of 0 inside the <code>container</code> clip (that is, below <code>pictB</code>).&nbsp; This <code>swapDepths()</code> method now puts <code>pictA</code> above its sibling at depth 1, and consequently moves <code>pictB</code> to depth 0.</p>
<p>The <code>tw</code> variable is now set to a new <code>Tween</code> instance that changes the <code>MovieClip._alpha</code> property of <code>pictA</code> from 0 to 100 over two seconds.&nbsp; The first parameter of this <code>Tween</code> constructor function is probably the most interesting.&nbsp; The expression is <code>root[&quot;pict&quot; + currentPict]</code> &#8230; so what does that mean?&nbsp; Well, remember, <code>root</code> simply refers to the main timeline, which contains a handful of variables, including the variables <code>pictA</code> and <code>pictB</code>.&nbsp; So the main timeline is our reference point.&nbsp; From the main timeline&rsquo;s point of view, there are two variables that start with the string &ldquo;pict&rdquo; (which is the next part of the expression), and finally, the value of <code>currentPict</code> happens to be &ldquo;A&rdquo;, so the full expression effectively reads <code>root.pictA</code>.&nbsp; From the main timeline&rsquo;s point of view, that simply means <code>pictA</code>, which refers to the inner container clip that has just been instructed to fade itself in from zero to 100.</p>
<p>Why use a made-up variable, here, instead of <code>_root</code>?&nbsp; For discussion on that question, see &ldquo;<a href="http://www.quip.net/blog/2006/flash/actionscript-20/is-root-evil" target="_blank">Is _root Evil?</a>&rdquo;&nbsp; For more information on this brackets (<code>[]</code>) approach to targeting variables, see &ldquo;<a href="http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically" target="_blank">How to Reference Objects Dynamically</a>.&rdquo;&nbsp; The reason a dynamic reference is useful here is because we can simply change the value of <code>currentPict</code> from &ldquo;A&rdquo; to &ldquo;B&rdquo;, and back again, to keep flip-flopping which container is the currently operative movie clip.&nbsp; Finally, a custom <code>togglePicts()</code> function is called, which does just that (changes &ldquo;A&rdquo; to &ldquo;B&rdquo;, or vice versa).</p>
<p>Here&rsquo;s the <code>positionContent()</code> function:</p>
<pre><code>function positionContent():Void {
  container._width = Stage.width;
  container._yscale = container._xscale;
}</code></pre>
<p>This can get as crazy or simple as you like.&nbsp; In this case, the width of <code>container</code> clip is set to the width of the Stage, and its <code>_yscale</code> is set to the same value as its <code>_xscale</code>, which simply ensures that <code>container</code> maintains its current aspect ratio when scaled.&nbsp; The fading and depth swapping that occurs with <code>pictA</code> and <code>pictB</code> isn&rsquo;t affected by the positioning of <code>container</code>.</p>
<p>Here&rsquo;s the <code>togglePicts()</code> function:</p>
<pre><code>function togglePicts():Void {
  if (currentPict == &quot;A&quot;) {
    currentPict = &quot;B&quot;;
  } else {
    currentPict = &quot;A&quot;;
  }
}</code></pre>
<p>Pretty straightforward.&nbsp; Now, in Eddy&rsquo;s question, the website provided a way to change the background image.&nbsp; Let&rsquo;s step through how that might happen.&nbsp; Assuming a button symbol with the instance name <code>myButton</code>, take a look at the following code:</p>
<pre><code>myButton.onRelease = buttonHandler;</code></pre>
<pre><code>function buttonHandler():Void {
  mcl.loadClip(&quot;theSecondImage.jpg&quot;, root[&quot;pict&quot; + currentPict]);
}</code></pre>
<p>The <code>Button.onRelease</code> event is assigned to a custom function named <code>buttonHandler()</code>.&nbsp; The <code>buttonHandler()</code> function invokes <code>MovieClipLoader.loadClip()</code> on the <code>mcl</code> instance, passing it a new JPG to load, and a new container in which to load it.&nbsp; Remember, at this point, the custom <code>loadHandler()</code> function has already been called (it was triggered when theFirstImage.jpg loaded).&nbsp; The last thing that function does is call <code>togglePicts()</code>, which changes the value of <code>currentPict</code> from &ldquo;A&rdquo; to &ldquo;B&rdquo;.&nbsp; At this point, then, the value of <code>root[&quot;pict&quot; + currentPict]</code> actually points to <code>pictB</code>.&nbsp; The <code>mcl</code> instance is already listening to <code>listener</code>, so when theSecondImage.jpg loads, <code>loadHandler()</code> will be called again.&nbsp; Here&rsquo;s that code again, just for reference:</p>
<pre><code>function loadHandler():Void {
  if (firstRun) {
    Stage.addListener(listener);
    positionContent();
    firstRun = false;
  }
  pictA.swapDepths(pictB);
  tw = new Tween(root[&quot;pict&quot; + currentPict], &quot;_alpha&quot;, Strong.easeOut, 0, 100, 2, true);
  togglePicts();
}</code></pre>
<p>The code inside the <code>if</code> statement won&rsquo;t run this time, because <code>firstRun</code> is <code>false</code>.&nbsp; The <code>swapDepths()</code> line will swap <code>pictA</code> and <code>pictB</code>, putting <code>pictB</code> back on top.&nbsp; The <code>tw</code> variable will be set to a new instance of the <code>Tween</code> class.&nbsp; Even though the code doesn&rsquo;t change, <em>the value of <code>root[&quot;pict&quot; + currentPict]</code> has changed</em>, which means it&rsquo;s now <code>pictB</code> that will be tweened from an alpha of zero to 100, thus fading in <code>pictB</code>.&nbsp; After that, <code>togglePicts()</code> changes <code>currentPict</code> from &ldquo;B&rdquo; back to &ldquo;A&rdquo; and the cycle can easily repeat again, for as many images as you want to load.</p>
<p>Eddy could, for example, offer a second button with this code:</p>
<pre><code>myButton2.onRelease = buttonHandler2;</code></pre>
<pre><code>function buttonHandler2():Void {
  mcl.loadClip(&quot;theThirdImage.jpg&quot;, root[&quot;pict&quot; + currentPict]);
}</code></pre>
<p>Just another <code>Button.onRelease</code> event handler.&nbsp; Here, a third image is loaded, and the container it&rsquo;s loaded into is &mdash; as it happens &mdash; <code>pictA</code> (simply because the value of <code>currentPict</code> is &ldquo;A&rdquo; again).&nbsp; The inner two containers, <code>pictA</code> and <code>pictB</code>, keep swapping depths as needed, and the value of <code>currentPict</code> always ensure that the upper container is the one that fades in.</p>
<p>Here&rsquo;s the full code discussed, all on one grand swoop.</p>
<pre><code>import mx.transitions.Tween;
import mx.transitions.easing.*;

Stage.scaleMode = &quot;noScale&quot;;
Stage.align = &quot;TL&quot;;

var container:MovieClip = this.createEmptyMovieClip(&quot;mcContainer&quot;, 0);
var pictA:MovieClip = container.createEmptyMovieClip(&quot;mcPictA&quot;, 0);
var pictB:MovieClip = container.createEmptyMovieClip(&quot;mcPictB&quot;, 1);

var root:MovieClip = this;
var firstRun:Boolean = true;
var currentPict:String = &quot;A&quot;;
var tw:Tween;

var listener:Object = new Object();
listener.onResize = positionContent;
listener.onLoadInit = loadHandler;

var mcl:MovieClipLoader = new MovieClipLoader();
mcl.loadClip(&quot;a.gif&quot;, pictA);
 mcl.addListener(listener);

function loadHandler():Void {
  if (firstRun) {
    Stage.addListener(listener);
    positionContent();
    firstRun = false;
  }
  pictA.swapDepths(pictB);
  tw = new Tween(root[&quot;pict&quot; + currentPict], &quot;_alpha&quot;, Strong.easeOut, 0, 100, 2, true);
  togglePicts();
}

function positionContent():Void {
  container._width = Stage.width;
  container._yscale = container._xscale;
}

function togglePicts():Void {
  if (currentPict == &quot;A&quot;) {
    currentPict = &quot;B&quot;;
  } else {
    currentPict = &quot;A&quot;;
  }
}

myButton.onRelease = buttonHandler;

function buttonHandler():Void {
  mcl.loadClip(&quot;b.gif&quot;, root[&quot;pict&quot; + currentPict]);
}</code></pre>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-position-dynamically-loaded-content-browser-resizing/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Using SWFObject for More than Just Embedding SWFs</title>
		<link>http://www.quip.net/blog/2008/flash/using-swfobject-for-more</link>
		<comments>http://www.quip.net/blog/2008/flash/using-swfobject-for-more#comments</comments>
		<pubDate>Thu, 13 Mar 2008 04:48:31 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>Flash</category>
	<category>Web Development</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/using-swfobject-for-more</guid>
		<description><![CDATA[

My favorite mechanism for embedding SWFs, hands down, is Geoff Stearns&#8217; SWFObject.&#160; It&#8217;s clean, lightweight, and easy to use.&#160; Since April 2006, SWFObject has been my first choice for working around the &#8220;click to activate and use this control&#8221; warning in Internet Explorer.&#160; Microsoft has decided to remove this activation behavior from Internet Explorer in [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/" title="Flash"><img src="/blog/wp-images/icons/topic_flash.gif" align="left" width="18" height="18" alt="Flash" class="icon" /></a>
<a href="http://www.quip.net/blog/category/web-development/" title="Web Development"><img src="/blog/wp-images/icons/topic_web-development.gif" align="left" width="18" height="18" alt="Web Development" class="icon" /></a>
<p>My favorite mechanism for embedding SWFs, hands down, is <a href="http://blog.deconcept.com/2004/09/19/about-me/" target="_blank">Geoff Stearns</a>&rsquo; <a href="http://blog.deconcept.com/swfobject/" target="_blank">SWFObject</a>.&nbsp; It&rsquo;s clean, lightweight, and easy to use.&nbsp; Since April 2006, SWFObject has been my first choice for working around the &ldquo;click to activate and use this control&rdquo; warning in Internet Explorer.&nbsp; Microsoft has decided to remove this activation behavior from Internet Explorer in April 2008 &mdash; right around the corner, as of this writing &mdash; but there&rsquo;s still plenty of reason to keep right on using SWFObject.&nbsp; Why?&nbsp; Because it provides an elegant way to detect what version of Flash Player (if any) a website visitor has installed.&nbsp; If you&rsquo;re using the On2 video codec, for example, it means your site requires Flash Player 8 or higher.&nbsp; With SWFObject, you can detect if your visitor has <em>at least 8</em> and then display the SWF; otherwise, display a stand-in message (or image), such as &ldquo;This site requires Flash Player 8 or higher.&rdquo;&nbsp; But what if you want to redirect to another page instead?&nbsp; Or what if you want to display two different SWFs, depending on what&rsquo;s installed?&nbsp; Read on.&nbsp; <a id="more-164"></a></p>
<h3>Getting familiar with SWFObject</h3>
<p>Geoff&rsquo;s website explains in detail how to use SWFObject, and he also wrote an <a href="http://www.adobe.com/devnet/flash/articles/swfobject.html" target="_blank">Adobe article</a> on the subject.&nbsp; In a nutshell, you download the source file, which comes in a ZIP archive, you extract swfobject.js and put it somewhere in your site, such as among your HTML documents in the root or some subfolder of the root.&nbsp; Then you reference this external JavaScript file in the <code>&lt;head&gt;</code> element of any page that intends to use it.</p>
<pre><code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&nbsp;&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot; /&gt;
  &lt;title&gt;Some Document&lt;/title&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;swfobject.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;</code></pre>
<p>Naturally, the src attribute of that <code>&lt;script&gt;</code> element has to path correctly to the JavaScript file.&nbsp; As shown, the swfobject.js file would be in the same folder as the HTML file that references it.</p>
<p>This allows the HTML document able to create a <code>SWFObject</code> object, then use that object to embed a SWF.&nbsp; The SWF will automatically replace the contents of <code>&lt;div&gt;</code> element with an arbitrarily named <code>id</code> attribute, such as &ldquo;flashcontent&rdquo; (though you could just as easily name it &ldquo;bananaHead&rdquo;).</p>
<pre><code>&lt;div id=&quot;flashcontent&quot;&gt;
  This text is replaced by the Flash movie.
&lt;/div&gt;</code></pre>
<p>This <code>&lt;div&gt;</code> element can hold stand-in text, as shown, or an <code>&lt;img&gt;</code> element, or whatever you like.</p>
<pre><code>&lt;div id=&quot;flashcontent&quot;&gt;
  &lt;img src=&quot;someImage.jpg&quot; alt=&quot;Some image&quot; width=&quot;320&quot; height=&quot;240&quot; /&gt;
&lt;/div&gt;</code></pre>
<p>If the user doesn&rsquo;t have JavaScript enabled (most users do) or if the user doesn&rsquo;t have the version of Flash Player you specify &mdash; you&rsquo;ll see this in a moment &mdash; the <code>&lt;div&gt;</code> and all its content will be displayed in the browser. If the user does have the specified version of Flash Player (and JavaScript enabled), the browser will display the Flash content.</p>
<p>That part is handled like this.</p>
<pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
  var so = new SWFObject(&quot;someMovie.swf&quot;, &quot;mymovie&quot;, &quot;320&quot;, &quot;240&quot;, &quot;9&quot;, &quot;#FFFFFF&quot;);
  so.write(&quot;flashcontent&quot;);
&lt;/script&gt;</code></pre>
<p>&hellip; where <code>so</code> is an arbitrarily named variable that points to the new <code>SWFObject</code> instance.&nbsp; The first parameter is the name of your SWF; the second parameter is a unique identifier (like the <code>&lt;div&gt;</code> element&rsquo;s <code>id</code> attribute) that can be reference with JavaScript if desired; the third and fourth are the width and height; the fifth is the minimum required version of Flash Player (here, Flash Player 9), and finally, the background color of the SWF, which can differ from the actual background color of the FLA.</p>
<p>Note that the parameter inside the <code>write()</code> method references the stand-in <code>&lt;div&gt;</code> used earlier by its <code>id</code>.&nbsp; Badda bing.&nbsp; It&rsquo;s that easy.</p>
<h3>Using SWFObject for more</h3>
<p>What if you want to send users to a different page altogether if they don&rsquo;t have the right version of Flash Player?&nbsp; What if you have one FLV video encoded in On2 for users with Flash Player 8 or higher, and another encoded in Sorenson Spark for users with Flash Player 6 or 7?&nbsp; SWFObject gives you that, too.&nbsp; And it&rsquo;s still really easy.</p>
<p>Under the hood, SWFObject creates a <code>deconcept</code> object (the name of Geoff&rsquo;s website) as soon as the page loads.&nbsp; This <code>deconcept</code> object contains a <code>SWFObjectUtil</code> child object that, in turn, features a <code>getPlayerVersion()</code> method.&nbsp; This method returns an object with three useful properties:&nbsp; <code>major</code>, <code>minor</code>, and <code>rev</code>.&nbsp; You can use those numbers to detect very specific versions of Flash Player.&nbsp; As of today, for example, the current version of Flash Player is 9,0,115,0.&nbsp; Of those numbers, 9 is the major version, the first 0 is the minor, and 115 is the revision, which correspond to the properties just mentioned (the last number refers to internal builds, and is irrelevant to version sniffing).</p>
<p>Here&rsquo;s a quick example that uses the <code>SWFObjectUtil()</code> method to embed a Flash Player 8 or higher SWF for an On2 video and, for users with Flash Player 6 or 7, a SWF that only requires either of those versions:</p>
<pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
  var so;
  var flashVersion = deconcept.SWFObjectUtil.getPlayerVersion();
  if (flashVersion.major &gt;= <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' />  {
    so = new SWFObject(&quot;On2Vid.swf&quot;, &quot;mymovie&quot;, &quot;200&quot;, &quot;200&quot;, &quot;8&quot;, &quot;#FFFFFF&quot;);
  } else if (flashVersion.major &gt;= 6) {
    so = new SWFObject(&quot;SorensonSqueezeVid.swf&quot;, &quot;mymovie&quot;, &quot;200&quot;, &quot;200&quot;, &quot;6&quot;, &quot;#FFFFFF&quot;);
  }
  so.write(&quot;flashcontent&quot;);
&lt;/script&gt;</code></pre>
<p>The <code>so</code> variable is declared, as before, but this time isn&rsquo;t set to anything &mdash; yet.&nbsp; Next, an arbitrarily named variable, <code>flashVersion</code>, is set to the return value of the <code>SWFObjectUtil.getPlayerVersion()</code> method.&nbsp; Inside an <code>if</code> statement, the <code>major</code> property of this object (referenced by way of the variable as <code>flashVersion.major</code>) is compared against the number 8 to see if it&rsquo;s greater than or equal to.&nbsp; If it is, the <code>so</code> variable is set to a new instance of <code>SWFObject</code>, as before, and embeds a SWF that displays an On2 video.&nbsp; Otherwise, if <code>flashVersion.major</code> is greater than or equal to 6, an alternate SWF is embedded.&nbsp; If the user has, say, Flash Player 5 &mdash; or none at all, or JavaScript disabled &mdash; the original content of the <code>&lt;div&gt;</code> element is displayed.</p>
<p>Let&rsquo;s say you&rsquo;ve got some nifty content that requires Flash Player 9,0,115,0, because it displays fullscreen video.&nbsp; If your visitor doesn&rsquo;t have the right version of Flash Player, you want to send that visitor to another page on your site that touts the benefits of Flash Player 9 (or whatever &hellip; you get the idea).&nbsp; Here&rsquo;s one way to do that:</p>
<pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
  var so;
  var flashVersion = deconcept.SWFObjectUtil.getPlayerVersion();
  if (flashVersion.major &gt;= 9 &amp;&amp; flashVersion.rev &gt;= 115) {
    so = new SWFObject(&quot;Flash9Content.swf&quot;, &quot;mymovie&quot;, &quot;320&quot;, &quot;240&quot;, &quot;9.0.115&quot;, &quot;#FFFFFF&quot;);
  } else {
    window.location = &quot;someOtherPage.html&quot;;
  }
  so.write(&quot;flashcontent&quot;);
&lt;/script&gt;</code></pre>
<p>Here, the <code>if</code> statement checks both the <code>major</code> and <code>rev</code> properties.&nbsp; Make sure to always check for greater than or equal to (<code>&gt;=</code>), so that next year, when most of your visitors have Flash Player 10, everything will still fly without a hitch. &nbsp;This time, the <code>else</code> clause sets the native JavaScript <code>window.location</code> property to another HTML document.&nbsp; If the user doesn&rsquo;t have 9,0,115,0 or higher installed &mdash; for example, has Flash Player 8 installed instead, or none &mdash; the browser will immediately go to someOtherPage.html.&nbsp; If the user doesn&rsquo;t have JavaScript enabled, the original <code>&lt;div&gt;</code> content will be displayed.&nbsp; If all&rsquo;s well, however, your user will see your SWF.</p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/using-swfobject-for-more/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>New O’Reilly Title, Coming Soon!</title>
		<link>http://www.quip.net/blog/2008/flash/new-oreilly-title-coming-soon</link>
		<comments>http://www.quip.net/blog/2008/flash/new-oreilly-title-coming-soon#comments</comments>
		<pubDate>Thu, 13 Mar 2008 04:36:25 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>Flash</category>
	<category>ActionScript 2.0</category>
	<category>General</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/new-o%e2%80%99reilly-title-coming-soon</guid>
		<description><![CDATA[



I just finished the last of my eleven chapters for a new O&#8217;Reilly title, ActionScript 3.0:&#160; The Quick Answer Guide for Flash Professionals, last Wednesday.&#160; This was shortly after lunch, 12:30 on the dot, and a neat thing happened almost as I lifted my hands from the keyboard (more on that in a sec).&#160; The [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/" title="Flash"><img src="/blog/wp-images/icons/topic_flash.gif" align="left" width="18" height="18" alt="Flash" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<a href="http://www.quip.net/blog/category/general/" title="General"><img src="/blog/wp-images/icons/topic_general.gif" align="left" width="18" height="18" alt="General" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p>I just finished the last of my eleven chapters for a new O&rsquo;Reilly title, <em><a href="http://www.amazon.com/gp/product/0596517351?ie=UTF8&amp;tag=davistilsblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596517351" target="_blank">ActionScript 3.0:&nbsp; The Quick Answer Guide for Flash Professionals</a></em>, last Wednesday.&nbsp; This was shortly after lunch, 12:30 on the dot, and a neat thing happened almost as I lifted my hands from the keyboard (more on that in a sec).&nbsp; The rest of the book is still being written, and it&rsquo;s shaping up nicely.&nbsp; I&rsquo;m pretty excited about this project already!&nbsp; <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp; The aim of this reference is to help developers, even keyframe coders, get up to speed with AS3, so keep your eyes peeled in June!</p>
<p>I took a wild tumble off the grid while I was researching and writing for the past five or six weeks, so I apologize for my late replies to blog comments and email.&nbsp; I&rsquo;ve been catching up on my inbox since last Wednesday and plan to reply to everyone as soon as I can.&nbsp; Thanks, so much, to my friends for all the encouragement!&nbsp; (Go, <a href="http://www.flashgods.org/" target="_blank">FlashGods.org</a>!)</p>
<p>I&rsquo;ll be going over author reviews in the coming weeks, but the heavy lifting is behind me.&nbsp; The neat thing that happened, on day I finished, is this:&nbsp; I went downstairs to gaze out the window for a few moments, and right as I did, UPS brought a package to the door.&nbsp; I opened the box, and inside was a <em>huge</em> tin of Turkish coffee (over a pound!), imported from Turkey, sent by reader &Ccedil;a&#287;atay.&nbsp; What a way to commemorate the milestone!&nbsp; Thanks, &Ccedil;a&#287;atay!&nbsp; This coffee is a real treat!</p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/new-oreilly-title-coming-soon/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>How to use Font Symbols (Embedded Fonts) with ActionScript</title>
		<link>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-use-embedded-fonts-with-actionscript</link>
		<comments>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-use-embedded-fonts-with-actionscript#comments</comments>
		<pubDate>Fri, 29 Feb 2008 04:21:23 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>ActionScript 2.0</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/actionscript-20/how-to-use-embedded-fonts-with-actionscript</guid>
		<description><![CDATA[

You certainly don&#8217;t need ActionScript to make use of embedded fonts.&#160; You can always just embed font outlines manually by selecting a text field, then using the Embed button in the Property inspector.&#160; Select your range of characters, type in your text, then publish; you&#8217;re good to go (and you only need to do it [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p>You certainly don&rsquo;t need ActionScript to make use of embedded fonts.&nbsp; You can always just embed font outlines manually by selecting a text field, then using the Embed button in the Property inspector.&nbsp; Select your range of characters, type in your text, then publish; you&rsquo;re good to go (and you only need to do it with dynamic and input text fields: &nbsp;static text fields embed font outlines automatically).&nbsp; In fact, if your text field has an instance name &mdash; something you can also set in the Property inspector &mdash; then you can determine its text content with ActionScript and the embedded font outlines still hold.&nbsp; But if you&rsquo;re using ActionScript to create text fields on the fly, the mechanics are a bit different.&nbsp; Let&rsquo;s take a look.&nbsp; <a id="more-163"></a></p>
<h3>First, create your font symbol</h3>
<p>To get your font(s) into the SWF, use a Font symbol. They&rsquo;re easy enough to make, but doing so is not necessarily as intuitive as using <kbd>Insert &gt; New Symbol</kbd> or <kbd>Modify &gt; Convert</kbd> to Symbol. Instead, right-click/Command-click inside the Library panel and choose <kbd>New Font</kbd> from the context menu.&nbsp; This brings up the Font Symbol Properties dialog.&nbsp; Ignore the Name field for the moment.&nbsp; Use the listbox in the Font field to find the font you want to embed.&nbsp; Different variations, such as <strong>bold</strong> and <em>italic</em>, must each be imported as individual symbols, so select the appropriate checkbox if necessary.&nbsp; You&rsquo;ll see a choice for Bitmap text, but you should only select that if you want your fonts rendered as bitmaps (rasterized; that is, no vector shapes).&nbsp; Doing so can save on file size, but your embedded fonts won&rsquo;t scale as well, like any bitmap.&nbsp; The Size field only matters if you use bitmapped text.</p>
<p>Once you&rsquo;ve chosen your font and selected the variations (if desired), give your font a name in the Name field.&nbsp; This is the name what will show up in the Library panel, so you might want to use, say, &ldquo;Blackadder normal&rdquo; for the non-bold, non-italic version of a font named Blackadder ITC and &ldquo;Blackadder italic&rdquo; for the italic version of that font.</p>
<p>Having font symbols in your Library isn&rsquo;t enough.&nbsp; You&rsquo;ve also got to give them Linkage identifiers (AS2) or Linkage classes (AS3).&nbsp; Right-click/Command-click on each font symbol and select <kbd>Linkage</kbd> from the context menu.&nbsp; Select Export for ActionScript, which will do a number of things at once: either a) in AS1 or AS2 documents, it will enable the Identifier and Class fields, then populate the Identifier field with the font&rsquo;s Library name; or b) in AS3 documents, it will enable the Class and Base Class fields, then populate the Class field with the font&rsquo;s Library name (the Base Class field will be populated with a reference to the <code>flash.text.Font</code> class).</p>
<p>If you&rsquo;re using AS3 and supplying a class name, you need to make sure your Linkage class name is valid. Like variables, class names can only contain alphanumeric characters &mdash; no punctuation, except for underscore (<code>_</code>) or dollar sign (<code>$</code>), and no spaces &mdash; and the first character can’t be a number. For this example, your “Blackadder normal” might become, say, <code>BlackadderNormal</code>. When you click OK on the Linkage Properties dialog, Flash will warn you that no such class exists and will offer to write it for you automatically. Agree to that, and you’re ready for the code.</p>
<p>Now, on to the ActionScript.</p>
<h3>An answer, short and sweet, and how it works (AS2)</h3>
<p>There are two ways to make use of an embedded font with code:&nbsp; either with an instance of the <code>TextFormat</code> class or the <code>StyleSheet</code> class.&nbsp; We&rsquo;ll look at both.&nbsp; Here&rsquo;s the first example:</p>
<pre><code>var fmt:TextFormat = new TextFormat();
fmt.font = &quot;Blackadder normal&quot;;
fmt.size = 32;

var text1:TextField = this.createTextField(&quot;tf1&quot;, 0, 0, 0, 0, 0);
text1.autoSize = &quot;left&quot;;
text1.embedFonts = true;
text1.setNewTextFormat(fmt);
text1.text = &quot;Lorem ipsum dolor sit amet.&quot;</code></pre>
<p>In the first line, an arbitrarily named variable, <code>fmt</code>, is declared and set to an instance of the <code>TextFormat</code> class.&nbsp; <code>TextFormat</code> features all sorts of styling properties, and two of those are <code>font</code> and <code>size</code>.&nbsp; Here, the <code>font</code> property is set to &ldquo;Blackadder normal,&rdquo; which is the Linkage identifier that was given to the font in the Linkage Properties dialog.</p>
<p>Next, another arbitrarily named variable, <code>text1</code>, is declared and set to an instance of the <code>TextField</code> class.&nbsp; In AS2, there is no direct way to instantiate a text field, such as <code>new TextField</code> (for that, you need AS3).&nbsp; The indirect way is to use the <code>MovieClip.createTextField()</code> method, which is here invoked on the global <code>this</code> property, which happens to refer to a movie clip because this code is in a frame of the main timeline (the main timeline is, in fact a movie clip).&nbsp; This method provides a return value, which is a reference to the newly made text field, stored in the variable <code>text1</code>.&nbsp; So <code>text1</code> is enough to refer to the new text field, but you could also use <code>tf1</code>, which is the instance name provided as the first parameter.&nbsp; The rest of the parameters, all zeroes, can be other values if you like, but whatever you use, they refer to (in order):&nbsp; depth, x position, y position, width, and height.&nbsp; Why set them all to zero?&nbsp; Well, x and y are totally up to you.&nbsp; I usually prefer to set a width and height of zero because the very <em>next</em> line, <code>text1.autoSize = &quot;left&quot;</code>, instructs the text field to resize as needed to contain its text (other valid <code>autoSize</code> values are &quot;center&quot; and &quot;right&quot;, which change the justification of the text).</p>
<p>The line after that is the important one.&nbsp; The <code>TextField.embedFonts</code> property, as invoked on <code>text1</code>, is set to <code>true</code>.&nbsp; Next, the <code>TextField.setNewTextformat()</code> sets the <code>fmt</code> object (the <code>TextFormat</code> instance) as the default styling for this text field.&nbsp; Finally, a string of text is supplied to the <code>TextField.text</code> property.&nbsp; Be aware that you can also use <code>TextField.setTextFormat()</code>, which allows you to set formatting <em>after</em> text has been added to the text field (optional parameters even allow you to specify which characters should be affected).</p>
<p>Alternatively, you can format your text with the <code>StyleSheet</code> class, which requires HTML formatted text.&nbsp; Check it out:</p>
<pre><code>var css:TextField.StyleSheet = new TextField.StyleSheet();
css.setStyle(&quot;p&quot;, {fontFamily:&quot;Blackadder normal&quot;, fontSize:32});
css.setStyle(&quot;a&quot;, {textDecoration:&quot;underline&quot;});

var text2:TextField = this.createTextField(&quot;tf2&quot;, 1, 0, 0, 0, 0);
text2.autoSize = &quot;left&quot;;
text2.embedFonts = true;
text2.styleSheet = css;
text2.html = true;
text2.htmlText = &quot;&lt;p&gt;Lorem ipsum dolor
&lt;a href='http://www.quip.net/'&gt;sit amet&lt;/a&gt;.&lt;/p&gt;&quot;;
text2._y = 52;</code></pre>
<p>This time, no <code>TextFormat</code> instance is needed.&nbsp; Instead, you&rsquo;re creating a <code>StyleSheet</code> object, which works very much like normal CSS.&nbsp; Note that in AS2, the <code>StyleSheet</code> class is accessible as a child of the <code>TextField</code> class, which is why both terms appear in the variable declaration and new expression.&nbsp; If you like, you can use the <code>import</code> directive and avoid the extra keystrokes, like this:</p>
<pre><code>import TextField.StyleSheet;
var css:StyleSheet = new StyleSheet();</code></pre>
<p>Either which way.</p>
<p>In any case, an arbitrarily named variable, <code>css</code>, is declared and set to an instance of the <code>StyleSheet</code> class.&nbsp; This gives <code>css</code> access to all the functionality defined by that class, which includes a <code>setStyle</code> method.&nbsp; The <code>StyleSheet.setStyle()</code> method accepts two parameters:&nbsp; a) the name of a CSS selector (in other words, what HTML element to style), and b) an object that contains the CSS style properties to use.&nbsp; In actual CSS, the <code>fontFamily</code> property would be <code>font-family</code>, and <code>fontSize</code> would be <code>font-size</code>.&nbsp; In ActionScript, it&rsquo;s close enough, but bear in mind that Flash only understands a very small subset of the CSS spec, just like it only understands a very small subset of the HTML spec.&nbsp; In that second parameter, the curly braces (<code>{}</code>) are a shortcut for the expression <code>new Object()</code>.&nbsp; Note that <code>fontFamily</code> is set, once again, to the Linkage identifier set earlier.</p>
<p>After that, the rest is pretty much the same.&nbsp; Note that this time the text field is created at depth 2, so that it doesn&rsquo;t overwrite the first one.&nbsp; Again, <code>embedFonts</code> is set to <code>true</code>.&nbsp; This time, because the text is formatted as HTML, the <code>TextField.html</code> property is set to <code>true</code>, and the <code>htmlText</code> property is used to supply the text.&nbsp; The <code>TextField._y</code> property is set to 52 just so the second text field appears beneath the first one.</p>
<h3>An answer, short and sweet, and how it works (AS3)</h3>
<p>ActionScript 3.0 works a bit differently, but the new approach makes sense in an elegant way.&nbsp; Check back to the AS2 version for information on the details that overlap, but here are the same two examples in AS3, with a brief summary of changes after each one.</p>
<pre><code>var fnt: BlackadderNormal = new BlackadderNormal();

var fmt:TextFormat = new TextFormat();
fmt.font = fnt.fontName;
fmt.size = 32;

var text1:TextField =&nbsp; new TextField();
text1.autoSize = &quot;left&quot;;
text1.embedFonts = true;
text1.defaultTextFormat = fmt;
text1.text = &quot;Lorem ipsum dolor sit amet.&quot;
addChild(text1);</code></pre>
<p>This time, the font is simply instantiated, just like any other object &mdash; because it <em>is</em> a <code>Font</code> instance.&nbsp; So &hellip; an arbitrarily named variable, <code>fnt</code>, is declared and set to an instance of <code>BlackadderNormal</code>, as supplied in the Class field of the Linkage Properties dialog.</p>
<p>The <code>TextFormat</code> instance works almost exactly the same, only this time, the <code>font</code> property is set to the <code>Font.fontName</code> property of the <code>fnt</code> (<code>BlackadderNormal</code>) instance.</p>
<p>The <code>TextField</code> instance is also very nearly the same.&nbsp; In ActionScript 3.0, you have the convenience of simply saying <code>new TextField()</code>, which is nice.&nbsp; Instead of the <code>setNewTextFormat()</code> method, you&rsquo;ve got a <code>defaultTextFormat</code> property (AS3 still has the <code>setTextFormat()</code> method for text already present; works pretty much the same).</p>
<p>The final line, <code>addChild(text1)</code>, adds the text field to the display list, so that it becomes visible.</p>
<p>Here&rsquo;s the <code>StyleSheet</code> update:</p>
<pre><code>var css:StyleSheet = new StyleSheet();
css.setStyle(&quot;p&quot;, {fontFamily:fnt.fontName, fontSize:32});
css.setStyle(&quot;a&quot;, {textDecoration:&quot;underline&quot;});

var text2:TextField =&nbsp; new TextField();
text2.autoSize = &quot;left&quot;;
text2.embedFonts = true;
text2.styleSheet = css;
text2.htmlText = &quot;&lt;p&gt;Lorem ipsum dolor
&lt;a href='http://www.quip.net/'&gt;sit amet&lt;/a&gt;.&lt;/p&gt;&quot;;
text2.y = 52;
addChild(text2);</code></pre>
<p>This time, the <code>StyleSheet</code> class stands alone.&nbsp; Here, the <code>fontFamily</code> property is set to <code>fnt.fontName</code>, rather than the Linkage identifier of AS2.&nbsp; The second text field is created just like the first, as a direct instantiation, and the <code>addChild()</code> method adds the second text field to the display list as well.&nbsp; Notice that in AS3, the <code>TextField._y</code> property loses its underscore, as is the case with all previously underscored properties.</p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-use-embedded-fonts-with-actionscript/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Progressive Enhancement with Flash</title>
		<link>http://www.quip.net/blog/2008/flash/progressive-enhancement-with-flash</link>
		<comments>http://www.quip.net/blog/2008/flash/progressive-enhancement-with-flash#comments</comments>
		<pubDate>Mon, 04 Feb 2008 16:00:48 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>Flash</category>
	<category>ActionScript 2.0</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/progressive-enhancement-with-flash</guid>
		<description><![CDATA[


One of the main tenets of good Web design is the principle of progressive enhancement. &#160;In general, the idea goes like this: &#160;make sure the user can access a web site&#8217;s essential content regardless of browser. &#160;It shouldn&#8217;t matter whether the user visits your site with Internet Explorer, Firefox, Safari, Netscape, or pick your favorite [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/" title="Flash"><img src="/blog/wp-images/icons/topic_flash.gif" align="left" width="18" height="18" alt="Flash" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p>One of the main tenets of good Web design is the principle of progressive enhancement. &nbsp;In general, the idea goes like this: &nbsp;make sure the user can access a web site&rsquo;s essential content regardless of browser. &nbsp;It shouldn&rsquo;t matter whether the user visits your site with Internet Explorer, Firefox, Safari, Netscape, or pick your favorite flavor. &nbsp;In fact, the site&rsquo;s content should be accessible even without JavaScript or peripheral plugins like Flash Player. &nbsp;Once the basics are covered, use CSS to enhance the visual design. &nbsp;Then, and only then, introduce the whizz-bang stuff &mdash; nifty rollovers, AJAX interactivity, Flash &mdash; and do it in a way that doesn&rsquo;t penalize users who don&rsquo;t have (or choose to disable) the needed machinery.</p>
<p>Sound pretty neat?&nbsp; I&rsquo;ve seen a few examples of this online and at conferences, so I delved into this topic myself to see how much fun it might be.&nbsp; Turns out it can be somewhat challenging, but definitely fun to see the results.&nbsp; I put together an example in order to explore the basic mechanics of this form of progressive enhancement &mdash; a slideshow SWF that takes its cue from the HTML in which it appears &mdash; and turned it into a three-part series on <a href="http://www.communitymx.com/" class="external" target="_blank">CommunityMX.com</a>.&nbsp; The first part is free and covers how to get the HTML from the Web page itself into the SWF.&nbsp; The follow-up articles go into how to parse that HTML in AS2 and AS3.&nbsp; CommunityMX offers free trial memberships, so if you aren&rsquo;t interested in becoming a subscriber, you can wait until the follow-up that interests you gets posted (not sure yet when that will be), then sign up for the trial membership.&nbsp; Of course, if you want to subscribe, that would be cool too!</p>
<p><a href="http://www.communitymx.com/content/article.cfm?cid=02395" class="external" target="_blank">http://www.communitymx.com/content/article.cfm?cid=02395</a></p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/progressive-enhancement-with-flash/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Working with asfunction in AS2 Class Files</title>
		<link>http://www.quip.net/blog/2008/flash/actionscript-20/working-with-asfunction-in-as2-class-files</link>
		<comments>http://www.quip.net/blog/2008/flash/actionscript-20/working-with-asfunction-in-as2-class-files#comments</comments>
		<pubDate>Wed, 30 Jan 2008 04:31:12 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>ActionScript 2.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/actionscript-20/working-with-asfunction-in-as2-class-files</guid>
		<description><![CDATA[
Flash Player has supported a limited subset of the HTML specification since version 6 &#8212; just set a text field&#8217;s htmlText property to an HTML-formatted string and you&#8217;re good to go.&#160; Fortunately, &#60;a&#62; (anchor) tags are among the supported few, which means you can even put working hyperlinks inside your text.&#160; Not only that, but [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<p>Flash Player has supported a limited subset of the HTML specification since version 6 &mdash; just set a text field&rsquo;s <code>htmlText</code> property to an HTML-formatted string and you&rsquo;re good to go.&nbsp; Fortunately, <code>&lt;a&gt;</code> (anchor) tags are among the supported few, which means you can even put working hyperlinks inside your text.&nbsp; Not only that, but Flash includes a special protocol, <code>asfunction</code>, that allows you to trigger functions from those hyperlinks, in case you prefer to do that instead of visiting URLs.&nbsp; ActionScript 3.0 uses a different approach, but if you&rsquo;re coding in AS1 or 2, just replace <code>http://someURL.com</code> with <code>asfunction:someFunction,someParam</code>, as described <a href="http://www.quip.net/blog/2006/flash/how-to-trigger-actionscript-with-hyperlinks" target="_blank">elsewhere on this blog</a>.&nbsp; If you&rsquo;re coding in timeline keyframes, it&rsquo;s all pretty straightforward.&nbsp; But <code>asfunction</code> can seemingly break when used in custom class files.&nbsp; Here&rsquo;s what&rsquo;s going on and how to fix it.&nbsp; <a id="more-161"></a></p>
<h3>A bit of backstory</h3>
<p>When a text field responds to an <code>asfunction</code> trigger, it looks for the specified function inside the movie clip that contains the text field in question (that is, the container object of that text field, or its parent).&nbsp; Again, if you&rsquo;re coding on the timeline, everything tends to fall into place.&nbsp; Imagine the following ActionScript 2.0 in a keyframe of the main timeline:</p>
<pre><code>function linkHandler(param:String):Void {
  // do something neat with the hyperlink
}

myTextField.htmlText = &quot;&lt;a href='asfunction:linkHandler,param'&gt;click me&lt;/a&gt;&quot;;</code></pre>
<p>When a user clicks the words &ldquo;click me&rdquo; in this text field, <code>asfunction</code> looks for a function named <code>linkHandler()</code> in the movie clip (aka the timeline) that contains this text field.&nbsp; And of course, there it is, in the very same keyframe.</p>
<p>In some custom class situations, the above concept still works without a hitch.&nbsp; If your class extends <code>MovieClip</code>, for example, then the class itself becomes the &ldquo;timeline&rdquo; that contains both the text field and <code>asfunction</code>&rsquo;s function.</p>
<pre><code>class CustomClass extends MovieClip {
  private var tf:TextField;
  public function CustomClass() {
    tf = this.createTextField(&quot;tf&quot;, 0, 0, 0, 100, 22);
    tf.html = true;
    tf.htmlText = &quot;&lt;a href='asfunction:linkHandler,clicked'&gt;click me&lt;/a&gt;&quot;;
  }
  private function linkHandler(param:String):Void {
    trace(param);
  }
}</code></pre>
<p>In the preceding code, an arbitrarily named property, <code>tf</code>, is declared and &mdash; inside the constructor function &mdash; set to an instance of the <code>TextField</code> class by way of the <code>MovieClip.createTextField()</code> method.&nbsp; This method succeeds here when invoked on the global <code>this</code> property because the class extends <code>MovieClip</code> (<em>i.e.</em>, it is a movie clip).&nbsp; In the following line, <code>tf</code>&rsquo;s <code>TextField.html</code> property is set to <code>true</code>, which allows it to accept HTML input, then its <code>htmlText</code> property is set to the same HTML-formatted string as before.&nbsp; Note that <code>asfunction</code> still points to a custom <code>linkHandler()</code> function, this time passing in the parameter &ldquo;clicked&rdquo; (a string).&nbsp; The private <code>linkHandler()</code> method (which could be public too, either way) hears <code>asfunction</code>&rsquo;s call when the hyperlink is clicked.&nbsp; All is still well &mdash; the Output panel traces the word &ldquo;clicked&rdquo; because <code>param</code>&rsquo;s value is that word.</p>
<p><strong>Note:</strong>&nbsp; To test the above class, you&rsquo;ll have to save the preceding code as a simple text file named CustomClass.as and put it into the same folder as a new FLA.&nbsp; Inside that FLA, draw a quick rectangle and convert it to a movie clip symbol.&nbsp; Right-click / Command-click the symbol in the Library and choose Linkage from the context menu.&nbsp; Check &ldquo;Export for ActionScript&rdquo; and type the name of the class (<code>CustomClass</code>) into the Class field.</p>
<h3>So what&rsquo;s the problem?</h3>
<p>The problem is, your class might not extend <code>MovieClip</code> (or extend anything!) at all.&nbsp; Which would be fine.&nbsp; There&rsquo;s no reason to extend <code>MovieClip</code>, or any class, unless the class you&rsquo;re writing actually <em>is an example of the class you&rsquo;re extending</em>.&nbsp; In the following code &mdash; almost identical &mdash; <code>asfunction</code> is no longer able to find its <code>linkHandler()</code> function:</p>
<pre><code>class CustomClass {
  private var tf:TextField;
  public function CustomClass(target:MovieClip) {
    tf = target.createTextField(&quot;tf&quot;, 0, 0, 0, 100, 22);
    tf.html = true;
    tf.htmlText = &quot;&lt;a href='asfunction:linkHandler,clicked'&gt;click me&lt;/a&gt;&quot;;
  }
  private function linkHandler(param:String):Void {
    trace(param);
  }
}</code></pre>
<p>What&rsquo;s the difference?&nbsp; This time, the class doesn&rsquo;t extend anything.&nbsp; It&rsquo;s instantiated in the main timeline &hellip;</p>
<pre><code>new CustomClass(this);</code></pre>
<p>&hellip; and fed a <code>MovieClip</code> instance &mdash; here, the global <code>this</code> property &mdash; as a parameter.&nbsp; The only reason for the parameter is so that the target reference inside the constructor function can be used in the very next line as the <code>MovieClip</code> instance required for the <code>createTextField()</code> method.&nbsp; The difference may be subtle, but when this class is instantiated, the text field becomes an immediate child of the main timeline.&nbsp; The problem is, the class itself &mdash; which contains the <code>linkHandler()</code> method &mdash; is <em>also</em> a child of the main timeline, which means <code>asfunction</code> is looking in the wrong place for <code>linkHandler()</code>.&nbsp; As always, <code>asfunction</code> is looking in the movie clip that contains the text field.&nbsp; Here, the movie clip is the main timeline, but the desired function is inside an object (this <code>CustomClass</code> instance) inside the main timeline.&nbsp; So close, but yet so far, eh?</p>
<h3>So what&rsquo;s a solution?</h3>
<p>The easiest way to fix the disconnect is to re-route the function, which can be done with a single line.&nbsp; Here&rsquo;s a quick look at the constructor function only:</p>
<pre><code>public function CustomClass(target:MovieClip) {
  tf = target.createTextField(&quot;tf&quot;, 0, 0, 0, 100, 22);
  tf.html = true;
  tf.htmlText = &quot;&lt;a href='asfunction:linkHandler,clicked'&gt;click me&lt;/a&gt;&quot;;
  tf._parent.linkHandler = this.linkHandler;
}</code></pre>
<p>The workaround here takes advantage of the fact that the <code>MovieClip</code> class is dynamic, which means it can have properties and methods added to it at runtime.&nbsp; In this case, a reference is made to the movie clip that contains the text field &mdash; <code>tf._parent</code> (the <code>TextField._parent</code> property of <code>tf</code>) &mdash; and then a new function is declared in that movie clip and set to the function defined inside the class.&nbsp; Because the constructor function accepts a <code>target</code> parameter, you could also use the following in this scenario, which means the exact same thing:</p>
<pre><code>target.linkHandler = this.linkHandler;</code></pre>
<p>It&rsquo;s interesting to note that <code>linkHandler()</code>, as defined in the class, is private, yet the twin function, dynamically set to a different <code>MovieClip</code> instance, is able to invoke this private method without any issues.&nbsp; That&rsquo;s just the broken way it works in AS2.&nbsp; It shouldn&rsquo;t do that, but hey, you&rsquo;re a developer.&nbsp; That makes you like a stage hand.&nbsp; You keep the magician&rsquo;s secrets.</p>
<h3>Is there a better solution?</h3>
<p>There is a better solution &mdash; though &ldquo;better&rdquo; is subjective, and you really only need the alternate approach if the scope of your function needs to reside inside the class.&nbsp; Under the current setup, if you replace <code>trace(param);</code> with <code>trace(this);</code>, you&rsquo;ll see _level0 in the Output panel.&nbsp; Why?&nbsp; Because the dynamically added function exists in the main timeline, so the this reference thinks that&rsquo;s where it is.&nbsp; (It only happens like this because the parent of the text field happens to be the main timeline.&nbsp; The point, however, is that this refers to an object that <em>isn&rsquo;t</em> the <code>CustomClass</code> instance.)</p>
<p>To re-route not only the function, but its scope, use the <code>Delegate</code> class as follows:</p>
<pre><code>import mx.utils.Delegate;
class CustomClass {
  private var tf:TextField;
  public function CustomClass(target:MovieClip) {
    tf = target.createTextField(&quot;tf&quot;, 0, 0, 0, 100, 22);
    tf.html = true;
    tf.htmlText = &quot;&lt;a href='asfunction:linkHandler,clicked'&gt;click me&lt;/a&gt;&quot;;
    tf._parent.linkHandler = Delegate.create(this, linkHandler);
  }
  private function linkHandler(param:String):Void {
    trace(param);
  }
}</code></pre>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/actionscript-20/working-with-asfunction-in-as2-class-files/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Moock&#8217;s Pitch for AS3 (It&#8217;s a Good One!)</title>
		<link>http://www.quip.net/blog/2008/flash/moocks-pitch-for-as3</link>
		<comments>http://www.quip.net/blog/2008/flash/moocks-pitch-for-as3#comments</comments>
		<pubDate>Tue, 22 Jan 2008 01:58:38 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>Flash</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/moocks-pitch-for-as3-its-a-good-one</guid>
		<description><![CDATA[

O&#8217;Reilly and Adobe have teamed up on a new website dedicated to Rich Internet Applications (RIA).&#160; Among the first featured articles is a comparative look at ActionScript 3.0 and its predecessors, 2.0 and 1.0, in which Colin Moock offers a number of solid insights (and encouragements) toward learning the new language.
http://www.insideria.com/2008/01/actionscript-30-is-it-hard-or.html
]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/" title="Flash"><img src="/blog/wp-images/icons/topic_flash.gif" align="left" width="18" height="18" alt="Flash" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p><a href="http://www.oreilly.com/" class="external" target="_blank">O&rsquo;Reilly</a> and <a href="http://www.adobe.com/" class="external" target="_blank">Adobe</a> have teamed up on a new website dedicated to Rich Internet Applications (RIA).&nbsp; Among the first featured articles is a comparative look at ActionScript 3.0 and its predecessors, 2.0 and 1.0, in which <a href="http://www.moock.org/" class="external" target="_blank">Colin Moock</a> offers a number of solid insights (and encouragements) toward learning the new language.</p>
<p><a href="http://www.insideria.com/2008/01/actionscript-30-is-it-hard-or.html" class="external" target="_blank">http://www.insideria.com/2008/01/actionscript-30-is-it-hard-or.html</a></p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/moocks-pitch-for-as3/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>How to Play a Timeline Backwards (with Easing!)</title>
		<link>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-play-timeline-backwards</link>
		<comments>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-play-timeline-backwards#comments</comments>
		<pubDate>Thu, 03 Jan 2008 04:48:57 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>ActionScript 2.0</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2008/flash/actionscript-20/how-to-play-timeline-backwards</guid>
		<description><![CDATA[

Reader James Colvin wrote me in mid-December to ask if I had any thoughts on playing a timeline backwards.&#160; As it turns out, this question comes up every now and then on the Adobe forums, where longtime regular kglad usually posts his very handy custom function in reply.&#160; In kglad&#8217;s version, the MovieClip.nextFrame() and prevFrame() [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/actionscript-20/" title="ActionScript 2.0"><img src="/blog/wp-images/icons/topic_actionscript-20.gif" align="left" width="18" height="18" alt="ActionScript 2.0" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p>Reader James Colvin wrote me in mid-December to ask if I had any thoughts on playing a timeline backwards.&nbsp; As it turns out, this question comes up every now and then on the Adobe forums, where longtime regular kglad usually posts his <a href="http://groups.google.com/group/macromedia.flash.actionscript/browse_frm/thread/33a6f20a59eea158/68e9c0b30a4992a7?hl=en&amp;lnk=gst&amp;q=timeline+backwards+kglad#68e9c0b30a4992a7" class="external" target="_blank">very handy custom function</a> in reply.&nbsp; In kglad&rsquo;s version, the <code>MovieClip.nextFrame()</code> and <code>prevFrame()</code> methods are used in cahoots with <code>setInterval()</code> to accomplish the goal.&nbsp; He often assigns the function to the <code>Object.prototype</code> property of the <code>MovieClip</code> class, which makes the new functionality available to all movie clips (a pre-AS3 technique).</p>
<p>My initial reaction was to search the forums and send back a link, but James&rsquo; question had an interesting twist:&nbsp; could this non-standard timeline movement include easing?&nbsp; Wow, what a cool challenge!&nbsp; So I thought about it off and on over the holidays, and a neat solution occurred to me just this morning.&nbsp; <a id="more-159"></a></p>
<h3>A handful of answers, not so short, but sweet</h3>
<p>What follows is my personal take on a solution, presented in three formats:&nbsp; two versions in AS2 and one version in AS3. &nbsp;Why the two versions of the AS2?&nbsp; Frankly, while both are fairly short, one is even simpler than the other.&nbsp; The only thing is, it involves an AS2 feature some would argue as inefficient.&nbsp; (To my thinking, it would only be a problem in complex FLAs, but I&rsquo;ll supply both and you can take your pick.)&nbsp; The second AS2 version is close enough to AS3 in syntax that it acts as a good introduction to the new language.</p>
<p>The trick to my approach, in all cases, is to use the native <code>Tween</code> class for the easing, then take note of a numeric property adjusted by the programmatic tween.</p>
<h4>ActionScript 2.0, Object.watch() approach</h4>
<pre><code>import mx.transitions.Tween;
import mx.transitions.easing.Bounce;

stop();

var w:Object = new Object();
w.prop = 0;
w.watch(&quot;prop&quot;, onUpdate);

function onUpdate(prop:String, oldVal:Number, newVal:Number):Number {
  gotoAndStop(Math.floor(newVal));
  return newVal;
}

var tween:Tween = new Tween(w, &quot;prop&quot;, Bounce.easeIn, 58, 2, 4, true);</code></pre>
<p>So what&rsquo;s going on?&nbsp; This particular batch of code may look a bit more cryptic than my usual examples, I realize, but once you see what&rsquo;s going on, it&rsquo;s not too bad.</p>
<p>The first two lines import the <code>Tween</code> and <code>Bounce</code> classes.&nbsp; There are a number of easing classes in AS2, and you&rsquo;ll find them in the Components Language Reference rather than the ActionScript 2.0 Language Reference (&hellip; <code>Regular</code> approximates a standard timeline tween, <code>Strong</code> is stronger than that, <code>Bounce</code> gives you what it says [a bit of bounce!], <code>None</code> gives you a tween without easing, and there are a few more; search &ldquo;About easing classes and methods&rdquo;).</p>
<p>Next, a simple <code>stop()</code> halts the timeline.&nbsp; Keep in mind, this code appears in frame 1 of the main timeline. so James&rsquo; animation (and yours) will start on frame 1 as well and extend as far as you need &mdash; in this example, the animation (a series of standard timeline tweens) extends through frame 58.</p>
<p>Now it gets a bit interesting.&nbsp; Check out these three lines again:</p>
<pre><code>var w:Object = new Object();
w.prop = 0;
w.watch(&quot;prop&quot;, onUpdate);</code></pre>
<p>An arbitrarily named variable, <code>w</code>, is declared and set to an instance of the <code>Object</code> class (to me, &ldquo;w&rdquo; stands for &ldquo;watcher,&rdquo; but name your variable what you like).&nbsp; This <code>w</code> object is given a property named <code>prop</code>, which is set to 0.&nbsp; As we continue, this property will be updated by the <code>Tween</code> class to indicate which frame in the main timeline to travel to.&nbsp; Finally, the <code>Object.watch()</code> method is invoked on <code>w</code>.&nbsp; This is the line that some will find inefficient, so keep that in mind as you experiment.&nbsp; The reason is, <code>Object.watch()</code> causes Flash Player to keep a constant eye on the value of <code>w.prop</code> (or whatever property you feed in), which sets up a tight loop that may monitor the property longer than you need it to.&nbsp; For the sake of this example &mdash; and, I would argue, for many simple implementations &mdash; you&rsquo;re probably fine.&nbsp; What <code>Object.watch()</code> does is trigger a custom function (provided by you) whenever the given property (here, <code>prop</code>) changes.&nbsp; Note that the property is provided in quotes, and the function is provided without the parentheses.</p>
<p>And what might this custom function be?&nbsp; Check it out.&nbsp; The syntax is a bit weird, but for as useful as <code>Object.watch()</code> is, the weird is worth it.</p>
<pre><code>function onUpdate(prop:String, oldVal:Number, newVal:Number):Number {
  gotoAndStop(Math.floor(newVal));
  return newVal;
}</code></pre>
<p>The function receives three incoming parameters:&nbsp; <code>prop</code>, <code>oldVal</code>, and <code>newVal</code>.&nbsp; Here, <code>prop</code> is the property (referred to as a string) that was passed in earlier by <code>w.watch()</code>.&nbsp; <code>oldVal</code> is <em>the value of that property</em> &mdash; before it gets changed by whatever force changes it (for us, that force is the <code>Tween</code> instance).&nbsp; <code>newVal</code> is the value after the property gets changed.&nbsp; Inside the function, a simple <code>gotoAndStop()</code> tells the main timeline to proceed to a frame determined by the <code>prop</code> property (in turn determined by the <code>Tween</code> instance).&nbsp; The <code>Math.floor()</code> part rounds down the number, in case it&rsquo;s not an integer.&nbsp; If you run the tween from, say, 58 to 2, <code>prop</code> will update from 58 downwards to 2.&nbsp; The exact numbers will change depending on easing, and the timeline will travel to each of those frames (backwards or forwards) in response.&nbsp; The return statement is needed to ensure that <code>w</code>&rsquo;s <code>prop</code> property actually updates.</p>
<p>Finally, we come to the tween:</p>
<pre><code>var tween:Tween = new Tween(w, &quot;prop&quot;, Bounce.easeIn, 58, 2, 4, true);</code></pre>
<p>An arbitrarily named variable, tween, is declared and set to an instance of the Tween class.&nbsp; A number of parameters feed the constructor, and here&rsquo;s what they mean:</p>
<ul>
<li><code>w</code> is  our <code>Object</code> instance;</li>
<li><code>&quot;prop&quot;</code> is <code>w.prop</code>, again indicated as a string;</li>
<li>the  expression <code>Bounce.easeIn</code> is a static method of the <code>Bounce</code> class (a  standard ease out would be <code>Regular.easeOut</code>; again, check the Help docs);</li>
<li><code>58</code> is  the starting position, frame 58;</li>
<li><code>2</code> is  the destination position, frame 2;</li>
<li><code>4</code> means &ldquo;do this tween over a period of four seconds&rdquo;;</li>
<li><code>true</code> means &ldquo;yes, seconds&rdquo; (if this one was <code>false</code>, the previous parameter would  refer to four frames, rather than seconds &mdash; that would be much quicker!)</li>
</ul>
<p>And that&rsquo;s it.&nbsp; The neat thing is, the <code>Tween</code> class gives you a number of useful methods and events, so you can respond to the completion of a tween (for example) by repeating it, or repeating it backwards (and so on):</p>
<pre><code>// An example, using Tween.onMotionFinished
// and Tween.yoyo()
tween.onMotionFinished = function():Void {
  this.yoyo();
}</code></pre>
<p>Why tween from frame 58 down to 2 instead of down to 1?&nbsp; As soon as the playhead hits frame 1 again, all this ActionScript we&rsquo;ve been discussing gets triggered all over again.</p>
<h4>ActionScript 2.0, custom event approach</h4>
<p>If you prefer not to use <code>Object.watch()</code>, here&rsquo;s another approach.&nbsp; It requires a small custom class file in addition to the keyframe code, so we&rsquo;ll look at both.&nbsp; Note how closely the keyframe code matches the previous version:</p>
<pre><code>stop();</code></pre>
<pre><code>import mx.transitions.Tween;
import mx.transitions.easing.Bounce;

var w:Watcher = new Watcher();
w.addEventListener(&quot;update&quot;, onUpdate);

function onUpdate(evt:Object):Void {
  gotoAndStop(Math.floor(Number(evt.prop)));
}

var tween:Tween = new Tween(w, &quot;prop&quot;, Bounce.easeIn, 58, 2, 4, true);</code></pre>
<p>Instead of the <code>Object.watch()</code> portion &mdash; in fact, instead of the <code>Object</code> instance at all &mdash; we now have an instance of a custom <code>Watcher</code> class.&nbsp; This instance, <code>w</code> (same variable name as before, for convenience) features a <code>Watcher.update</code> event, which is handled to perform a custom <code>onUpdate()</code> function.&nbsp; This happens via the <code>addEventListener()</code> method.&nbsp; The <code>onUpdate()</code> function again tells the main timeline to <code>gotoAndStop()</code>, only this time there&rsquo;s only a single parameter, <code>evt</code>.&nbsp; The <code>evt</code> parameter carries with it a property named <code>prop</code>, and that property tells the timeline which frame to visit.&nbsp; Same concept, different approach.</p>
<p>Here&rsquo;s the code for the <code>Watcher</code> class.&nbsp; This goes in a text file external to the FLA.</p>
<pre><code>import mx.events.EventDispatcher;
class Watcher {
  private var _prop:Object;
  public var addEventListener:Function;
  public var removeEventListener:Function;
  private var dispatchEvent:Function;
  public function Watcher() {
    EventDispatcher.initialize(this);
  }
  public function get prop():Object {
    return _prop;
  }
  public function set prop(o:Object):Void {
    _prop = o;
    dispatchEvent({type:&quot;update&quot;, prop:o});
  }
}</code></pre>
<p>The native <code>EventDispatcher</code> class is imported, so that the <code>Watcher</code> class can dispatch events.&nbsp; The class is declared, and a handful of properties are declared:&nbsp; <code>_prop</code> (should be familiar by now) and three functions, which are provided by the <code>EventDispatcher.initialize()</code> method just inside the constructor.&nbsp; (See &ldquo;<a href="http://www.quip.net/blog/2006/flash/actionscript-20/how-to-raise-events-eventdispatcher" target="_blank">How to Raise Custom Events (EventDispatcher)</a>&rdquo; for details on this procedure in AS2.)</p>
<p>A pair of getter/setter functions allow <code>_prop</code> to be changed from outside the class (in this case, by the <code>Tween</code> instance), and note that the <code>set</code> function dispatches the event, of type &ldquo;update&rdquo; and with a property <code>prop</code> whose value is determined by the incoming value (again, by the tween).&nbsp; In the keyframe code that uses <code>addEventListener()</code>, the <code>gotoAndStop()</code> line that uses <code>evt.prop</code> gets its value from <code>prop</code> as seen here.</p>
<h4>ActionScript 3.0</h4>
<p>AS3 no longer supports <code>Object.watch()</code>, so the only possibility for an alternate approach involves a custom event.&nbsp; This version&rsquo;s keyframe portion matches the AS2 version almost 100%.&nbsp; (See <a href="http://justin.everett-church.com/index.php/2007/06/13/wiwtw-so-long-objectwatch/" class="external" target="_blank">Justin Everett-Church&rsquo;s blog</a> for reasons why AS3 crossed this feature of its list.)&nbsp; Here&rsquo;s the code:</p>
<pre><code>import fl.transitions.Tween;
import fl.transitions.easing.Bounce;

stop();

var w:Watcher = new Watcher();
w.addEventListener(WatcherEvent.UPDATE, onUpdate);

function onUpdate(evt:WatcherEvent):void {
  gotoAndStop(Math.floor(evt.prop));
}

var tween:Tween = new Tween(w, &quot;prop&quot;, Bounce.easeIn, 58, 2, 4, true);</code></pre>
<p>The import statements change just a tad (the package is now <code>fl.transitions</code>, instead of <code>mx.transitions</code>).&nbsp; The <code>addEventListener()</code> line uses a static <code>WatcherEvent</code> constant instead of the string &ldquo;update&rdquo;.&nbsp; The <code>onUpdate()</code> function only changes in that its incoming parameter is of type <code>WatcherEvent</code>, rather than <code>Object</code>.&nbsp; What this should tell you is that we need two custom classes for the AS3 version:&nbsp; <code>Watcher</code> and <code>WatcherEvent</code>.&nbsp; Here they are:</p>
<pre><code>package {
  import WatcherEvent;
  import flash.events.EventDispatcher;
  public class Watcher extends EventDispatcher {
    private var _prop:Object;
    public function Watcher() {};
    public function get prop():Object {
      return _prop;
    }
    public function set prop(o:Object):void {
      _prop = o;
      dispatchEvent(new WatcherEvent(WatcherEvent.UPDATE, o));
    }
  }
}</code></pre>
<p>Here, the class actually <em>extends</em> <code>EventDispatcher</code>, rather than using it as a mix-in.&nbsp; The syntax changes a bit, but the concept is still the same.&nbsp; Here, the constructor is empty.&nbsp; This time, the <code>set</code> function dispatches a new instance of <code>WatcherEvent</code>, along with the updated value, <code>o</code>.</p>
<pre><code>package {
  import flash.events.Event;
  public class WatcherEvent extends Event {
    public static const UPDATE:String = &quot;update&quot;;
    private var _prop:Object = new Object();
    public function WatcherEvent(type:String, prop:Object) {
      super(type);
      _prop = prop;
    }
    public function get prop():Object {
      return _prop;
    }
    public override function clone():Event {
      return new WatcherEvent(type, _prop);
    }
  }
}</code></pre>
<p><code>WatcherEvent</code> extends the <code>Event</code> class, then provides the custom event, <code>update</code>, which is stored as a static constant (<code>UPDATE</code>) of this class.&nbsp; See Trevor McCauley&rsquo;s &ldquo;<a href="http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html" class="external" target="_blank">Introduction to event handling in ActionScript 3.0</a>&rdquo; for an in-depth look at raising custom events in AS3.</p>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2008/flash/actionscript-20/how-to-play-timeline-backwards/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Flash CS3 (9.0.2) Supports New Video and Audio Codecs; FLVPlayback Works, But &#8230;</title>
		<link>http://www.quip.net/blog/2007/flash/flashcs3-update-supports-new-video-and-audio-codecs</link>
		<comments>http://www.quip.net/blog/2007/flash/flashcs3-update-supports-new-video-and-audio-codecs#comments</comments>
		<pubDate>Thu, 13 Dec 2007 19:56:31 +0000</pubDate>
		<dc:creator>David Stiller</dc:creator>
		
	<category>Flash</category>
	<category>ActionScript 3.0</category>
		<guid isPermaLink="false">http://www.quip.net/blog/2007/flash/flash-cs3-902-supports-new-video-and-audio-codecs-flvplayback-works-but</guid>
		<description><![CDATA[

If you want to be able to deliver non-FLV video in Flash (how cool is that?!), head over to the Adobe Flash Support Center and download the Adobe Flash Player Update for Flash CS3 Professional (9.0.2).&#160; The result?&#160; At runtime, you&#8217;ll be able to load MOV and MP4 files encoded in the H.264 hi-def codec.&#160; [...]]]></description>
			<content:encoded><![CDATA[<a href="http://www.quip.net/blog/category/flash/" title="Flash"><img src="/blog/wp-images/icons/topic_flash.gif" align="left" width="18" height="18" alt="Flash" class="icon" /></a>
<a href="http://www.quip.net/blog/category/flash/actionscript-30/" title="ActionScript 3.0"><img src="/blog/wp-images/icons/topic_actionscript-30.gif" align="left" width="18" height="18" alt="ActionScript 3.0" class="icon" /></a>
<p>If you want to be able to deliver non-FLV video in Flash (how cool is that?!), head over to the <a href="http://www.adobe.com/support/flash/downloads.html" class="external" target="_blank">Adobe Flash Support Center</a> and download the Adobe Flash Player Update for Flash CS3 Professional (9.0.2).&nbsp; The result?&nbsp; At runtime, you&rsquo;ll be able to load MOV and MP4 files encoded in the H.264 hi-def codec.&nbsp; This is in addition to the usual FLV, and we&rsquo;re talking Flash Player 9 only (specifically, version 9.0.115.0 or higher).&nbsp; You&rsquo;ll also be able to load M4A audio instead of only MP3.&nbsp; The upgrade to the IDE allows you to view such content as you test and debug SWFs right in the authoring environment.&nbsp; I&rsquo;ve already installed and tested, and the update works &hellip; but I have seen one stumbling block.&nbsp; You can work around it &mdash; in fact, you can work right through it &mdash; but it isn&rsquo;t especially obvious.&nbsp; <a id="more-158"></a></p>
<h3>Using the FLVPlayback component</h3>
<p>If you&rsquo;re coding up your video content with a Video object and <code>NetConnection</code>/<code>NetStream</code>, you won&rsquo;t see any issues, but if you&rsquo;re a fan of FLVPlayback, you&rsquo;ll have to fly blind during a few steps that are normally straightforward.&nbsp; Here&rsquo;s what happens for me in Windows XP (and perhaps your mileage may vary):</p>
<p>I drag an instance of FLVPlayback from the Components panel to the Stage.&nbsp; I select the Parameters tab of the Property inspector and double-click source so I can browse to one of the new video file types.&nbsp; This opens a <kbd>Content Path</kbd> dialog with a button that browses for files.&nbsp; I click that button to locate an MP4 file, which opens a <kbd>Browse for FLV file</kbd> dialog, but nothing shows &mdash; even though I&rsquo;m in the folder that has such files.&nbsp; The reason nothing shows is because the <em>Files of type</em> list box in this dialog only has an entry for <em>*.flv</em> files.&nbsp; To get around that, I type *.* into the <em>File name</em> field and hit <kbd>Enter</kbd>.&nbsp; Now I can see my MP4s (and every other file type in that folder).&nbsp; I double click my MP4.</p>
<p>Flash chews on that for a while.&nbsp; Honestly &hellip; maybe a minute and a half.&nbsp; Finally, Flash tells me &ldquo;Failed to load FLV: [<em>name of file</em>].&rdquo;&nbsp; This surprises me, because, well, I didn&rsquo;t browse for an FLV.&nbsp; <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> &nbsp; But I forged ahead anyway and clicked <kbd>OK</kbd>.&nbsp; I selected <kbd>Control &gt; Test Movie</kbd> to see if the association succeeded anyway.&nbsp; It did.</p>
<p>So it does work.&nbsp; But Flash plays a bit of hide-and-seek with you while it gets you there.&nbsp; My take on it is, roll with the flow.&nbsp; <img src='http://www.quip.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &nbsp; It&rsquo;s too bad that the manual association doesn&rsquo;t seem to realize the new possibilities, but if it really gets on your nerves, you can always just set that source parameter via ActionScript 3.0:&nbsp; just give your FLVPlayback component an instance name in the Property inspector, such as <code>videoPlayer</code>, and set its FLVPlayback.source property:</p>
<pre><code>videoPlayer.source = &quot;coolNewFormat.mp4&quot;;</code></pre>
]]></content:encoded>
			<wfw:commentRSS>http://www.quip.net/blog/2007/flash/flashcs3-update-supports-new-video-and-audio-codecs/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
