Published on November 27, 2009
in AS3 and Flash.
I recently stumbled over a new AS3 gotcha that seems to be bugging a lot of the Flash community. Apparently a new 'feature' of AS3, whereby using the mousewheel inside an embedded Flash movie still scrolls the surrounding browser window (if it has enough content, that is).
This didn't happen in AS2. If a Flash movie had focus, mousewheel events weren't sent to the browser window. It wasn't a problem in FireFox either, until a recent update and adversely affects any AS3 Flash app embedded in an HTML page where you might want to scroll or zoom with the mousewheel while over the Flash, instead of the scrolling the browser window.
So, I decided to cook up some code to fix this. While it's early version and isn't perfect, it suits my purposes, is easy to set up and doesn't require any external JavaScript - so I'd thought I'd share it with the world, in case Adobe never get around to addressing this 'feature'.
Introducing MouseWheelTrap. A handy little utility class that traps mousewheel events while the mouse is over the Flash, so your app scrolls how it was intended to. I've tested it with SWFObject on PC IE and FireFox, but not with SWFMacMouseWheel on Mac (if someone could tell me if it works). I decided against using jQuery for trapping mouse events, partly out of laziness, partly because many people have no control over the HTML that embeds their Flash app and therefore can't add custom JavaScript.
Setting up MouseWheelTrap is easy:
Download the MouseWheelTrap ZIP package, or just the AS file.
Unzip the package and have a look at the demo, or just take the MouseWheelTrap.as file and put it in the com.spikything.utils folder into your own project or classpath.
Import the utility and set it up somewhere in your main class like so:
import com.spikything.utils.MouseWheelTrap;
MouseWheelTrap.setup(stage);
Simple huh? Let me know how you get on...
Published on October 17, 2009
in Flash.
I've seen a few people get into a pickle over this one. When you're developing and testing locally, you need to set the 'Local Playback Security' setting (sometimes referred to as the 'use network services' option) depending on whether you wish to access local external files (e.g. XML files, or images) or some other server (e.g. your dev backend server). You can't access both from a locally running SWF anymore, since it's a security risk. So here's how to set that option in Flash CS3 / CS4 or from within FlashDevelop.
Published on September 6, 2009
in AS3 and Flash.
On my travels as a contractor, I've seen various methods used for embedding fonts in ActionScript 3 projects (code embedded, creating font SWFs, runtime loading, etc). Each of them has its own merits (and limitations), but generally you're looking for something that's easy to do, easy to maintain (for others, not just yourself) and of course that works!
So, here's the method I use for pure AS3 projects I compile with the Flex SDK. I embed the TTF or OTF file in a 'Style' class and include those font files in the CVS/SVN repository too - it does my head in when fonts go missing, because a designer can't remember which font they used:
import flash.
text.
Font;
import flash.
text.
TextFormat;
// UNICODE RANGE REFERENCE
/*
Default ranges
U+0020-U+0040, // Punctuation, Numbers
U+0041-U+005A, // Upper-Case A-Z
U+005B-U+0060, // Punctuation and Symbols
U+0061-U+007A, // Lower-Case a-z
U+007B-U+007E, // Punctuation and Symbols
Extended ranges (if multi-lingual required)
U+0080-U+00FF, // Latin I
U+0100-U+017F, // Latin Extended A
U+0400-U+04FF, // Cyrillic
U+0370-U+03FF, // Greek
U+1E00-U+1EFF, // Latin Extended Additional
*/
[Embed(source = '../fonts/myfont.ttf', fontName = 'MY_FONT',
fontWeight = 'regular', unicodeRange =
'U+0020-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E',
mimeType = 'application/x-font')]
public static const MY_FONT :Class;
public static const DEFAULT_FONT :String = "MY_FONT";
public static const DEFAULT_TEXT_COLOUR :int = 0xFFFFFF;
public static const DEFAULT_TEXT_SIZE :int = 14;
public static const MY_TEXT_FORMAT :TextFormat = new
TextFormat(DEFAULT_FONT, DEFAULT_TEXT_SIZE, DEFAULT_TEXT_COLOUR);
Through experience, this seems to be the most robust and maintainable way of dealing with fonts (not to throw out more flexible, but perhaps less robust ways, such as runtime loading), especially when you need control of the unicode ranges you are embedding - which I have included for reference.
Published on August 23, 2009
in AS3 and Flash.
Here's a quick snippet of code for integrating your SWFs with JavaScript, using ExternalInterface:
import flash.
external.
ExternalInterface;
// Calling a JavaScript function from AS3 (with optional parameters)
if (ExternalInterface.available) ExternalInterface.call("javascriptFunction", param1, param2);
// Implementing a JavaScript callback in AS3 (flashFunction is called when JavaScript calls javascriptFunction)
if (ExternalInterface.available) ExternalInterface.addCallback("javascriptFunction", flashFunction);