<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>nodename</title>
	<atom:link href="http://nodename.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://nodename.com/blog</link>
	<description></description>
	<pubDate>Mon, 22 Sep 2008 15:50:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Distributing Points on the Sphere</title>
		<link>http://nodename.com/blog/2008/09/19/distributing-points-on-the-sphere/</link>
		<comments>http://nodename.com/blog/2008/09/19/distributing-points-on-the-sphere/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 05:25:45 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=64</guid>
		<description><![CDATA[Distributing points on the sphere by electrostatic repulsion, ported from Bulatov&#8217;s C++ code.


<object	type="application/x-shockwave-flash"
			data="/wpEmbeds/SpherePoints/SpherePointsApp.swf"
			width="600"
			height="600">
	<param name="movie" value="/wpEmbeds/SpherePoints/SpherePointsApp.swf" />
</object>

References
Distributing points on the sphere
Symmetries of configurations of charges on a sphere
]]></description>
			<content:encoded><![CDATA[<p>Distributing points on the sphere by electrostatic repulsion, ported from <a href="http://www.math.niu.edu/~rusin/known-math/96/repulsion">Bulatov&#8217;s C++ code</a>.</p>
<p><center><br />

<object	type="application/x-shockwave-flash"
			data="/wpEmbeds/SpherePoints/SpherePointsApp.swf"
			width="600"
			height="600">
	<param name="movie" value="/wpEmbeds/SpherePoints/SpherePointsApp.swf" />
</object><br />
</center></p>
<h3>References</h3>
<p><a href="http://www.maths.unsw.edu.au/school/articles/me100.html">Distributing points on the sphere</a></p>
<p><a href="http://article.pubs.nrc-cnrc.gc.ca/ppv/RPViewDoc?issn=1480-3291&#038;volume=66&#038;issue=9&#038;startPage=2161">Symmetries of configurations of charges on a sphere</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/09/19/distributing-points-on-the-sphere/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dynamic Programming in AS3</title>
		<link>http://nodename.com/blog/2008/09/19/dynamic-programming-in-as3/</link>
		<comments>http://nodename.com/blog/2008/09/19/dynamic-programming-in-as3/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 04:21:22 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=62</guid>
		<description><![CDATA[AS3 provides a built-in data structure that you can use for dynamic programming to supplement your application&#8217;s functionality.  This structure is the prototype chain.
What&#8217;s the prototype chain?
To answer that, let&#8217;s start by forgetting about user-defined Classes for the moment, and looking at some older mechanisms that have been common to ActionScript and JavaScript since [...]]]></description>
			<content:encoded><![CDATA[<p>AS3 provides a built-in data structure that you can use for dynamic programming to supplement your application&#8217;s functionality.  This structure is the <em>prototype chain</em>.</p>
<p>What&#8217;s the prototype chain?</p>
<p>To answer that, let&#8217;s start by forgetting about user-defined Classes for the moment, and looking at some older mechanisms that have been common to ActionScript and JavaScript since AS1.</p>
<h3>Functions and the <strong>new</strong> operator</h3>
<p>Whenever a Function is created, the Function constructor also creates a new generic Object.  The Function gets a property named &#8220;prototype&#8221; that refers to this new Object.  This generic object is a default that you can change by setting the Function&#8217;s prototype to a different object, as we shall see.</p>
<p>Now when you the programmer want to create a new custom object, you can do it by invoking a function preceded by the <strong>new</strong> operator:</p>
<pre>

var myObject:Object = new SomeFunction();
</pre>
<p>When used with the <strong>new</strong> operator, a function is known as a <em>constructor function</em> (not to be confused with a constructor method!), or just a <em>constructor</em>.  The <strong>new</strong> operator modifies the way the function works in three ways:</p>
<p>1.  Ordinarily, the keyword <strong>this</strong>, when used inside a function, is bound to the global object.  When a function is invoked with <strong>new</strong>, a new Object is created, and <strong>this</strong> refers to that Object.  (This is like using the <strong>Function.apply()</strong> method.)</p>
<p>2.  The new Object gets a property named <strong>__proto__</strong> that refers to the constructor&#8217;s  prototype.  The Object&#8217;s __proto__ is a basis for inheritance &#8212; a way to customize the new Object &#8212; as described in the next section.</p>
<p>3.  If the function returns a non-object value, then when invoked with <strong>new</strong>, <strong>this</strong> (the new object) is returned instead.  If the function returns an object, that object becomes the value of the <strong>new</strong> expression, and <strong>this</strong> is discarded.</p>
<p>You can see that use of the <strong>new</strong> operator causes the function to behave quite differently.  So although there is no formal difference between a constructor function and any other function, the code you put in them will be quite different.  Constructor functions are conventionally named with an upper-case first letter as a reminder of the distinction.</p>
<h3>Prototype Inheritance</h3>
<p>When the runtime encounters a reference to a property of an object, as in <strong>myObject.var</strong> or <strong>myObject.func()</strong>, it naturally looks for the property in the object itself.  But if it doesn&#8217;t find it there, it looks in the object&#8217;s __proto__  (the prototype object).  Note that the prototype is an object like any other, and when it was created it got a __proto__ reference to a prototype, too.  Thus we have a prototype chain.  The chain has an end, though, because eventually we reach an Object that was created with <strong>new Object();</strong> or with an Object literal in curly braces, and the next object in the chain is the unique and final Object.prototype.  The runtime will search the chain until it finds the desired property or until it reaches Object.prototype.</p>
<p>The constructor&#8217;s prototype appears as the first element in the new Object&#8217;s prototype chain.  Thus, although no new Class is created, and the new object&#8217;s type is simply Object, this particular prototype has a tangible effect on the object&#8217;s behavior that is shared with all other objects that have the same prototype chain.  The prototype constitutes the Object&#8217;s <em>pseudoclass</em>. (The pseudoclass is often called &#8220;class&#8221; for simplicity when speaking of AS1 or JavaScript).  The <strong>instanceof</strong> operator works by traversing the prototype chain.</p>
<p>If properties of the prototype are modified, the object naturally inherits the new values.  If the object is directly assigned a property of a given name, that property will mask (or &#8220;shadow&#8221;) a property with the same name on the prototype.</p>
<p>If the constructor function is assigned a different prototype, then objects subsequently created with <strong>new</strong> SomeFunction() will inherit from the new prototype, but old objects continue to inherit from the original prototype.  Normally, if a pseudoclass is to inherit from another, the programmer will explicitly set the constructor&#8217;s prototype before creating any objects from it:
<pre>

function Shape() { this.handle = "shape"; }    // leave default prototype; shape objects will inherit from an Object

function Circle(r) { this.radius = r; }
Circle.prototype = new Shape();    // circle objects will inherit from a shape object

var circle = new Circle();    // circle will have the "handle" property
</pre>
<p>Note that the constructor function itself, being an object of class Function, inherits properties not from its prototype, but rather from its own __proto__, which is a reference to Function.prototype.</p>
<p>The programmer can also replace an object&#8217;s __proto__, changing its pseudoclass at will.  This may seem a bit out there, but it was actually the way to set the document type in AS1 and AS2.</p>
<p>All of the above applies equally to JavaScript and to all versions of ActionScript.  But ActionScript 3 imposes certain restrictions:<br />
1.  Neither package-level functions nor class methods may be used as constructor functions.  So a constructor function must be defined outside a package statement, within a method, or in a frame script.</p>
<p>2.  The __proto__ property of an object is not accessible to the programmer.</p>
<p>3. In strict mode, passing any parameters to a constructor function that is declared outside a package statement is a compile-time error, even if the function is declared to accept parameters.  (This is not the case when the function is defined within the method where it is invoked, or when the function is invoked through a reference defined within the method where it is invoked.)  This appears to be a mistake on the part of the compiler.</p>
<h3>Bringing it Up to Date</h3>
<p>First let&#8217;s note that the above stuff is dynamic in that it can all be changed at runtime.  Good times.  Of course it comes with a price: slower performance, and everything is public and untyped.</p>
<p>Now let&#8217;s bring back Classes, and see how they behave compared to these ancestral features of the language.</p>
<p>In AS2, classes were grafted onto the underlying prototype-based language.  Class syntax was just that:  syntax allowing programmers to ignore the implementation and submit to certain restrictions at compile time.  But prototype-based inheritance was all there really was in the VM, and its full power (and dangers) were still available at run time.  AS3 is different.  It has true classes and class-based inheritance.</p>
<p>When a Class is created, it gets a prototype object just as a Function does.  The instances created by <strong>new ClassName()</strong> inherit from the class prototype, and you can add, delete, or change properties of the class prototype, and those properties are all public and untyped, too.  The difference is that you cannot replace a Class&#8217;s prototype; the prototype chain (or tree, really) is isomorphic to the class hierarchy and is immutable.</p>
<p>Another difference, of course, is that you normally define fixed instance properties (variables and methods) when you write a Class.  The fixed properties are placed in the class&#8217;s <em>traits</em> object, and the runtime checks the traits object for a property before searching the prototype chain; that is, class inheritance takes precedence over prototype inheritance.  Fixed properties are typed, need not be public, and are found more quickly.  As implied by the name, fixed properties cannot be deleted.</p>
<p>(You might think that the runtime has to search up the class hierarchy to find fixed properties that are inherited from a superclass.  But in fact in AS3 these are copied down into the current class&#8217;s traits object, so they are found even more quickly.)</p>
<p>A Class&#8217;s constructor method is much like a constructor function.  But you don&#8217;t get the chance to return anything from a constructor method, so <strong>new ClassName()</strong> will always return the new object.  In fact you can&#8217;t even get a reference to the constructor method as a Function object, so you have no opportunity to try to invoke it without the <strong>new</strong> operator.  This suggests that in AS3 the constructor method is still in a sense the same thing as the class.</p>
<p>I&#8217;m not going to get into the fascinating topic of <em>closures</em> right now, except to note that every function in AS and JS is a closure, so named because the scope of an inner function in these languages encloses the parameters and variables of the functions they are defined within.  </p>
<blockquote><p>Closures are first-class citizens of ActionScript.  Every method in your class is a closure.  That&#8217;s how it knows the instance variables of the class.  Essentially every class is a big closure.  You can write a function with closures inside that would be very much a class for all practical purposes.</p></blockquote>
<p>&#8211; the RIA Book, p. 88</p>
<p>That last sentence describes what Douglas Crockford does in <strong>JavaScript: The Good Parts</strong> &#8212; what classical programmers may call simulating classes, and what Crockford calls better than classes.</p>
<h3>Should you use dynamic features in your programming?</h3>
<p>Sure, occasionally.  Obviously, if you want to, you can create objects with a constructor function, then replace the constructor&#8217;s prototype, and create more objects with it.</p>
<p>If you think up a good use case for doing that, please let me know.</p>
<p>In fact, you should probably not use constructor functions at all.  Crockford advises against them &#8212; and this in a language without user-defined classes!  The lack of privacy is just one of the reasons he cites.</p>
<p>But you should definitely consider placing properties on class prototypes where appropriate.</p>
<p>UPDATE 22 Sept 2008:  I now believe that the <a href="http://en.wikipedia.org/wiki/Strategy_pattern#ActionScript_3">STRATEGY pattern</a> is a better solution for this use case (see the comments on this post), but I&#8217;m leaving this example up as a demonstration of the technique for those who are interested.</p>
<p>Consider a game character who has a number of private methods; he wants to execute the appropriate method when he arrives at various targets, depending on the type of the target. He may have one method that he must execute whenever he arrives at any Flower, whether it be a Daisy or a Pansy or whatever derived class instance. If I set</p>
<p>Flower.prototype.myTask = flowerTask;</p>
<p>then on arrival, my character can call</p>
<p>target["myTask"]();</p>
<p>and the flowerTask is the one that will be executed if the target is any Flower derivative.</p>
<p>Here&#8217;s a little example;  I&#8217;ve set properties on Target1.prototype and on Target2.prototype but not on Target3.prototype.  Target2A is derived from Target2.  Click on the targets:</p>
<p><center></p>
<p />

<object	type="application/x-shockwave-flash"
			data="/wpEmbeds/PrototypeUseCase/PrototypeUseCase.swf"
			width="400"
			height="200">
	<param name="movie" value="/wpEmbeds/PrototypeUseCase/PrototypeUseCase.swf" />
</object><br />
</center></p>
<p>An archive of the FlexBuilder project with sources and fuller explanation is <a href="http://nodename.com/wpEmbeds/PrototypeUseCase/PrototypeUseCase.zip">here</a>.</p>
<p>I like to think of prototype properties as a kind of enhanced reverse Dictionary; if you think of the property name as the data structure, and the object instances as keys &#8212; as though you were saying &#8220;myTask&#8221;[target] in the last example &#8212; then, when a certain value is set on a class&#8217;s prototype, you retrieve that value through any index that&#8217;s an <strong>instanceOf</strong> that class.</p>
<h3>Further Reading</h3>
<p><a href="http://livedocs.adobe.com/specs/actionscript/3/wwhelp/wwhimpl/js/html/wwhelp.htm?href=as3_specification43.html">AS3 Language Specification</a></p>
<p>Adobe Flex 3 Help -> Programming ActionScript 3.0 -> Object-oriented programming in ActionScript -> Advanced topics</p>
<p>Colin Moock: <a href="http://www.amazon.com/Essential-ActionScript-3-0/dp/0596526946">Essential ActionScript 3.0</a> Chapter 15, &#8220;Dynamic ActionScript&#8221;</p>
<p>Douglas Crockford: <a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/">JavaScript: The Good Parts</a> (get this book!)</p>
<p><a href="http://eismann-sf.com/news/wp-content/uploads/2008/09/googlechromecomic.pdf">Google Chrome Comic</a> (JavaScript implementation and garbage collection)</p>
<p>Branden Hall and Samuel Wan: <a href="http://www.amazon.com/Object-Oriented-Programming-ActionScript-Branden-Hall/dp/0735711836">Object-Oriented Programming with ActionScript</a> (historical interest; AS1 only)</p>
<p>Yakov Fain, Victor Rasputnis, and Anatole Tartakovsky: <a href="http://www.amazon.com/Internet-Applications-Adobe-Secrets-Masters/dp/097776222X">Rich Internet Applications with Adobe Flex &#038; Java (Secrets of the Masters)</a> (aka The RIA Book; especially Chapter 4, &#8220;Learning Flex Through Applications&#8221;)</p>
<p>Jens C Brynildsen: <a href="http://www.flashmagazine.com/news/detail/the_future_of_actionscript/">The Future of ActionScript</a></p>
<p>Derek Wischusen: <a href="http://flexonrails.net/?p=79">Mixins in ActionScript 3</a> (perhaps going too far)</p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/09/19/dynamic-programming-in-as3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Perlin Color</title>
		<link>http://nodename.com/blog/2008/07/13/perlin-color/</link>
		<comments>http://nodename.com/blog/2008/07/13/perlin-color/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 02:09:09 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=61</guid>
		<description><![CDATA[Adding color to the previous example, we obtain tinted Perlin Clouds; allowing the color values to overflow past 1 gives us Perlin Plasma.  A shift in coordinates reveals that the Frocessing implementation of Perlin noise is symmetric about all three axes (not necessarily implying that it&#8217;s incorrect if you stay in one octant&#8230;), producing [...]]]></description>
			<content:encoded><![CDATA[<p>Adding color to the <a href="http://nodename.com/blog/2008/07/11/perlin-clouds-and-frocessing-with-an-f/">previous example</a>, we obtain tinted Perlin Clouds; allowing the color values to overflow past 1 gives us Perlin Plasma.  A shift in coordinates reveals that the Frocessing implementation of Perlin noise is symmetric about all three axes (not necessarily implying that it&#8217;s incorrect if you stay in one octant&#8230;), producing Perlin Kaleidoscope and Perlin Oriental Rug.</p>
<p><center><br />
<img src="http://nodename.com/wpEmbeds/PerlinClouds/samples.jpg" alt="Samples" /></p>
<p />

<object	type="application/x-shockwave-flash"
			data="/wpEmbeds/PerlinClouds/ColorPerlinCloudsTest.swf"
			width="800"
			height="600">
	<param name="movie" value="/wpEmbeds/PerlinClouds/ColorPerlinCloudsTest.swf" />
</object><br />
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/07/13/perlin-color/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Perlin Clouds and Frocessing (with an F)</title>
		<link>http://nodename.com/blog/2008/07/11/perlin-clouds-and-frocessing-with-an-f/</link>
		<comments>http://nodename.com/blog/2008/07/11/perlin-clouds-and-frocessing-with-an-f/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 05:31:31 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=60</guid>
		<description><![CDATA[Here are two instances of the same demo, modeled after the one Seb Lee-Delisle showed last fall, built with two different Flash implementations of 3D Perlin noise.  The one on the right uses Ron Valstar&#8217;s Perlin class (as does Seb&#8217;s demo), which appears to be a faithful port of Ken Perlin&#8217;s Improved Noise reference [...]]]></description>
			<content:encoded><![CDATA[<p>Here are two instances of the same demo, modeled after the one <a href="http://www.sebleedelisle.com/?p=134">Seb Lee-Delisle</a> showed last fall, built with two different Flash implementations of 3D Perlin noise.  The one on the right uses <a href="http://www.sjeiti.com/?p=305">Ron Valstar&#8217;s Perlin class</a> (as does Seb&#8217;s demo), which appears to be a faithful port of <a href="http://mrl.nyu.edu/~perlin/noise/">Ken Perlin&#8217;s Improved Noise reference implementation</a> in Java, and therefore reliable.<br />
The one on the left is built on the PerlinNoise class from the Frocessing library by Tomoaki TAKANAWA.  This library is in an early stage but it is intriguing nonetheless.  Note, though, the different responses of the two instances to changes in the parameter values.  The Frocessing implementation seems to be faster but perhaps a bit buggy.  Anyway I&#8217;ll be watching the development of Frocessing with interest.</p>
<p>
UPDATE:   I have rebuilt the Valstar example using <a href="http://www.quasimondo.com/archives/000672.php">Mario Klingemann&#8217;s optimized version</a> of Ron&#8217;s class.
</p>
<p><center><br />

<object	type="application/x-shockwave-flash"
			data="/wpEmbeds/PerlinClouds/PerlinCloudsTest.swf"
			width="800"
			height="480">
	<param name="movie" value="/wpEmbeds/PerlinClouds/PerlinCloudsTest.swf" />
</object><br />
</center></p>
<h3>Frocessing Resources</h3>
<ul>
<li>
Takanawa&#8217;s <a href="http://nutsu.com/blog/2008/061100_as_frocessing.html">Frocessing demo</a><br />
His code is presented as a Flash frame script; <a href="http://nodename.com/wpEmbeds/PerlinClouds/Curve3D.as">here</a> is a .as version of it suitable for Flex Builder.  He also notes that for performance reasons, Font and Image implementation will have to wait for the Astro release.  How close can Flash ultimately come to Java performance?
</li>
<li>
<a href="http://www.libspark.org/wiki/nutsu/Frocessing">Frocessing Wiki (Japanese)</a>
</li>
<li>
<a href="http://nutsu.com/doc/frocessing/">ASDocs</a>
</li>
<li>
<a href="http://www.libspark.org/svn/as3/Frocessing/trunk">Frocessing source code (library and samples)</a>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/07/11/perlin-clouds-and-frocessing-with-an-f/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Upon Reflection</title>
		<link>http://nodename.com/blog/2008/06/15/upon-reflection/</link>
		<comments>http://nodename.com/blog/2008/06/15/upon-reflection/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 00:54:16 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=59</guid>
		<description><![CDATA[In ActionScript 3, classes (and functions as well) are objects that can be manipulated like any other object.  The language provides us with some constructs to facilitate this, and they are an important part of the programmer's toolkit.

We need to be able to traverse the connections among the Class, an instance of the Class, and the name of the Class.]]></description>
			<content:encoded><![CDATA[<p>In ActionScript 3, classes (and functions as well) are objects that can be manipulated like any other object.  The language provides us with some constructs to facilitate this, and they are an important part of the programmer&#8217;s toolkit.</p>
<p>We need to be able to traverse the connections among the Class, an instance of the Class, and the name of the Class.</p>
<h1>From name to Class; from instance to Class</h1>
<p>First, you want to be able to get hold of a Class object, either from its package path and class name:</p>
<pre>
import flash.utils.*;

var myClass:Class = getDefinitionByName("com.myDomain.myPackage.SomeClass");
</pre>
<p>or from an instance of the class:</p>
<pre>
var myClass:Class = myInstance.constructor;
</pre>
<p>Here are a few simple ways you can use these:</p>
<ul>
<li>
The Class object is a factory for making instances of its class, so using getDefinitionByName() you can write a factory method to instantiate objects of types specified by Strings, e.g. in  an XML file:</p>
<pre>
var myClass:Class = getDefinitionByName("com.myDomain.myPackage." + xmlData.@type);
var myObject:Object = new myClass();
</pre>
</li>
<li>
Run-time enforcement of an abstract class:</p>
<pre>
public function MyClass()
{
	super();
	if (this.constructor == MyClass)
	{
		throw new IllegalOperationError("AbstractException");
	}
}
</pre>
</li>
<li>
Suppose a game character is collecting items such as gems or weapons or vegetables, and all you need to know about the items is their type.  You don&#8217;t have to hang on to the items themselves; you can store the collection as a <a href="#AS3Collections">Dictionary</a> indexed by class objects:</p>
<pre>
private var _collection:Dictionary = new Dictionary(true);
.
.
.
private function collect(item:Object):void
{
	var itemClass:Class = item.constructor;
	if (_collection[itemClass] == null)
	{
		_collection[itemClass] = 1;
	}
	else
	{
		_collection[itemClass]++;
	}
}
</pre>
</li>
</ul>
<h1>From Class  to class name; from instance to class name</h1>
<p>I&#8217;ll just show my getClassName() function here:</p>
<pre>
import flash.utils.*;

public function getClassName(o:Object):String
{
	if (o is Class)
	{
		var s:String = o.toString();	// "[class SoAndSo]&#8221;
		return s.substring(7, s.length - 1);
	}
	else
	{
		var fullClassName:String = getQualifiedClassName(o);
		var i:int = fullClassName.lastIndexOf(&#8221;::&#8221;);
		if (i == -1)
		{
			return fullClassName;
		}
		return fullClassName.slice(i + 2);
	}
}
</pre>
<p>The <b>toString()</b> method is defined in the root Object class, and it can be redefined in your own classes.  It&#8217;s called whenever the String representation of an object is needed.  For example in getClassName() I could have written</p>
<pre>
var s:String = "" + o;
</pre>
<p>which would have implicitly invoked o.toString().  The trace() function also uses toString().  So it&#8217;s a good idea to implement toString() in your classes.  I usually start out by doing this:</p>
<pre>
public override function toString():String
{
	return getClassName(this);
}
</pre>
<p>and then add whatever specific data I need to show into it.</p>
<p>Related ActionScript 3 Tip of the Day by Senocular: <a href="http://www.kirupa.com/forum/showpost.php?p=1893462&#038;postcount=73">#73</a></p>
<h1>From Class to Class</h1>
<p>Here&#8217;s the last basic class-slinging tool, and I believe it&#8217;s one that not many Flash programmers are aware of.  Suppose you want to know if class A is derived from class B.  You might think you&#8217;d have to make an instance of A and use the <b>is</b> operator on it.  And that&#8217;s OK if you don&#8217;t have to come up with parameters for the constructor.  But there&#8217;s a cleaner way: </p>
<pre>
public function isDerived(childClass:Class, ancestorClass:Class):Boolean
{
	if (ancestorClass.prototype.isPrototypeOf(childClass.prototype))
	{
		return true;
	}
	return false;
}
</pre>
<p>Now you may say yeccch, we&#8217;ve left the prototype behind, that&#8217;s old-school AS1 programming, and you&#8217;d certainly be correct in a way: In AS1 there was no way to simulate classes except by mucking with the prototype, and in AS2 the compiler simulated classes under the hood by mucking with the prototype, and in AS3 we have true class inheritance.  So there&#8217;s almost certainly no need to simulate classes in AS3.  But the prototype, and the <b>isPrototypeOf()</b> function, are not going away any time soon; they&#8217;re in the ECMA spec, so they must be retained in the language.  I believe I can guarantee, though, that aside from this <b>isDerived()</b> function, there is NO other legitimate use case for <b>isPrototypeOf()</b> in AS3!  The prototype itself is a different matter, and I mentioned a use case for placing properties there in a comment to my recent post <a href="http://nodename.com/blog/2008/01/13/advanced-users-may-choose/">Advanced Users May Choose</a>.</p>
<p>Related ActionScript 3 Tip of the Day by Senocular: <a href="http://www.kirupa.com/forum/showpost.php?p=1894555&#038;postcount=74">#74</a></p>
<p>What can you do with <b>isDerived()</b>?  Suppose my game character is collecting various kinds of Gems, such as Rubies, Sapphires, and Diamonds, and various kinds of Vegetables, such as Rutabagas, Kohlrabi, and JerusalemArtichokes.  He&#8217;s saving them in a Dictionary as in the example above, and I need to know how many Gems or how many Vegetables he has:</p>
<pre>
private function numInCollection(collectionClass:Class):uint
{
	var n:uint = 0;
	for (var key:* in _collection)
	{
		var c:Class = key as Class;
		if (c == collectionClass
		||  isDerived(c, collectionClass))
		{
			n += _collection[key];
		}
	}
	return n;
}
</pre>
<h1 id="AS3Collections">Summary of indexed collections in AS3</h1>
<ul>
<li>Object: properties indexed by Strings</li>
<li>Array: properties indexed by Numbers</li>
<li>Dictionary: properties indexed by Objects</li>
</ul>
<p>Occasionally I meet a programmer who indexes an Array object&#8217;s properties using Strings.  Of course this works, but it works because the array is an Object; if you&#8217;re not using Numbers as indices, then you&#8217;re just wasting space by declaring the collection as an Array.  You could make it a MovieClip and it would work just as well!</p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/06/15/upon-reflection/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Behind the Beyond, or Beyond the Behind</title>
		<link>http://nodename.com/blog/2008/06/05/behind-the-beyond-or-beyond-the-behind/</link>
		<comments>http://nodename.com/blog/2008/06/05/behind-the-beyond-or-beyond-the-behind/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 05:49:08 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/2008/06/05/behind-the-beyond-or-beyond-the-behind/</guid>
		<description><![CDATA[With the recent advances in Flash 3D and panoramas, and the fact that Flash 10, now out in beta, has intrinsic support for arbitrary warping of triangles, we can look forward to a future of increased performance and the complete disappearance of those wavy lines in panos.  All of that fancy 3D stuff is [...]]]></description>
			<content:encoded><![CDATA[<p>With the recent advances in Flash 3D and panoramas, and the fact that Flash 10, now out in beta, has intrinsic support for arbitrary warping of triangles, we can look forward to a future of increased performance and the complete disappearance of those wavy lines in panos.  All of that fancy 3D stuff is based on matrix transformations, but lately I&#8217;ve been working with simpler analytic-geometry concepts, some of which turn out to be quite useful.  Let&#8217;s look at a few.</p>
<p>We&#8217;ll start with the <strong>sameSide()</strong> function, which I&#8217;ll use to implement some more interesting functions:</p>
<pre>
// sameSide.as
package
{
    import flash.geom.Point;

    // are p1 and p2 on the same side of the line through a and b?
    public function sameSide(p1:Point, p2:Point, a:Point, b:Point):Boolean
    {
        var cp1:Number = cross(b.subtract(a), p1.subtract(a));
        var cp2:Number = cross(b.subtract(a), p2.subtract(a));
        return (dot(cp1, cp2) >= 0);
    }
}       

    import flash.geom.Point;

    function cross(a:Point, b:Point):Number
    {
        // z coordinate of the cross product; x and y coordinates are zero
        return a.x * b.y - a.y * b.x;
    }

    function dot(a:Number, b:Number):Number
    {
        // z vectors
        return a * b;
    }
</pre>
<p>A little AS3 note about the foregoing: The above code is the entire sameSide.as file; it&#8217;s a bit unusual in that it does not contain a class.  AS3 requires that a source file expose a single external definition, which in this case is a function rather than a class.  A function defined in this way is called a package-level function.  The functions <strong>cross()</strong> and <strong>dot()</strong> that appear after the closing curly brace are visible only to code within the same file, and thus any access control specifier like &#8220;internal&#8221; or &#8220;private&#8221; would be redundant.</p>
<p>Of course you could make <strong>sameSide()</strong> a public static method of a Class, and <strong>cross()</strong> and <strong>dot()</strong> private static methods.</p>
<p>Now we implement some other functions based on <strong>sameSide()</strong>:</p>
<pre>
// behind.as
package
{
    import flash.geom.Point;

       /**
         * Is p2 behind me when I stand at p0 and face p1?
         *
         */
        public function behind(p0:Point, p1:Point, p2:Point):Boolean
        {
            // slope of normal to the line p0p1:
            var m:Number = (p0.x - p1.x)/(p1.y - p0.y);
            // y-intercept of normal through p0:
            var b:Number = p0.y - m * p0.x;

            return !sameSide(p1, p2, p0, new Point(0, b));
        }
}
</pre>
<pre>
// beyond.as
package
{
    import flash.geom.Point;

       /**
         * Is p2 beyond p1 when I stand at p0 and face p1?
         *
         */
       public function beyond(p0:Point, p1:Point, p2:Point):Boolean
        {
            // slope of normal to the line p0p1:
            var m:Number = (p0.x - p1.x)/(p1.y - p0.y);
            // y-intercept of normal through p1:
            var b:Number = p1.y - m * p1.x;

            return !sameSide(p0, p2, p1, new Point(0, b));
        }
}
</pre>
<p>Cute, eh?  Nice to know if something is behind you, or beyond me.</p>
<p>Here&#8217;s another:</p>
<pre>
package
{
    import flash.geom.Point;

/**
* Is the point p inside the triangle abc?
*/
    public function pointInTriangle(p:Point, a:Point, b:Point, c:Point):Boolean
    {
        return sameSide(p, a, b, c) &#038;&#038; sameSide(p, b, a, c) &#038;&#038; sameSide(p, c, a, b);
    }
}
</pre>
<p>which is to say p is inside triangle abc if it&#8217;s on the same side of line bc as a, and on the same side of line ac as b, and on the same side of line ab as c.  How obvious is that?</p>
<p>I have to credit my source here, because &#8220;point in triangle&#8221; was the search that led me to <strong>sameSide()</strong>: <a href="http://www.blackpawn.com/texts/pointinpoly/default.html">Point In Triangle Test</a>.  As it turns out,  I haven&#8217;t actually used <strong>pointInTriangle()</strong> for anything yet.  <strong>behind()</strong> and <strong>beyond()</strong>, though, play an important part in <strong>Geometry-Based Path Planning</strong>, the subject of a future post.</p>
<p>Frequent sources of inspiration on similar topics are <a href="http://mathworld.wolfram.com/">Wolfram MathWorld</a> and of course Wikipedia.</p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/06/05/behind-the-beyond-or-beyond-the-behind/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Advanced Users May Choose</title>
		<link>http://nodename.com/blog/2008/01/13/advanced-users-may-choose/</link>
		<comments>http://nodename.com/blog/2008/01/13/advanced-users-may-choose/#comments</comments>
		<pubDate>Sun, 13 Jan 2008 23:33:29 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/2008/01/13/advanced-users-may-choose/</guid>
		<description><![CDATA[Can anybody show me a use case for the following?
(Flex language manual, Object.constructor)
Advanced users may choose to use the function keyword instead of the class keyword to define a Function object that can be used as a template for creating objects. Such a function is called a constructor function because you can use it in [...]]]></description>
			<content:encoded><![CDATA[<p>Can anybody show me a use case for the following?</p>
<p>(Flex language manual, Object.constructor)</p>
<p>Advanced users may choose to use the function keyword instead of the class keyword to define a Function object that can be used as a template for creating objects. Such a function is called a constructor function because you can use it in conjunction with the new operator to create objects. If you use the function keyword to create a constructor function, its prototype object is assigned a property named constructor that holds a reference to the constructor function. If you then use the constructor function to create an object, the object inherits the constructor property from the constructor function&#8217;s prototype object. For example, the following code creates a new constructor function, f, and an object named myF:</p>
<p>  function f() {}<br />
  trace(f.prototype.constructor);      // function Function() {}<br />
  trace(f.prototype.constructor == f); // true<br />
  var myF = new f();<br />
  trace(myF.constructor == f);         // true</p>
<p>UPDATE:  I&#8217;ve looked into this and I have some answers to my own question:<br />
<a href="http://nodename.com/blog/2008/09/19/dynamic-programming-in-as3/">Dynamic Programming in AS3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2008/01/13/advanced-users-may-choose/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Two Exciting Developments in Flash 3D</title>
		<link>http://nodename.com/blog/2006/08/01/two-exciting-developments-in-flash-3d/</link>
		<comments>http://nodename.com/blog/2006/08/01/two-exciting-developments-in-flash-3d/#comments</comments>
		<pubDate>Tue, 01 Aug 2006 22:47:34 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/2006/08/01/two-exciting-developments-in-flash-3d/</guid>
		<description><![CDATA[The past few days have seen the release of version 1.1 of Thomas Pfeiffer&#8217;s AS2 (and soon to be AS3) 3D library Sandy &#8211; check out the great kitty demo! &#8212; and of Andre Michelle&#8217;s AS3 CubicVR immersive panorama application (requires Flash 9 of course).  (For a bit of technical background, check out Andre&#8217;s post AS3 Perspective Texturemapping.)
Wow, and wow!  [...]]]></description>
			<content:encoded><![CDATA[<p>The past few days have seen the release of version 1.1 of Thomas Pfeiffer&#8217;s AS2 (and soon to be AS3) 3D library <a href="http://sandy.media-box.net/blog/" target="_blank">Sandy</a> &#8211; check out the great kitty demo! &#8212; and of Andre Michelle&#8217;s AS3 <a href="http://blog.andre-michelle.com/cubicvr/" target="_blank">CubicVR</a> immersive panorama application (requires Flash 9 of course).  (For a bit of technical background, check out Andre&#8217;s post <a href="http://blog.andre-michelle.com/2005/as3-perspective-texturemapping/">AS3 Perspective Texturemapping</a>.)</p>
<p>Wow, and wow!  Both products are absolutely free (and Sandy is open-source as well), and they&#8217;re both asking for donations.</p>
<p>Send them each ten bucks now!</p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2006/08/01/two-exciting-developments-in-flash-3d/feed/</wfw:commentRss>
		</item>
		<item>
		<title>REAL Live Docs: Gradient Fills, Transitions, Tweens, and Flex CSS Explored</title>
		<link>http://nodename.com/blog/2006/04/06/real-live-docs-gradient-fills-transitions-tweens-and-flex-css-explored/</link>
		<comments>http://nodename.com/blog/2006/04/06/real-live-docs-gradient-fills-transitions-tweens-and-flex-css-explored/#comments</comments>
		<pubDate>Fri, 07 Apr 2006 01:21:17 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=46</guid>
		<description><![CDATA[In a post entitled Testing the Flash Gradient Fill Method, and Some Personal Thoughts, Kinglong&#8217;s Blog (zh) presents a neat little swf reminiscent of the test swf in Psyark&#8217;s DisplacementMapFilter Tutorial.  This one demonstrates the effects of various parameters of the beginGradientFill() method in Flash.   (But not all the parameters.  I&#8217;d [...]]]></description>
			<content:encoded><![CDATA[<p>In a post entitled <a href="http://www.klstudio.com/post/67.html">Testing the Flash Gradient Fill Method, and Some Personal Thoughts</a>, Kinglong&#8217;s Blog (zh) presents a neat little swf reminiscent of the test swf in <a href="http://nodename.com/blog/2006/01/16/psyarks-displacementmapfilter-tutorial/">Psyark&#8217;s DisplacementMapFilter Tutorial</a>.  This one demonstrates the effects of various parameters of the beginGradientFill() method in Flash.   (But not all the parameters.  I&#8217;d like to see it extended, and a little larger.)</p>
<p>The controls are all clear to the non-Chinese reader, except for the selector at bottom left.  It&#8217;s &#8220;fillType: linear/radial.&#8221;</p>
<p>Here&#8217;s what Kinglong has to say:</p>
<blockquote><p>
Recently when developing a Flash component, I needed to implement gradient fills (beginGradientFill) in ActionScript.  Previously I had little understanding of the matrix used in this method.  I searched on the Web and found some information, but it was all pretty unclear!  So I wrote this test program to gain basic understanding of the method.  I hope it&#8217;s useful for everyone; I call this &#8220;using a program to study a program&#8221;!  I hope those who study AS coding can also learn this mode of thinking, and can &#8220;use a program to study a program&#8221;  to understand the real meaning of other methods!  That&#8217;s much better than reading dry docs.</p>
<p>&#8220;Learning Flash development is really not dull!&#8221;
</p></blockquote>
<p>Yes, indeed.  May we all &#8220;Learn from model developer Kinglong!&#8221;<br />
<a href="http://news.xinhuanet.com/english/2006%2D03/16/content%5F4308138.htm"><img src="http://nodename.com/wpEmbeds/LeiFeng.jpg" alt="Lei Feng" /></a></p>
<p />
Meanwhile, over on the mother ship, Clive Whitear of Adobe Consulting has come up with a <a href="http://weblogs.macromedia.com/mc/archives/2006/04/transition_and.cfm">Transition and Tween Explorer</a>, and a couple of weeks ago Peter Baird of the same group posted a <a href="http://weblogs.macromedia.com/mc/archives/2006/03/flex_2_style_ex_1.cfm">Flex 2 Style Explorer</a>.</p>
<p />
<a href="http://www.flashinsider.com/2006/04/06/transition-and-tween-explorer/">Flash Insider</a> suggests that Adobe build explorers into the Flash UI.</p>
<p />
Explorers!</p>
<p />
UPDATE 2006/4/24:  Keith Peters is in the Explorers Club too!  Check out the Noise Explorer and Perlin Explorer in his <a href="http://www.bit-101.com/blog/?p=776">FITC presentation</a>!</p>
<p>UPDATE 2006/5/17:  Here&#8217;s a link to the updated <a href="http://weblogs.macromedia.com/mc/archives/2006/05/flex_2_style_ex_2.cfm">Flex 2 Style Explorer Beta 3</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2006/04/06/real-live-docs-gradient-fills-transitions-tweens-and-flex-css-explored/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PixelTip</title>
		<link>http://nodename.com/blog/2006/03/27/pixeltip/</link>
		<comments>http://nodename.com/blog/2006/03/27/pixeltip/#comments</comments>
		<pubDate>Mon, 27 Mar 2006 16:14:52 +0000</pubDate>
		<dc:creator>alan</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://nodename.com/blog/?p=44</guid>
		<description><![CDATA[In my Flash 8 bit-twiddling adventures I&#8217;ve found it useful to be able to check the colors of individual pixels in a running swf.  If you need that too, I offer you the PixelTip class, a variant of my ToolTip class that simply displays the coordinates and value of the pixel under the cursor. [...]]]></description>
			<content:encoded><![CDATA[<p>In my Flash 8 bit-twiddling adventures I&#8217;ve found it useful to be able to check the colors of individual pixels in a running swf.  If you need that too, I offer you the <strong>PixelTip</strong> class, a variant of my <a href="http://nodename.com/blog/2005/09/12/tooltip/">ToolTip</a> class that simply displays the coordinates and value of the pixel under the cursor.  (The cursor itself doesn&#8217;t show up in these screenshots.)<br />
<br />
<img src="http://nodename.com/wpEmbeds/PixelTip.jpg" alt="PixelTip" /><br />
<br />
The class has just four public methods, all static:</p>
<p>· <strong>setBoxSize</strong>(wd:Number, ht:Number) (default 150 x 50)<br />
· <strong>showDecimal</strong>(yesno:Boolean) (default true)<br />
· <strong>showHex</strong>(yesno:Boolean) (default false)<br />
· <strong>register</strong>(tipTarget:MovieClip).  Of course you can register multiple MovieClips.<br />
<br />
<a href="http://nodename.com/wpEmbeds/PixelTip.as">Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nodename.com/blog/2006/03/27/pixeltip/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
