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]
OK, so I've changed it to this which nearly works apart from the mouseout.
Which now gives me an undefined property: keyDownHandler?
Any help?
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
tetrad.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver, false, 0, true);
tetrad.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut, false, 0, true);
function manageMouseOver(event:MouseEvent):void{
stage.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";
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
removeChild(tetrad)
}
}
}
function manageMouseOut(event:MouseEvent):void{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
}
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
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
North America
Europe, Middle East and Africa
Asia Pacific