Skip to main content
setht94929637
Participating Frequently
August 3, 2018
Answered

Can I create a AS3 menu without buttons?

  • August 3, 2018
  • 2 replies
  • 918 views

I'm using Flash CS6.

I've found a lot of menu tutorials for creating menus with buttons, but what I want to do is a little different.

I want the animation will begin on a blank screen, then if the user hits any key, the menu options will appear. There will be 7 options, each assigned a number 1-7. I want the user to be able to hit that number on their keyboard to select that menu option.

I've created all the assets and animations for my project, but can't figure out how to script it.

Any help?

This topic has been closed for replies.
Correct answer Ned Murphy

Google for tutorials/info regarding "AS3 keyboard event listeners"... here's couple links from such a search to get you started

KeyboardEvent - Adobe ActionScript® 3 (AS3 ) API Reference

Adobe Flash Platform * Capturing keyboard input

AS3: Keyboard Interaction

2 replies

JoãoCésar17023019
Community Expert
Community Expert
August 3, 2018

Hi.

Adding to what Ned suggested, here is a suggestion of how this be can be achieved.

P.S.: Forgot to mention that the Esc key won't work while testing the app inside of Animate CC. You have to run the SWF from the OS file explorer.

Preview:

AS3 code:

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import fl.transitions.Tween;

import fl.motion.easing.*;

var menu:MovieClip = sideMenu;

var optionsFrames:Vector.<uint> = new <uint>[1, 2];

var optionsNames:Vector.<String> = new <String>

[

     "Option 1",

     "Option 2",

     "Option 3",

     "Option 4",

     "Option 5",

     "Option 6",

     "Option 7",

     "Exit",

];

var optionsKeys:Vector.<uint> = new <uint>

[

     Keyboard.NUMPAD_1,

     Keyboard.NUMPAD_2,

     Keyboard.NUMPAD_3,

     Keyboard.NUMPAD_4,

     Keyboard.NUMPAD_5,

     Keyboard.NUMPAD_6,

     Keyboard.NUMPAD_7

];

var exitKeys:Vector.<uint> = new <uint>

[

     Keyboard.ESCAPE,

     Keyboard.SPACE

];

function start():void

{

     setMenu();

     stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

}

function setMenu():void

{

     for (var i:int = 0, total:int = menu.numChildren; i < total; i++)

          (menu.getChildAt(i) as MovieClip).txt.text = optionsNames;

}

function callMenu():void

{

     var tween:Tween = new Tween(menu, "x", Back.easeOut, menu.x, stage.stageWidth - menu.width, 0.5, true);

}

function dismissMenu():void

{

     var tween:Tween = new Tween(menu, "x", Sine.easeIn, menu.x, stage.stageWidth, 0.5, true);

}

function highlightOption(index:int):void

{

     for (var i:int = 0, total:int = menu.numChildren; i < total; i++)

     {

          var option:MovieClip = menu.getChildAt(i) as MovieClip;

          if (i != index)

               option.gotoAndStop(optionsFrames[0]);

          else

               option.gotoAndStop(optionsFrames[1]);

     }

}

function keyHandler(e:KeyboardEvent):void

{

     if (e.type == KeyboardEvent.KEY_DOWN)

     {

          if (exitKeys.indexOf(e.keyCode) == -1)

          {

               callMenu();

               highlightOption(-1);

          }

          if (exitKeys.indexOf(e.keyCode) > -1)

          {

               dismissMenu();

               highlightOption(menu.numChildren - 1);

          }

          else if (optionsKeys.indexOf(e.keyCode) > -1)

               highlightOption(optionsKeys.indexOf(e.keyCode));

     }

}

start();

FLA download:

flash_cs6_as3_menu_with_key_navigation.zip - Google Drive

I hope this helps.

Regards,

JC

setht94929637
Participating Frequently
August 3, 2018

This is amazing! Thanks.

JoãoCésar17023019
Community Expert
Community Expert
August 3, 2018

You're welcome!

P.S.: Forgot to mention that the Esc key won't work while testing the app inside of Flash CS6/Animate CC. You have to run the SWF from the OS file explorer.

Ned Murphy
Ned MurphyCorrect answer
Legend
August 3, 2018

Google for tutorials/info regarding "AS3 keyboard event listeners"... here's couple links from such a search to get you started

KeyboardEvent - Adobe ActionScript® 3 (AS3 ) API Reference

Adobe Flash Platform * Capturing keyboard input

AS3: Keyboard Interaction

setht94929637
Participating Frequently
August 3, 2018

Thank you! I'm very new to AS3 coding, so I don't even know what a lot of these things are called so I don't know what to search for.

Cheers.