Posts Tagged ‘actionscript’

20081021 Embedding fonts in ActionScript 3, using Flex SDK

Say you have a class and you want to use an embedded, non-system font in the class. That way you can rotate, scale, change alpha and apply filters to the text. And this is how it would look like, roughly:

public class MyClass extends Sprite
{
        [Embed(source="./../assets/MyFontLightItalic.TTF", fontFamily="MyFontFamily", fontWeight= "light", fontStyle = "italic", mimeType="application/x-font-truetype")]
        private var MyFont:Class;

        public function MyClass()
        {
                var tf:TextField = new TextField();
                tf.embedFonts = true;
                tf.defaultTextFormat = new TextFormat("MyFontFamily", 17, 0x7F7F7F);
        }
}

You might also need to make sure you’re linking to framework.swc, or otherwise you will get some strange errors like Error: The definition of base class FontAsset was not found.

Interesting stuff

Because most of the examples out there simply use “Arial” as font, they will never get the myriad errors I was getting, such as “Error: exception during transcoding: Font for alias ‘Blah Blah Light Italic’ with plain weight and style was not found” or the much dreaded “Error: java.lang.Integer cannot be cast to java.lang.String”.

The problem is that there are more sophisticated True Type Fonts out there, which split the font in several files, one for each variation of weight and style, and if you try to embed a file for a non normal weight, normal style, the Flex compiler won’t be able to find the definition of the shapes for the “normal version” and then will simply stop and let you wondering why it doesn’t work.

So that’s why we’re specifying not only the source file but also the weight and style of the font, and that’s what most of the examples never deal with — because Arial comes as a single file.

Then there’s the other important thing: the private class to which the font file will be attached. Even if you simply use the embedded font with the new alias defined in fontFamily (“MyFontFamily” in this example), you still need to provide that variable, or you will get a big “Error #2136: the swf file contains invalid data”.

More info

I assembled the solution to my woes taking parts from several sources:

20080717 How to crash Flash Player in few lines

I tried this with the stand-alone Flash Player, versions 9.0.45 and 9.0.124. I was hoping to crash the browser too when executing the malicious code inside a browser but that didn’t work :-/

Anyway this is how you would proceed if you wanted to crash your Flash Player. Or this is what you shouldn’t do if you do not want that to happen…

  1. Create a URLStream
  2. Add to it an event listener for HTTPStatusEvent.HTTP_STATUS
  3. Load a url which won’t return a 200 OK status code
  4. When you receive the HTTP_STATUS event, close the URLStream
  5. CRASH!

This only happens in Windows.

And here’s the bug in Adobe’s bug management system (mainly so that I can remember where is it and because the search engine there is terrible too!)

20070924 Security sandbox violation? How come!

I’ve received a few error reports on a for-fun-only flash thing/experiment/joke I did some time ago, the next captcha generation for myspace forms.

At first I thought it had been my fault, due to my inexperience in flash, but after having been working more time with sounds in flash, I found the problem is not in my code but in Flash’s internals. For some reason, if you’re calling, for example, computeSpectrum in one movie and at the same time there’s another flash movie being executed and playing any type of media in another tab, your movie will begin to trigger Security sandbox violation errors.

It seems to happen because the player doesn’t distinguish between different movies, and treats all the sounds as if they belonged to the same soundspace (like a namespace, but for sounds :D), hence it tries to mix them all together and process the soundSpectrum of this global mix. Obviously, they don’t belong to the same domain, and that’s why you’ll soon get errors.

And you might wonder what’s going wrong, because you’ve got your policy files and everything sorted out, everything seems fine but it’s not when you try to access global sound stuff. Hopefully now you know why (although you can’t do anything about that).

I’m not sure if this should be an expected behaviour or they just didn’t think that we could execute several flash movies in different tabs. Obviously, this only shows up if you’re using the debug version of the player. In any other case, all these errors are lost in a flash log file which is hidden somewhere in your hard disk.

Anybody knows of any solution or work around? It seems to be happening to a few more people as well.

20070713 Thumbs up for FlashDevelop

Some weeks ago I said bye bye to php, html, css and all those pain inducing drugs for a while, and switched to work mainly with Flash. At the beginning I was using only the Flash IDE and external editors, as I had done since ActionScript2 appeared on stage (pun!!). But I still had a pending task in my to-do list: to have a look at FlashDevelop, specially since Zarate talks so much about it ;-)

I started using FlashDevelop a couple of days ago and I must say that it absolutely makes a difference, and what difference! Once configured, it detects my classes, methods and properties (showing an Outline panel) and helps me writing code with some autocompletion love each time I press the magical dot key, unfolding a list with available methods and properties for the active object.

I suddenly got back that I’m programming for a compiled language feeling. It was so cool! It felt like programming in Java again, but without Java annoyances.

There are some small details that I still haven’t had time to tweak, like the sorting method in the outline (currently it seems to be listing everything in the same order than it was defined), or configure it to use Flex2 compiler and all that. But even if I’m just scratching the surface of what can be done with this program, I strongly recommend it. The autocomplete feature is worth a try by itself!

On a side note, I still don’t get how Flash’s IDE is so 1998 in what regards to the Code editor (the Actions panel). It looks as if it hasn’t been given much attention, presumably because they dedicated more resources to Flex and its editor instead.

20070703 VerifyError: Error #1025: An invalid register 1 was accessed

If you need to parse some XML with namespaces in ActionScript 3 you may find this error quite soon: VerifyError: Error #1025: An invalid register 1 was accessed

It all comes because of this line:

default xml namespace = myXML.namespace();

It is needed in order to access the values in the namespaced value so removing it is not an option. The problem appears when you call any other function in your object… and you get that weird error.

I haven’t managed to find out why. It kind of seems that changing the namespace (as the very example from Flash’s reference on how to read an RSS feed suggests) also alters the namespace of later function calls, and so the interpreter can’t find the functions.

I found the simplest solution was to reset the namespace back to nothing when I was done with accessing the XML:

default xml namespace = new Namespace('');

There should be an explanation for this but I haven’t yet found it. Meanwhile this can save your life :-)