Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Problem of simple musical instrument simulation

New Here ,
Jul 14, 2016 Jul 14, 2016

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];

  1. stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent){

    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();

    });

}

Untitled.png

TOPICS
ActionScript
272
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 14, 2016 Jul 14, 2016

use a keyup listener to stop sounds.  generally, you'll want to use a rapid sound fade-out and not a sudden stop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2016 Jul 16, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2016 Jul 17, 2016

i don't download and correct files unless hired.  free help i only offer via the adobe forums.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 18, 2016 Jul 18, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 18, 2016 Jul 18, 2016
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines