AS3 Mouse Wheel Trouble

by Mike Welsh on March 12, 2011

If you’ve ever tried to add mouse wheel support to a Flash web app, it’s likely you’ve run into this problem: Scrolling the mouse wheel will scroll the web page, regardless of any mouse wheel listeners or code you have inside the Flash. There seems to be no way to easily prevent the web page from scrolling. Here’s an example.

This is a regression from AS2. In AS2, adding a mouse wheel listener would prevent the web page from scrolling. Not so in AS3. Even preventDefault doesn’t seem to help.

[click to continue…]

{ 7 comments }

Julia sets on the GPU with Molehill

by Mike Welsh on March 2, 2011

Everyone has been buzzing about Molehill, the new low-level 3D API that will be available in Flash Player 11. This API finally gives Flash developers the GPU acceleration that they’ve been waiting for. It’s a wrapper over OpenGL/DirectX, giving you a nice interface with a OpenGL ES 2.0 featureset. That’s right, all the Z-buffered triangles and shaders your heart desires. :)

Julia set rendered in Flash Player Incubator

Julia set rendered in Flash Player Incubator

Adobe released a public beta of Flash Player 11, so I grabbed it and tried to get my Pixel Bender Julia set plotter running in the player. Unfortunately, Adobe hasn’t yet released the latest Pixel Bender Toolkit, which you would use to compile shaders in Adobe’s shader format for use with Molehill. But then HxSL appeared, from the brilliant Nicolas Cannasse — a shader compiler released before Adobe’s official tools!

[click to continue…]

{ 2 comments }

ActionScript 3 Version Checking

by Mike Welsh on September 1, 2010

The Flash Player makes a decent effort to be forward-compatible. When you run an Flash Player 10 targeted SWF in Flash Player 9, it’ll run fine until you start using Flash Player 10 specific features. At best, the code will just fail silently or pop up an error dialog. But at worst, the file will fail to play at all! Here are some examples of features that will cause errors if the user is running an old version:

  • 3D transformations.
  • The Vector class.
  • Dynamic audio using SampleDataEvent.
  • Global error handling.
  • Multitouch and accelerometer events.

Even seemingly innocuous code can cause problems, such as Event.ADDED_TO_STAGE—it was first added in Flash Player 9 Revision 1! Therefore, it’s very important to check that the user has the Flash Player version that we expect. Otherwise, you may get some nasty reviews stating that your game refuses to work. :)

[click to continue…]

{ 2 comments }

Memory management is tricky when working with a garbage collector. Throw in a complex display list hierarchy with lots of event handlers, and it’s definitely a challenge! It’s very easy to overlook some hidden object reference, causing objects to not be marked as garbage. Soon your memory usage is spiraling out of control! This has lead many developers such as Grant Skinner and Ted Patrick to suggest always setting useWeakReference to true when you call addEventListener.

This is indeed a good idea and can be a helpful failsafe, but I think that it fails to emphasize another important action: Always remove your event listeners. I fear that many developers may be given a false sense of security by always using weakly referenced listeners, so I’ve tried to create a clear description of how event listeners create references and how to clean them up. This assumes you have an idea of how Flash’s mark-sweep garbage collector works. If not, check out Grant’s excellent AS3 Resource Management articles, which are a must read for any Flash developer.

[click to continue…]

{ 9 comments }