Thursday, August 23, 2007

Building Flash web widgets: right click



If you are thinking "when is this guy going to finish speaking about 'building flash web widgets'"?
well, I still have got two more things to talk about (sorry, I know I said that before...), but my idea is to write as much as possible so you have a better understanding about "best practices" when building widgets ;)

This time is about managing the "right click" from the mouse and this is considered best practice in any web application, not only on widgets.
Best practice?
why do we need to care?
well, you never know what a users is going to do with your application and is better to prevent possible bugs testing whatever is possible to do. For instance, whenever I'm checking someone else's work, I'm always using the right click to zoom-in to their applications, press play, etcetera as I'm very curious and want to know if the developer cares about details; trust me it makes a difference.

The first thing we need to do is to get rid of the "built-in items", yes, the same from the picture above, and to do so we need only one line of code:

Stage.showMenu = false;


Adding that simple line helps us to avoid the user to do something we didn't want him/her to do like pressing "rewind" or "play" (if using more than one frame). I normally use one only frame so the right click menu doesn't show some options but still it shows the "zoom-in", "quality" & "print" among others. However, with this line of code we won't be able to get rid of all the items, for example the "about adobe flash player 9..." as is part of the "copyright" from Adobe.

And talking about copyrights, can we add our own copyright or something like that?
certainly we can and the way to do so is with the following actionscript in the first frame:

function linkToMe():Void {
getURL("http://overloadstudios.blogspot.com/");
}
function linkToDistributor():Void {
getURL("http://www.widgetbox.com");
}
var cmMenu:ContextMenu = new ContextMenu();
cmMenu.hideBuiltInItems();
cmMenu.customItems.push(new ContextMenuItem("Built by Overload Studios", linkToMe));
cmMenu.customItems.push(new ContextMenuItem("Powered by widgetbox", linkToDistributor));
this.menu = cmMenu;


We won't need the Stage.showMenu = false; anymore as with this code we are hiding the "traditional" menu.
What we have are two functions to link to this blog and to the widget distributor, in this case Widgetbox.
Then we are instantiating the class "ContextMenu", hiding the "built in items" and adding two items to the new "array" of items.
The last line is to tell flash which menu should show.
You can add more items if you want to or remove one or the other, but remember to add this right click menu to all your widgets to allow users to know who built the widget and to visit your website; this is a nice way to show that you are the developer and not with a cheese roll over banner as other people do. :P

Salut!

3 comments:

sshong said...

is there any method to prevent the about player and settings contextmenu?

Ernesto said...

Not that I know sshong. I've seen however, if in case you're looking to get rid completely of the right click menu, that using javascript you would be able to accomplish this.

salut

roman said...

It works great except I can't get rid of the show redraw regions... how do i disable it in flash 8?