AS3 Right-click functionality

I have heard much talk over the last couple years about “Right Click” functionality in Flash. I never paid puch attention to it as I never really needed it ever. Just the other day though it came up yet again and someone linked an article to this guy Uza’s blog who had a JScript for getting the functionality in AS3. The original code project can be found here caught my interest for some reason that I can’t explain. It probably had to do with the game library I’m coding, I figured some developers might actually want this in their games, so why not add it in!?

Unfortunately his script only informed flash of when the right mouse button was pressed over the Flash video. So I tweaked the script some to add release functionality as well.

To make this right click functionality blend well I created a Singleton class that emulates normal Flash MouseEvent protocols. You get a lot of the normal features you may already be used to with left-click all wrapped up nicely in this Singleton.

  • MouseEvents are dispatched into the event pool like other events in flash
  • MouseEvents are dispatched by the top most InteractiveObject that was right-clicked
  • MouseEvents bubble through the DisplayList like usual
  • Three Special events are registered and act just like left button events: rightMouseDown, rightMouseUp, and rightClick
  • An optional ability to check if the object is mouse-cancelled via its mouseEnabled property or any parents mouseChildren property

The final option may be considered processor intensive in some situations and can be optionally turned on/off. When off then the top most InteractiveObject is just returned as the target no matter if it is enabled or not. Unless you have tons of overlapping InteractiveObjects though, it really shouldn’t matter either way.

So lets introduce you to the script!

First we have the javascript which is necessary. I need to nod my hat at Uza as it really is his script with some modifications and tweaks. As for using in your project, well it’s done nearly the same exact way as Uza’s script is, only difference is the name of it slightly different. So here’s the script:

All you need to do is import LoDRightMouseButtonTrap into your html document and initialize it. When you initialize it you tell it what SWFObject to pay attention to:

LoDRightMouseButtonTrap.init( “idOfSWFObject”, “idOfFlashContainer” );

This way LoDRightMouseButtonTrap.js knows how to locate the SWF and communicate to it. At this time you can only support one SWF per page at a time. I’ll be changing that in the future of course. But lets get this out the door right now for you guys.

Now lets take a look at the Singleton that handles this thing:

There it is, yes it’s a little long. But that’s ok, it all runs fine and for you to utilize it is really freakin’ easy.

import com.lordofduct.ui.RightClickManager;
RightClickManager.instance.start( this.stage );

And there, it’s up and running. Keep in mind, the SWF MUST be running in an html container and have the .js script running at the same time. If this isn’t settled then it doesn’t work at all. So no, this does not work on locally running SWFs via a projector. If you want local running access, run it via a browser locally, OR just create an AIR app which gives you RightClick functionality out of the box. That was one of my drives for writing this, if you wanted to port your internet app to an AIR app, you just don’t start up the RightClickManager and just keep on going. The events are dispatched with the same MouseEvents that AIR would be dispatching… actually seeing as the manager check for the ExternalInterface at “start”, if it isn’t there, it just doesn’t run. So really you don’t have to change ANYTHING to port an app using this to AIR.

So who wants an example!?

Here I draw 3 boxes named child1 through child3, and stick them in various containers, then I listen to the root for right clicks and display some text for it. Note how everything bubbles and the correct displayObject is picked up here in the example running:

CLICK HERE FOR EXAMPLE!

SOURCE CODE

Please check out my game framework for the classes needed. You are intereseted in:

com.lordofduct.ui.RightClickManager – this is the actual manager
com.lordofduct.util.Assertions – needed for nil checking
com.lordofduct.util.SingletonEnforcer – needed to assert the Singleton instance
js.lordofduct.ui.LoDRightMouseButtonTrap.js

The latest version of it is available in the right hand link bar.

OR HERE!

LoD AS3 Game Framework

I’m sorry I haven’t been around in several months. I’ve been off doing a whole lot of stuff and completely lost track of the site. My apologies to everyone… I guess I’m kind of new to this whole blogging thing and haven’t quite gotten a hand of setting time aside for updating here. Along the same lines I’ve been doing a lot of contractual work which I couldn’t share any drop of due to agreements and really had nothing to talk about in consequence.

What I have in return though, for those of you who actually waited around for me, is the preperations for a nice little library some of you may find helpful.

This library is being released “AS IS” at this moment and is under severe development. Think of it as a pre-alpha non-build version with very little functionality. It’s more or less to show you some of the fun tools I hope to include in it.

But wait… what kind of library is it LoD??? Well it’s a Game Library. It’s to be a nice collection of different tools for creating 2D games in Actionscript 3. It’ll include many things like:

AI engine
Physics engine
Game Controller interfaces
Tweening engines
Game View Manager
Parametric Curves (at this moment it is a reskinned version of Jim Armstrong’s Singularity Parametric Curve library, used with his permission)
and much more

There is much work to come, and it won’t be done for some time. But I’ll be updating with majour points along the way for you guys to look at. Furthermore I’d like input on what kind of features you want added (please no 3D requests, I’d like too, but I want to first see where Flash goes with their built in 3D first). When you review what I have to offer right now, keep in mind I’ve yet to test full scale test runs on anything in here and much of it could crash your application. Some of the classes are completely barren and unuseable as I’ve only started to write their interfaces. Lastly a lot of it will probably be moved around a bit to meet stronger demands.

Check it out, tell me what you think!