MouseWheelTrap – how to stop simultaneous Flash / browser scrolling in AS3

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:
  • Actionscript:
    1. import com.spikything.utils.MouseWheelTrap;
    2. MouseWheelTrap.setup(stage);

    Simple huh? Let me know how you get on...

    Posted in AS3, Flash | Tagged , , , , , | 8 Comments

    CoolIris type thing in 30 lines

    Actionscript:
    1. package { // Piclens type thing in 30 lines (FP10+) Liam O'Donnell - spikything.com
    2.     import flash.display.Sprite;
    3.     import flash.events.Event;
    4.     public class Main extends Sprite {
    5.         private var container :Sprite;
    6.         private var imageGrid :Sprite;
    7.         private var images :Array = [];
    8.         public function Main():void {
    9.             container = addChild(new Sprite()) as Sprite;
    10.             imageGrid = container.addChild(new Sprite()) as Sprite;
    11.             for (var i:uint = 0; i <200; i++) images.push(getItem(i));
    12.             images.sortOn("z", Array.NUMERIC | Array.DESCENDING);
    13.             for each (var item:Sprite in images) imageGrid.addChild(item);
    14.             addEventListener(Event.ENTER_FRAME, update);
    15.         }
    16.         private function getItem(index:uint):Sprite {
    17.             var item:Sprite = new Sprite();
    18.             item.x = -(200 / 3) * 210 / 2 + (index / 3) * 210;
    19.             item.y = (index % 3) * 160 - 40;
    20.             item.z = 100 + Math.random() * 2000;
    21.             item.graphics.beginFill(Math.random() * 0xffffff);
    22.             item.graphics.drawRect(0, 0, 200, 150);
    23.             return item;
    24.         }
    25.         private function update(e:Event):void {
    26.             imageGrid.x += ((stage.stageWidth / 2) - mouseX) * .2;
    27.             container.rotationY = ((stage.stageWidth / 2) - mouseX) * .2;
    28.         }
    29.     }
    30. }

    Posted in 3D, AS3, Flash | Tagged , , , , | 3 Comments

    Local playback security in FlashDevelop and Flash CS3 / CS4

    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.

    Posted in Flash | Tagged , , | Leave a comment

    Turkey

    Whirling dervishes in TurkeyI recently went to Turkey and travelled around the entire Western half of the country. The highlight was hot air ballooning over Cappadoccia, but I just found this long exposure photo of some whirling dervishes I took and thought it was worth posting.

    Posted in Photos, Travel | Leave a comment

    How to embed fonts in pure AS3

    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:

    Actionscript:
    1. import flash.text.Font;
    2. import flash.text.TextFormat;
    3.  
    4. // UNICODE RANGE REFERENCE
    5. /*
    6. Default ranges
    7. U+0020-U+0040, // Punctuation, Numbers
    8. U+0041-U+005A, // Upper-Case A-Z
    9. U+005B-U+0060, // Punctuation and Symbols
    10. U+0061-U+007A, // Lower-Case a-z
    11. U+007B-U+007E, // Punctuation and Symbols
    12. Extended ranges (if multi-lingual required)
    13. U+0080-U+00FF, // Latin I
    14. U+0100-U+017F, // Latin Extended A
    15. U+0400-U+04FF, // Cyrillic
    16. U+0370-U+03FF, // Greek
    17. U+1E00-U+1EFF, // Latin Extended Additional
    18. */
    19.  
    20. [Embed(source = '../fonts/myfont.ttf', fontName = 'MY_FONT',
    21. fontWeight = 'regular', unicodeRange =
    22. 'U+0020-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E',
    23. mimeType = 'application/x-font')]
    24. public static const MY_FONT :Class;
    25.  
    26. public static const DEFAULT_FONT :String        = "MY_FONT";
    27. public static const DEFAULT_TEXT_COLOUR :int = 0xFFFFFF;
    28. public static const DEFAULT_TEXT_SIZE :int = 14;
    29. public static const MY_TEXT_FORMAT :TextFormat = new
    30. 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.

    Posted in AS3, Flash | Tagged , , , | 1 Comment