The right way

flash right click

This post may be totally opinion based. And I, for one, welcome our new possibilities in programming with Flash since version 11.2 and the useful feature of the RIGHT CLICK. YES! I am happy about this. This really is a step towards gaming in Flash. AND: Despite all the nice things one could add to the context menu, I do like the fact that it can be disabled now. Check this out:

A left click rotates the sprite CCW while a right click rotates it CW. Isn’t that cool? And there is no annoying context menu. Wooh! AlRIGHT, getting into the code:

Main.as

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author kegogrog
	 */
	public class Main extends Sprite 
	{
		
		public var sprite:Sprite;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			sprite = new Sprite();
			sprite.graphics.beginFill(0xff5500, 1);
			sprite.graphics.drawRect( -100, -50, 200, 100);
			sprite.graphics.endFill();
			sprite.x = 320;
			sprite.y = 240;
			stage.addChild(sprite);
			
			stage.addEventListener(MouseEvent.RIGHT_CLICK, rightClickListener);
			sprite.addEventListener(MouseEvent.CLICK, leftClickListener);
		}
		
		private function rightClickListener(event:Event):void
		{
			if (event.target == sprite)
			{
				sprite.rotation += 5;
			}
		}
		
		private function leftClickListener(Event:MouseEvent):void
		{
			sprite.rotation -= 5;
		}
		
	}
	
}

It is pretty simple. All that I have done here is adding a sprite to the stage, adding a listener to the stage for right click and a listener for the normal left click to the sprite. The stage listener disables the context menu, and the target check within rotates the sprite if it in fact is under the mouse. No rocket science here. And yes, I am back, looking into Blender, Flare and all that other cool stuff…

Yoho!

This entry was posted in flash, Snippet. Bookmark the permalink.

Comments are closed.