Skip navigation
Lemon Video
Currently Being Moderated

Assign focus / keyboard listener to a movieclip

Jul 11, 2012 2:18 AM

Tags: #3 #focus #actionscript #keyboard #listener #movieclip

Hi,

 

I am having difficulty assigning a keyboard listener only when a specific movieclip is hovered:

 

The control works fine with the line:stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

 

But as soon as I change stage to 'tetrad' which is the name of my move clip nothing happens.

 

I know this is something to do with assigning focus - can anybody help with my code?

 

[AS]

     //Cell Control//

 

          import flash.ui.Keyboard;

          import flash.events.KeyboardEvent;

 

 

tetrad.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler(event:KeyboardEvent):void

 

 

{

    if (event.keyCode == Keyboard.ENTER)

    {

    var movieClip1:MyCellClass = new MyCellClass();

    addChild(movieClip1);

    movieClip1.x = mouseX - 10;

    movieClip1.y = mouseY - 10;

    movieClip1.name = "cell1";

 

          var movieClip2:MyCellClass = new MyCellClass();

    addChild(movieClip2);

    movieClip2.x = mouseX + 10;

    movieClip2.y = mouseY + 5;

    movieClip2.name = "cell2";

 

          var movieClip3:MyCellClass = new MyCellClass();

    addChild(movieClip3);

    movieClip3.x = mouseX;

    movieClip3.y = mouseY;

    movieClip3.name = "cell3";

 

          var movieClip4:MyCellClass = new MyCellClass();

    addChild(movieClip4);

    movieClip4.x = mouseX + 8;

    movieClip4.y = mouseY - 5;

    movieClip4.name = "cell4";

    }

}

 

 

               //end of Cell Control//

[/AS]

 
Replies
  • Currently Being Moderated
    Jul 11, 2012 4:20 AM   in reply to Lemon Video

    You should never nest named functions.  Get that keyDownHandler function out on its own.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2012 6:35 AM   in reply to Lemon Video

    Simply move it.  You don't need the function inside the function.  You only need to have the listener that calls the function in there...

     

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

     

    Leave that line above in the function but move the....

     

    function keyDownHandler(event : KeyboardEvent) : void

    {

       .... // everything

    }

     

    outside of it

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2012 7:30 AM   in reply to Lemon Video

    You're welcome. 

     

    Functions do not necessarily need to be called with listeners.  You can also call functions just by their names.... such as ...

     

    manageMouseOver();

     

    but in the case of that function, it requires an argument, so you EITHER need to provide an argument that agrees with the class of the event (MouseEvent - you'd have to have one available)  OR you define the function so that it can be called with or without an argument....

     

    function manageMouseOver(event:MouseEvent=null):void{

     

    Adding that "=null" makes the argument optional - it assigns the argument as being null, passing the requirement for an argument, and it is replaced by any actual argument that gets passed in

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points