all groups > flash actionscript > september 2007 >
You're in the

flash actionscript

group:

Key Press Anyone? SImple Q?



Key Press Anyone? SImple Q? Fluffy Puppies
9/14/2007 9:11:49 PM
flash actionscript: Hi, and thanks in Advance!!!

So i have a button that needs to be activated from the key board.

Does anyone have a completed script that will allow the user to activate this
button that has sound attached to it from hitting the letter "A" ?

Also, the sound file that i want to have play on the button is on the "Down
State" of pressing. I kinda of need the sound to play in both instances. When
the letter on the keyboard is clicked and when you actually use your mouse.


Thanks A Million! I

Adam:beer;
Re: Key Press Anyone? SImple Q? Ardy15jan
9/15/2007 12:00:00 AM
I hope I helped. Also, if you want a suggestion on how to play your sound both
in the button and when you press "A", try making a MovieClip on the stage that
contains the sound, place it in the 'Over' state of your button and load it
dynamicly when user presses "A".
Best of luck!
Re: Key Press Anyone? SImple Q? Ardy15jan
9/15/2007 12:00:00 AM
Try listening for keyboard events.
ActionScript 2.0:


var oListener:Object = new Object();
oListener.onKeyDown = function():void {
// 65 is the Key code value for letter 'A'
if(Key.getKeyCode() == 65) {
// Here`s where you activate your button... I don`t understand what
you meant by that, but here`s where you do it
}
}
Key.addListener(oListener);
Re: Key Press Anyone? SImple Q? Ardy15jan
9/15/2007 12:00:00 AM
.... and ActionScript 3.0:



addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

function onKeyDown(event:KeyboardEvent):void {
if(event.keyCode == 65) {
// Activate your button
}
}
Re: Key Press Anyone? SImple Q? clbeech
9/15/2007 2:07:33 PM
You can't really call to a button instance to 'activate' it, you could do so if
your 'button' is an MC. I would recommend using a Sound Object on the timeline
for your event sound, then you can control it from either call, the keyboard or
the button. Ardy15jan, has the correct calls for the keyboard above, just add
aditional code for your button instance to activate the sound.

To construct a Sound Object, you will need to import the sound to the library,
and add a 'linkage identifier', like 'sound_id' then attach the sound in script
on the first frame of the main timeline like so:

var mySound:Sound = new Sound();
mySound.attachSound('sound_id');

...Then, construct an on handler for the button instance and call the sound:

myButton.onPress = function() {
mySound.start();
}

...and in the code for the keyboard as above where Ardy15jan has 'Activate you
button' place the start() method:

mySound.start();

...there you go :)

AddThis Social Bookmark Button