(previously titled : Flash Player bug? for each..in loops)
[UPDATE] Kudos and much thanks to Tim Knip who pointed out that it was in fact my mistake – there’s a stray semi-colon at the end of the line :
for each(byteArray in soundSources);
Which is of course ending the loop there and just running the code in braces after that once. I seem to keep accidentally typing semi-colons lately. Perhaps it’s the frustration of having to work with the sound API.
And of course the good news is that we can safely continue to use for each…in. Yay! Thank you everyone for helping me find this.
I’ve been pulling my hair out over the last few days trying to get a sound toy working, don’t you hate it when you feel like you’re fighting against Flash? Anyway, I think I’ve found a bug, I’m iterating through an array of ByteArrays using for each, but for some reason it will only give me the last ByteArray.
I’m sure this has been the source of several unexpected behaviours in my current project. Can you let me know if it happens in your FlashPlayer? I’m currently running the debug player version 10.1.51.82.
Here’s the swf :
And here’s what I see :

And here’s what I would expect to see :

You can see I’m iterating through the array twice, firstly with the iterator i and secondly with the for each directive.
And here’s the code :
(more…)
Posted in Actionscript, Flash | 28 Comments »
Friday, March 19th, 2010
… is measured in samples. Not bytes or mils.
So if you want to know how it relates to the ByteArray you’re working with, you need to multiply it by 4 (as each floating point sample requires 4 bytes). And if you want to convert it to mils, you need to divide by 44.1, the number of samples per millisecond.
I just thought I’d point it out because it doesn’t seem to be mentioned in the AS3 docs. Or the Flex docs. I checked this time
I expect you’re wondering why I’m discovering all this stuff. Watch this space
Posted in Actionscript, Flash, Sound API | 1 Comment »
Wednesday, March 17th, 2010
… is measured in milliseconds.
Why do I mention this? Because it seems to have been entirely omitted from the ActionScript live docs and it took me a while to figure it out. Maybe this post will save someone some time in the future.
[UPDATE] I just noticed that this is actually specified in the latest Flex 3.5 language reference, but not in the ActionScript reference. I guess it would be boring if it were too easy, huh?
Posted in Actionscript, Flash, Sound API | 3 Comments »
Tuesday, March 9th, 2010
UPDATE – HTML 5 Canvas version now online
During my session at the Flash Gaming Summit, I showed a very simple 3D particle renderer to attempt to demystify the process of converting 3D into 2D.
[kml_flashembed publishmethod="dynamic" fversion="10.0.0" useexpressinstall="true" movie="http://sebleedelisle.com/wp-content/uploads/2010/03/Particles3D.swf" width="500" height="350" targetclass="flashmovie"]

Click and drag.
The actual code that converts from 3D to 2D is easy! First you have to figure out how much you have to resize things depending on how far away they are. (ie what their z position is).
Where f is a notional field of view. Changing this will change how wide the (virtual) camera is. A good starting point would be 250. Then you multiply the 3D x and y positions by the newscale to get their 2D x and y positions.
x2d = x3d * newscale;
y2d = y3d * newscale;
So that’s where we put our particle, in this case represented by a MovieClip that we also need to resize by our new scale :
clip.scaleX = clip.scaleY = newscale;
And then the final thing we need to do is sort by the z position so that particles in front appear above particles in the background.
particles.sortOn("z", Array.DESCENDING | Array.NUMERIC);
for(var i:int = 0; i< particles.length;i++)
{
particle = particles[i];
particleClip.setChildIndex(particle.clip, i);
}
There’s some other stuff in there too, to create the depth of field blur and also the bitmap trails, but have a look and see what you can do with it.
Download the source code for simple 3D engine.
* I haven’t actually counted the number of lines. I’m guessing it’s about 10.