negush blog

Flash, ActionScript and stuff…

Archive for October, 2008

October-13-08

Package-level functions

posted by admin

Sometimes, in your projects, there’s the need for a function to execute some general actions and you would need that function in multiple applications/projects. You could place that function inside a utilitarian class, import that class and then use that function. But what happens if you usually do not use the other functions defined in the class ? They would be imported as well, even if you don’t need them.

In this case you could define that function directly inside a package and not inside a class. This way, you only need to import the function you want to use. These functions defined in packages are called package-level functions. A good example of such a function is navigateToURL() from the flash.net package, used to open URLs, or getDefinitionByName() from the flash.utils package, used to get a reference to a class object.

The way to define a package-level function is quite simple. All you have to do is define that function like you would do it inside a class, with the exception that the function definition is located directly inside a package:

package myPackage {
    public_or_internal functionName(...params):DataType {
    }
}

Now, all you need to do is import the entire package (if you need it) or just that specific function (in this case the public access specifier):

import myPackage.functionName;
functionName(param1, param2...);

Note: when you save the file containing the function definition, make sure you give it the same name as the function (just like in class declarations). In this case the file name would be functionName.as.

October-4-08

Flash font size limit

posted by admin

It seems that Flash has a limit for the font size. Normally, it displays the text with a font size of up to 127 px. So I tested it to see if there’s any workaround for this limitation and the easiest thing to do is set the size of the font to less than 128 px and then scale the text field until the size of the font suites your needs. This is what Colin Moock recommends in his article.

However, it seems that there is an exception to this font size rule: if you add a text field to the Stage using the Text tool in design time and not using code in runtime, it lets you use font sizes greater than 127 px (I’ve tested it with a font size of 300 px) ONLY AND ONLY if you DO NOT USE the “Render text as HTML” option. I’ve tested it with all three types of text field: dynamic, static and input; with and without embedded fonts; with single line and multiline; with and without border; using device fonts, bitmap fonts and anti-aliased fonts. And it worked with several types of fonts.

So, if you would like to use font sizes greater than 127 px and your text is static, just place that text in design time and don’t use it as HTML text. You can even update the contents by code and the font size will be kept, even when using the htmlText property to set the new text. But it seems that as soon as you use the “Render text as HTML” option on the text field, the font size limit kicks in. When you use code to create and set the text field, that rule always applies.

The new Flash Text Engine from Flash Player 10 no longer has this limit when using the new TextElement class, but the limit is still applied to all traditional text, according the Colin Moock’s article.