-
1. Re: How to add Mouse and Keyboard Event in same function?
moccamaximum Aug 26, 2013 3:50 AM (in response to vari25)you can basically set up one function to handle all differnent kind of events.
so lets say you have setup an eventlistener like this:
sequence.addEventListener(MouseEvent.CLICK, handleEvent);
you can simply setup a keyboardListener to the stage that has the same name.
stage.addEventlistener(KeyboardEvent.KEY_UP, handleEvent);
and then in the handleEvent (which has been generalized to the class which both event types inherit from) function you write
function handleEvent(e:Event):void{
switch(e.type){
case "click": trace("Stage was clicked");
//write your Mouse Logic
break;
case "keyUp": trace("Keyboard was used");
//write your keyBoard Logic
break;
}
}
-
2. Re: How to add Mouse and Keyboard Event in same function?
vari25 Aug 26, 2013 5:34 AM (in response to moccamaximum)Thank you for your help.
But I am getting this error.
Scene 1, Layer 'Layer 2', Frame 1, Line 50 1120: Access of undefined property e.
Please help.
-
3. Re: How to add Mouse and Keyboard Event in same function?
moccamaximum Aug 26, 2013 5:38 AM (in response to vari25)what does line 50 say?
-
4. Re: How to add Mouse and Keyboard Event in same function?
vari25 Aug 26, 2013 5:57 AM (in response to vari25)Sorry. My mistake! forgot to post the code.
var key:uint = e.keyCode;
The code is:
var key:uint = e.keyCode;
images.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed)
function keyPressed(e:Keyboard):void
{
if(key==37){
images_mc.gotoAndStop(images_mc.currentFrame - 1)
}else if(key==39){
images_mc.gotoAndStop(images_mc.currentFrame + 1)
}
} -
5. Re: How to add Mouse and Keyboard Event in same function?
moccamaximum Aug 26, 2013 6:08 AM (in response to vari25)the scope of e is only inside the function,
so when you write
var key:uint = e.keyCode;
Flash doesn`t know of any object called e
you have to rewrite like so:
var key:uint;
and then inside the keyPressed function you write:
...
key = e.keyCode
if(key==37){...
Beware of using any keyWords, when in doubt if "key" is a name flash has reserved internally always use a leading underscore (_key)
-
6. Re: How to add Mouse and Keyboard Event in same function?
vari25 Aug 26, 2013 10:25 PM (in response to moccamaximum)Scene 1, Layer 'Layer 2', Frame 1, Line 56 1119: Access of possibly undefined property keyCode through a reference with static type flash.ui:Keyboard.
key = e.keyCode (=Line 56)
if(key==37){
-
7. Re: How to add Mouse and Keyboard Event in same function?
moccamaximum Aug 27, 2013 12:34 AM (in response to vari25)rewrite
function keyPressed(e:Keyboard)
to
function keyPressed(e:KeyboardEvent)

