connectedpixel.com

actionscript, web development

ActionScript

Callback filter functions in E4X

Submitted by joelmay on 2 April, 2007 - 7:38am.

While porting some xpath-heavy AS2 code to AS3, I ran into problems. E4X (new and improved xml support in AS3) seems to make xpath obsolete. However, all the E4X query examples in the docs use hard-coded literals. I can't use literals because I don't know at code-time what the queries are going to be. My AS2 code creates xpath paths dynamically from variables. What is the E4X equivalent?

After some initial confusion on my part, I now get it. In retrospect, what's below seems obvious -- but that's the way it always is.

Selecting a variable-named node type

Let's work with this xml:

Detecting unwell xml

Submitted by joelmay on 4 January, 2007 - 9:07am.

If an XML file is created manually or edited manually, it is easy to misspell a closing tag, misplace a greater than sign or forget a quote mark – it's easy to create non-well-formed xml. Flash will not be able to read it and your application will fail. Furthermore, it might load some of the data and operate in a mysterious, crippled fashion, and the cause of the problem will not be obvious.

Preprocessor ASSERTS in ActionScript

Submitted by joelmay on 20 October, 2006 - 4:28pm.

Unlike most other computer languages, when ActionScript code fails, it fails silently. If my code sends an undefined argument to a function, there is no crash, no alert message, nothing scary.

Overall, this is a good thing. If my live swf hits a bug, it might work just fine, no one is the wiser and life goes goes on blissfully unaware.

This behavior has a downside, however. The bug that is ignored now might cascade into a serious problem later -- in the next frame or in some code far away from the source of the problem. And I'm left scratching my head trying to figure out the original source of the problem in my code. The appearance of the problem and the source of the problem may be far apart.

Array.sortOn does not work with MovieClips

Submitted by joelmay on 2 October, 2006 - 1:57pm.

This problem drove me crazy. I'd thought I'd get it in the google-verse so anyone else who runs into it can benefit from my pain.

Array.sortOn() does not seem to work when the array elements are objects derived from MovieClip. The following code demonstrates.

Simple class. "zz" property will be used to sort an array of these objects.

Face Morph - Part 1, Morphin' George

Submitted by joelmay on 6 June, 2006 - 4:24pm.

Unlike Grant Skinner's gooify, which is a free-form distortion, the morph presented here is more constrained. It has an editable triangle mesh (which I'm not showing right now).

Simulate download: Static vars not re-initialized

Submitted by joelmay on 4 May, 2006 - 3:06am.

The 'Simulate Download' feature in the Flash IDE is very useful. It can help debug race conditions that are a cause of intermittent problems. However, it can also lead you astray.

Problem: AS2 static variables are NOT reinitialized when you hit control-Enter the 2nd time to go into simulate-download mode.

If you'd like to try it, here's a simple test class:

Flash Perlin Texture Components: Wood and Marble

Submitted by joelmay on 20 April, 2006 - 12:26pm.

A while back, I wrote about rendering Wood and Marble using Perlin noise. But I've found that these code bits are a pain to use. You need to write code just to see them. You can't work with them in the Flash IDE; i.e. they are inconvenient.

Solution: package them in components with live preview. That way you can see them, adjust their color visually, mask them, drop shadow them, etc.

Component Array Parameter Gotcha

Submitted by joelmay on 22 November, 2005 - 11:18am.

So the other day, I was working on a component that has an inspectable set/get array property. I could not get it work. Although the default value for this array was not zero length and the parameter in the dialog box was not zero length, the set function was being called with a zero-length array at initialization.

This was an invalid situation and broke my app.

Here's a simplified version of the code:

Flash8 Perlin Marble Texture

Submitted by joelmay on 8 November, 2005 - 2:25pm.

Last week, I wrote an article about using Flash 8 Perlin noise to generate a wood texture. Today, it's marble.

According to this link, the Perlin formula for marble is:

texture = cosine( x + perlin(x,y,z) )

Again, we don't want to do math on every pixel -- too slow. So, how do we do this with the existing Flash 8 API? First, let's pretend that the perlin term in the above equation is not there. Here's what our image would look like (this image is arbitrarily blue monochrome):

Flash8 Perlin Wood Texture

Submitted by joelmay on 1 November, 2005 - 12:36pm.

The Flash 8 documentation for perlinNoise() has this intriguing statement:

You can use Perlin noise functions to simulate natural phenomena and landscapes, such as wood grain, clouds, and mountain ranges.

OK. That sounds like fun. But how is this done? It's not immediately obvious, at least not to me. So I googled .

According to these links (here and here), the formula for wood is:

g = perlin(x,y) * 20;
grain = g - int(g);