Hi, making a drum simulator in Flash MX, how the hell do I attach a sound to a key press. You would think it would be as simple as, " On KeyPress(whatverekey) play sound", but I cant seem to figure out. An help will be appreciated. (By the way, I want to attach the sound “highhat_closed” to keypress <Page Up>, <Home>, and <Insert>)
Oh yea, and could you explain it? Because I read an earlier post with similar question, but have no clue how to apply it to my problem. Thx.
[font=Times New Roman]Well I’m not that far up the greasy pole of Flash knowledge myself, but what I came up with (Puffs up chest!!) was:[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]Import your sound & delete all instances of it on stage so it’s just in the library, give it a linkage identifier. [/font]
[font=Times New Roman] [/font]
[font=Times New Roman]Make an empty movie clip & put the following actions on it, substituting the keys you want to use in the “if” statement.[/font]
[font=Times New Roman] [/font]
[font=Times New Roman][color=blue]onClipEvent (keyDown) {[/color][/font]
[font=Times New Roman][color=blue] if(Key.isDown(Key.PGDN)||Key.isDown(Key.PGUP)){[/color][/font]
[font=Times New Roman][color=blue] my_sound=new Sound();[/color][/font]
[font=Times New Roman][color=blue] my_sound.attachSound(“splat”);[/color][/font]
[font=Times New Roman][color=blue] my_sound.start();[/color][/font]
[font=Times New Roman][color=blue]}[/color][/font]
[font=Times New Roman][color=blue]}[/color][/font]
[font=Times New Roman] [/font]
[font=Times New Roman]I’m sure that there is a more eloquent way of doing this, but I hope this helps [/font][font=Wingdings][font=Wingdings]J[/font][/font]
Strange (Chest deflates) this works in the “Test Movie” enviroment, but not when I publish it - Unless I click anywhere in the movie first?
Pup 100,
You’re on the right track. You must click in the page to give the page focus. I could be wrong, but I don’t think there’s any way around that. A “click to start” button would solve this.
A couple of things:
-
For efficiency and organization, I would not define the sound object within the conditional code. I would define all sound objects either in the onClipEvent(load) or just in a single frame on the root time line.
-
One challenge of your code is that the sound will continue to activate as long as the keypress is held down. If the presenter only wants it to activate once, you need to provide code that will not allow it to activate again until the key is released and pressed again. For example:
onClipEvent(load) {
//Sound Objects
kick1=new Sound(this);
kick1.attachSound("kick01");
kick2=new Sound(this);
kick2.attachSound("kick02");
kick3=new Sound(this);
kick3.attachSound("kick03");
//
//Create a listener that detects when a key is released
myKeyUp = new Object();
myKeyUp.onKeyUp = function() {
releaseVar=0;
}
Key.addListener(myKeyUp);
}//END OF Clip Event Load
//
onClipEvent(enterFrame) {
//You could shorted the coding with the following.
_root.myKeyCode=Key.getCode();
keyPressed=Key.getCode();
//
//
if(Key.isDown(65) && releaseVar!=1){ // key "f"
releaseVar=1;
_root.kick1.start();
}
//
if(Key.isDown(83) && releaseVar!=1){ // key "s"
releaseVar=1;
_root.kick2.start();
}
//
if(Key.isDown(68) && releaseVar!=1){ // key "d"
releaseVar=1;
_root.kick3.start();
}
}//END
There are several ways to code this, but I typed it this way so you can see it in one glance.
I tried to edit my post and somehow posted it again. Look at the second one.
Thanks for tidying that up for me kennyb - very helpful & informative.
Sorry to sort of hijack your thread iornzionmarley - but hope this all helps
thanks alot, it helps a ton… thank you both
for some reason, the one pup100 supplied didnt work…
I’d stick with kennyb’s version iron
Mine seems ok on my system, but you do have to click the movie first.