Copy link to clipboard
Copied
I want to make a simple musical instrument simulation which will sounds when we press the keyboard that we used. On keyboard I used number 1 to 8 (do re mi fa sol la si do) . But the problem is when I have a long press the sounds will pilled, too noisy I think.
I hope it can sound dynamically as long as we press the button and stop when we release it. Iām newbie on flash and ActionScript 3.
Sorry for my bad english, here is my code and my capture. Anyone can help me? Many thanks before..
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Stage;
var suaraFileArray:Array =
["c5.mp3", "d5.mp3", "e5.mp3", "f5.mp3", "g5.mp3", "a5.mp3", "b5.mp3", "c6.mp3"];
var suaraArray:Array = new Array();
for(var i=0;i<suaraFileArray.length;i++){
var soundClip:Sound=new Sound();
var sndChannel:SoundChannel=new SoundChannel();
soundClip.load(new URLRequest(suaraFileArray));
suaraArray.push(soundClip);
}
var tombolKeyboard:Array = [Keyboard.NUMBER_1, Keyboard.NUMBER_2, Keyboard.NUMBER_3, Keyboard.NUMBER_4, Keyboard.NUMBER_5, Keyboard.NUMBER_6, Keyboard.NUMBER_7, Keyboard.NUMBER_8];
for(var i=0;i<tombolKeyboard.length;i++){
if(e.keyCode == tombolKeyboard){
suaraArray.play();
movieclipTut.play();
break;
}
}
});
var movieclipTut:Array = [tut1, tut2, tut3, tut4, tut5, tut6, tut7, tut8];
for(i=0;i<tombolKeyboard.length;i++){
(movieclipTut as MovieClip).nomor = i;
(movieclipTut as MovieClip).addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent){
var movieclip:MovieClip = e.currentTarget as MovieClip;
suaraArray[movieclip.nomor].play();
});
}
Copy link to clipboard
Copied
use a keyup listener to stop sounds. generally, you'll want to use a rapid sound fade-out and not a sudden stop.
Copy link to clipboard
Copied
Sorry for my late responses kglad...
Would you mind giving the correct script for me? I will send my file by email if you want.
Many thanks for your kindness.
Copy link to clipboard
Copied
i don't download and correct files unless hired. free help i only offer via the adobe forums.
Copy link to clipboard
Copied
Sorry for my impudence. I was new in the forum.
so where I have to put keyup listeners in the script ?
and for this code
if(e.keyCode== tombolKeyboard){
what conditions should I give into the script so that it can detect that the button has been release?
Thanks for reply.
Copy link to clipboard
Copied
it doesn't matter much but next to your keydown listener makes the most sense.
the biggest change for you is that you need each sound's soundchannel to control in the keyup listener function so you'll need to encode different soundchannels for each created sound.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now