HOWTO: Using the ContextMenu in AS3 with FullScreen Mode As a Sample

Right clicking and other mouse support in flash is not really a shining star but in AS2/AS3 you *can* stuff the context menu with items. You can clear out all but Settings and About from the menu and stuff in other items that may pertain to a selected movieclip or the entire app you are building. This can be good when adding multiple functions into applications and for extra functions that you want to make available from a quick location such as full screen, selecting an item etc.

To enable a context menu for full screen capabilities take this example:

1) Make your document class of your AS3 Flash CS3 file to the class below.

package
{
import flash.display.*;
import flash.errors.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import flash.net.*;
import flash.media.*;
import flash.ui.*;
import flash.utils.*;
import flash.xml.*;
public class FullScreenTest extends MovieClip
{
public function FullScreenTest()
{
// create the context menu, remove the built-in items,
// and add our custom items
var fullscreenCM:ContextMenu = new ContextMenu();
fullscreenCM.addEventListener(ContextMenuEvent.MENU_SELECT, onContextMenuHandler);
fullscreenCM.hideBuiltInItems();
var fs:ContextMenuItem = new ContextMenuItem("Show Full Screen" );
fs.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onShowFullScreen);
fullscreenCM.customItems.push( fs );
var xfs:ContextMenuItem = new ContextMenuItem("Exit Full Screen");
xfs.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onShowNormalScreen);
fullscreenCM.customItems.push( xfs );

// THIS ASSUMES YOU HAVE A MOVIECLIP IN THE LIBRARY
// THAT IS SET TO A CLASS NAMED 'mc' that inherits from MovieClip
var m:MovieClip = new mc();
m.contextMenu = fullscreenCM;
addChild(m);
}
// functions to enter and leave full screen mode
function onShowFullScreen(event:ContextMenuEvent):void
{
stage.displayState = StageDisplayState.FULL_SCREEN;
}
function onShowNormalScreen(event:ContextMenuEvent):void
{
stage.displayState = StageDisplayState.NORMAL;
}
// function to enable and disable the context menu items,
// based on what mode we are in.
function onContextMenuHandler(event:ContextMenuEvent):void
{
if (stage.displayState == StageDisplayState.NORMAL)
{
event.target.customItems[0].enabled = true; // show full screen button
event.target.customItems[1].enabled = false; // hide normal screen button
}
else
{
event.target.customItems[0].enabled = false; // hide full screen button
event.target.customItems[1].enabled = true; // shoe normal screen button
}
}
}
}

2) Now Export to HTML and be sure to change the exported parameters in the HTML file to allowFullScreen = true (for the fullscreen effect) and menu = true (for the ability to add in context menu items) to whatever library you use for embedding flash:

   'menu', 'true',
   'allowFullScreen', 'true'

OR if using HTML parameters

<param name="allowFullScreen" value="true" />
<param name="menu" value="true" />

You can view a sample here. (right click on the movie clip and ‘show full screen’, then ‘hide full screen’)

You can download a sample FlashCS3/AS3 version here.

If you want more information on fullscreen check here and here (downloadable sample and component).

  • percy

    maravilloso, hace dias que uso el flash cs3 y no comprendia bien en modo full screen haciendo uso del menu, gracias por tu trabajo, me ayuda mucho.

  • Pingback: Disabling the context menu in as3.. | The Musings Of A Newman

  • http://www.envolveweb.com.br Marcus

    Hello everyone,

    That´s pretty cool sample of how to use the contextmenu in AS3, but i like to know how apply the context for the entire stage, like this _root.menu = fullscreenCM; (AS2 Version)

    Thanks

  • matt

    in the document class

    (this as MovieClip).contextMenu =

    MaTT

  • http://www.blogger.com proogounk

    I read some of the posts and I think it is a great blog. What do you think of my inviting responsiveness I have a joke for you =) What do termites do to relax? Take a coffee table break!!

  • http://www.inovare.net Felipe Giotto

    Good post!

    The original Flash documentation don’t told me to set the menu param to true. My menu was OK inside flash, but not inside the HTML. Now it’s working fine!

    Thank you!

  • http://www.photoretouchonline.ru/ Antanida

    Thanks! gut text

  • Nofefethine

    Hello

    As a fresh drawlogic.com user i only want to say hi to everyone else who uses this bbs B-)

  • emerrogeMunkCracemy

    kdyupbqbczsdgfiiwell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch ;)

  • http://sex-porevo.com razali

    Porno BEST!! http:sex-porevo.com

  • http://pianotuning.blogrun.net/ piano tuning oahu

    Не задавайте людям вопросов, на которые у них нет определенного мнения или на которые они не будут отвечать правдиво.

  • Herman

    dude! wtf is this?

    import flash.display.*;
    import flash.errors.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.media.*;
    import flash.ui.*;
    import flash.utils.*;
    import flash.xml.*;

  • http://drawk.com drawk

    Herman, haha, it is a prototype, you are supposed to optimize it.

    In actuality it is better to be strict for good form but it makes no difference in performance. The flash player only compiles classes it uses even if imported lazily as a wildcard. Wildcards are messy code but for prototypes I use them sometimes, the flash player is still smart enough to only compile what it uses. Not to mention it is about 2 year old code when AS3 was first kicking in.

    See notes on imports how it optimizes even if classes are imported that are not used.
    http://summitprojectsflashblog.wordpress.com/2008/10/04/as3-design-patterns-book-review-1-the-oreilly-one/

  • JatoIrrafefob

    Hey, just i just wanted to remind, tha global I4oTuFskPCc4 is near.

  • peparcarfrinc

    Dear Friends, a little 9-year old girl is crying out for our help. Olga is sick with cancer and urgently needs 100,000 Euro for bone marrow transplantation. Her parents do not have this money, but hope that kind and mercifull people somewhere in the world still exist.

    In order to continue her life, a little 9-year old Ukrainian girl Olga Netyukhailo desperately and urgently needs your help.

    Olga was diagnosed Acute lymphoblastic leukemia (blood cancer) and currently needs 100 000 Euros for bone marrow transplantation from unrelated donor.

    This type of surgery cannot be made in Ukraine, therefore, Olga’s parents brought a girl to Israel. Olga is hospitalized in “Hadassah” hospital (Jerusalem) and is staying there since December 2008. During these 3 month girl has been prepared for surgery – getting chemotherapy and waited for remission. But in addition to cancer, it was discovered that Olga’s lungs are infected with fungus, which makes the treatment more complicated.

    http://www.saveolga.tk/
    http://www.youtube.com/user/SaveOlga