Easily handle right mouse button in Flash movies
Detecting and handling right mouse clicks in Flash has always been a pain.
I blogged about it four years ago in the post preventing the right mouse cheat in your Flash games where I explained a dirty trick to trigger right mouse button with As2, and guess what? Things got even worse with As3, although I found a workaround preventing players to cheat in my Flash game Stringy.
Finally, the pain is over. With Flash Player 11.2, you can finally do everything you want in Flash movies, without exporting them for Air.
Look at this movie:
You can use left mouse button to drag boxes, and right mouse button to destroy them. Also notice, the default right click menu does not appear.
Look at the source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*; public class Main extends Sprite { private var world:b2World; private var worldScale:Number=30; private var mouseJoint:b2MouseJoint; private var debugSprite:Sprite=new Sprite(); public function Main() { world=new b2World(new b2Vec2(0,9.81),true); debugDraw(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(320/worldScale,470/worldScale); var polygonShape:b2PolygonShape=new b2PolygonShape(); polygonShape.SetAsBox(320/worldScale,10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; var groundBody:b2Body=world.CreateBody(bodyDef); groundBody.CreateFixture(fixtureDef); var box:b2Body for (var i:int=0; i<20; i++) { bodyDef.position.Set((Math.random()*400+120)/worldScale,(Math.random()*400)/worldScale); bodyDef.type=b2Body.b2_dynamicBody; polygonShape.SetAsBox(30/worldScale,30/worldScale); fixtureDef.density=1; fixtureDef.friction=0.5; fixtureDef.restitution=0.2; box=world.CreateBody(bodyDef); box.CreateFixture(fixtureDef); } addEventListener(Event.ENTER_FRAME,updateWorld); stage.addEventListener(MouseEvent.MOUSE_DOWN,createJoint); stage.addEventListener(MouseEvent.RIGHT_CLICK,destroyBody); } private function destroyBody(e:MouseEvent):void { world.QueryPoint(queryCallbackDestroy,mouseToWorld()); } private function createJoint(e:MouseEvent):void { world.QueryPoint(queryCallbackJoint,mouseToWorld()); } private function queryCallbackJoint(fixture:b2Fixture):Boolean { var touchedBody:b2Body=fixture.GetBody(); if (touchedBody.GetType()==b2Body.b2_dynamicBody) { var jointDef:b2MouseJointDef=new b2MouseJointDef(); jointDef.bodyA=world.GetGroundBody(); jointDef.bodyB=touchedBody; jointDef.target=mouseToWorld(); jointDef.maxForce=1000*touchedBody.GetMass(); mouseJoint=world.CreateJoint(jointDef) as b2MouseJoint; stage.addEventListener(MouseEvent.MOUSE_MOVE,moveJoint); stage.addEventListener(MouseEvent.MOUSE_UP,killJoint); } return false; } private function queryCallbackDestroy(fixture:b2Fixture):Boolean { var touchedBody:b2Body=fixture.GetBody(); if (touchedBody.GetType()==b2Body.b2_dynamicBody) { world.DestroyBody(touchedBody); } return false; } private function moveJoint(e:MouseEvent):void { mouseJoint.SetTarget(mouseToWorld()); } private function killJoint(e:MouseEvent):void { world.DestroyJoint(mouseJoint); mouseJoint=null; stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveJoint); stage.removeEventListener(MouseEvent.MOUSE_UP,killJoint); } private function mouseToWorld():b2Vec2 { return new b2Vec2(mouseX/worldScale,mouseY/worldScale); } private function debugDraw():void { var debugDraw:b2DebugDraw=new b2DebugDraw(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(worldScale); debugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit); debugDraw.SetFillAlpha(0.5); world.SetDebugDraw(debugDraw); } private function updateWorld(e:Event):void { world.Step(1/30,10,10); world.ClearForces(); world.DrawDebugData(); } } } |
Without entering in detail into Box2D coding, which is just the drag and drop example you can find in the official demo, look at the listener at line 39, which is the core of the script.
Then, you can easily publish the movie for Flash Player 11.2, thanks to Flash Professional CS6 you can download and try for 30 days at this link.
Just notice right mouse clicks examples will only work in browsers, testing them from the Flash IDE won’t work.
What else to say? How many new game concepts can you imagine using both left and right mouse buttons?
They can be easily customized to meet the unique requirements of your project.





(10 votes, average: 4.30 out of 5)






This post has 8 comments
Chris
It’s nice that you don’t have to use JavaScript anymore to try to handle right-clicks
I guess this will also make it harder to distinguish if something is flash or html5 just by right-clicking on it ;) Maybe that is their motivation for finally adding it?
I wonder if they just added their own JS file to handle, and pass back right clicks, since it doesn’t work outside of the browser?
kaes
Hmm I still can see right-click menu and boxes do not disappear after right-clicking on them. There is no difference for me (chrome browser)
Lunux
Another way to do it(min req Flash Player 9)
import flash.events.ContextMenuEvent;
import flash.ui.ContextMenu;
var cmenu = new ContextMenu();
cmenu.addEventListener(ContextMenuEvent.MENU_SELECT , menuSelectHandler);
contextMenu = cmenu;
function menuSelectHandler(e:ContextMenuEvent):void
{
trace(“you are cheating!!!”);
}
David
Shame it doesn’t work outside the browser, as some of our flash games are now developed as standalone files. :-(
Pablo
Yeah, its nice indeed, the only problem so far is that Starling doesn’t support it, I hope that situation will change soon.
Husky
Cool!
Waiting for too long!
paala
If you are using flash develop 4.0.3 you don;t have to open the swf in browser.
luis
ufff ahora si voy a poder quitar ese molesto menĂº, gran aporte emanuele gracias