Hiding the Mouse in Flash

Hiding the mouse in a Flash app should be a simple matter of calling flash.ui.Mouse.hide().  This works totally fine in the standalone player.

However, in some browsers that doesn’t actually hide the mouse all the time.  For example, this has been a problem off and on in Chrome/Chromium for some time on various platforms.  In Linux under Chromium (build 20.0.1132.57), I could not figure out any real pattern to making it hide or not using just flash.ui.Mouse.hide().  Sometimes it works, sometimes it doesn’t.  You see this a lot in Flash games that show a custom display list cursor under the native system cursor; the latter didn’t hide properly.

Fortunately, assuming you’re using Flash >= 10.2, there is a seemingly reliable workaround.  Set the mouse cursor to the default cursor (!), then hide the mouse like usual.  E.g., in Haxe/NME:

  nme.ui.Mouse.cursor = flash.ui.MouseCursor.AUTO;
  nme.ui.Mouse.hide();

Note that MouseCursor isn’t wrapped by NME so you need to refer directly to the Flash classpath, and it only works for the flash target.

Setting Mouse.cursor triggers Flash’s native cursor support. Presumably doing that is in turn changing some interaction or state between Flash and the browser, such that Mouse.hide() seems to actually reliably hide the mouse.

In RocketHaxe this happens automatically on the Flash platform, e.g., by calling com.rocketshipgames.haxe.ui.Mouse.disable().

Singletons in Actionscript

The code below demonstrates a mechanism for creating a singleton class in Actionscript. The DebugPanel is only accessible via DebugPanel.instance, which ensures that one and only one instance are ever created. It uses the fact that the Singleton class cannot be accessed outside the DebugPanel scope, preventing anyone from calling the DebugPanel constructor, to enforce this.

Note that this is not a typechecked approach, so it throws runtime rather than compile time errors. There definitely may be better mechanisms for implementing this structure. Whether or not singletons are a good idea is also up to you. I think the places where they won’t come back to bite you in the ass are limited in number, but definitely exist.

package com.rocketshipgames.core
{

  public class DebugPanel {

    private static const _instance:DebugPanel =
      new DebugPanel(Singleton);

    public function DebugPanel(lock:Class)
    {
      if (lock != Singleton) {
	throw new
          Error("Invalid DebugPanel singleton instantiation.");
      }
    }

    public static function get instance():DebugPanel
    {
      return _instance;
    }

    // end DebugPanel
  }

  // end com.rocketshipgames.core
}

class Singleton {
  // end Singleton
}