BitmapData + PV3D = Fun!!!

This all started out by a back-end colleague mentioning he wanted to try something like this and I was thinking to myself that it shouldn’t be too hard to achieve.

I’m reading out colour values per pixel of an image that I’m externally loading. Based on that info I create a shitload of particles in papervision, the effect is quite cool, but needs to be taken much further from here. Imaging what fun stuff you can do with this!

See it in action: http://ansuz.nl/toys/particles_from_image/pv3d/main.swf

To my surprise this is still running at 30 fps with almost 2500 particles in the view, I guess I need to do some more testing to see how many particles we can really push in there.

A next step is going to try this with Flint (particles), because that can easily handle 10-15K particles!

Deserializing data

When deserializing data, do it at the very first time you can and continue to use the deserialized data throughout your application. This will save you a lot of aggravation later on.

Take deserializing XML into (Value)Object(s), when you want to change the objects later on, say add, remove, alter properties you can easily do so, it’s quite a bit harder to insert a node or attribute into the (serialized) XML.

Links:
(De)Serialization

PV3D pitch, roll, yaw vs. rotationX, Y, Z

While working on a project of mine this weekend I discovered that there is another difference between pitch, roll, yaw and rotationX, Y, Z in Papervision. Pitch, roll and yaw both ‘rotate’ using the localRotationX, Y, Z, I knew that. What I didn’t know was the following:
When using yaw you are changing the rotationY property between -90 and 90. So when you use yaw and then read out the rotationY values for further calculations, you might be in for a nasty surprise. A way around this is just using rotationY.

Adobe Flash Player Security

Ever got the following error and never found out how to solve it? Here’s the solution.

Error:

Adobe Flash player has stopped a potentially unsafe operation.
The following local application ….
C:\somewhere\file.swf
is try to communicate with…
somedomain.com
To let this app…

Solution:
Create a file with the extension .cfg in the <user>/macromedia/Flash_Player/#Security/FlashPlayerTrust folder (under windows XP this will be: C:\Documents and Settings\_username_\Application Data\Macromedia\Flash Player\#Security\FlashPlayerTrust) and create two lines in it using a regular text editor.

C:\somewhere\
http://somedomain.com

Save the file and rerun your application.

Fonts and loading times

When embedding fonts there are a couple of things you need to keep in mind.

  1. When will you need them? (i.e. during the loading)
  2. How to embed them? Using the IDE or Flex?
  3. Where will you embed them?

Since there is no way to export you library items in another frame than the first in AS3 you need to look for alternatives.

An alternative would be to just accept the increased file size when embedding them in the first frame and showing no information to the user when this is loading. I have noticed though that a simple Latin-1 font adds about 30 kB. One could reason with the widespread availability of broadband this shouldn’t be too bad.

The other option is to embed the font in an external file and decide not to use it for the loader. This keeps your file size down and increases initial loading, that way you can show the user sooner what is happening.

Also keep in mind that when you use a standard embed tag with Flex the embedded font will take up more space than when you use the Flash IDE to embed the fonts. You can avoid this though by specifying in the embed tag which characters you want to embed. A disadvantage of using the embedding with the IDE is that you won’t know for sure if your embedded fonts work until you have uninstalled them from you machine or test on a machine that doesn’t have the font.

Embedding in using the IDE goes as follows:

  1. In the library click
  2. ..

To embed a font in Flex you use the following code:

ActionScript

  1. // embedding fonts using flex
  2.  
  3. // note: leave the unicodeRange part out is you want to embed the entire font.
  4. [ Embed( source="Arial.ttf", fontName="Arial", mimeType="application/x-font-truetype", unicodeRange="<span><span class="character">U+0020-U+002F</span></span>" ) ]
  5. public var Arial:Class;

Now if you decide to load the font from an external file, you’ll need to register the freshly loaded font with the Flash player.

ActionScript

  1. // register the font with the player
  2. // where appDomain is a reference to the application domain the externally loaded font is in
  3. var arial:Class = Class(appDomain.getDefinition( "Arial" ));
  4. Font.registerFont(arial);

Some useful links about embedding a subset of a (unicode font):
Unicode Character Table
Yahoo on runtime font embedding

That’s about it for embedding fonts.

Update:
If embedded fonts aren’t showing up when compiling with Flex 4, add embedAsCFF=”false” to the embed-tag.

Masks

The other day I was trying to animate a mask on a Sprite. It was working only very jerkily or not all. After a lot of trial and error I decided to re-read the docs on masking a Sprite. It turns out you need to add the mask to the the Stage before you can animate it. Quite strange when you are used to just setting the mask in ActionScript 2.

ActionScript

  1. var mc:Sprite = new Sprite();
  2. // ... fill mc with something
  3. var myMask:Sprite = new Sprite();
  4. // ... draw the mask
  5. addChild(mc);
  6. addChild(mask);
  7. mc.mask = myMask;
  8. // .. now animate away!!! :)