A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Sound on mouseclick or keypress

  1. #1
    Junior Member
    Join Date
    Nov 2008

    Sound on mouseclick or keypress


    I have recently made a piano in CS3 using AS2. The basic idea is that you can both click on a piano key and click on a key on your keyboard to hear the sound that particular key makes.
    Everything works just dandy apart from the fact that the sound doesn't seem to work at all.
    Below I have posted the action script (the comments are in Dutch), not sure if that helps, perhaps you'll need the FLA file as well.
    If anyone can help me out with getting the sound to work that would be great!

    //Maakt een sound object en verbind het met de piano movie clip op de stage
    var mySound:Sound = new Sound(piano);
    //Een array die de letters en de nummers die getypt worden met de bijbehorende toetsencodes linkt
    var codeToKey:Array = new Array();
    //Zwarte toetsen die gespeeld kunnen worden door de aangegeven cijfers op het toetsenbord
    codeToKey[50] = 61; //2
    codeToKey[51] = 63; //3
    codeToKey[53] = 66; //5
    codeToKey[54] = 68; //6
    codeToKey[55] = 70; //7
    //Witte toetsen die gespeeld kunnen worden door de aangegeven letters op het toetsenbord
    codeToKey[81] = 60; //q
    codeToKey[87] = 62; //w
    codeToKey[69] = 64; //e
    codeToKey[82] = 65; //r
    codeToKey[84] = 67; //t
    codeToKey[89] = 69; //y
    codeToKey[85] = 71; //u
    codeToKey[73] = 72; //i
    //Een function die een muzieknoot doormiddel van een nummer kan afspelen
    function playNote(num:Number){
    	piano["key"+num]._width = widths[num]-1;
    	piano["key"+num]._height = heights[num]-1;
    	mySound.attachSound(num + "short.mp3");
    var heights:Array = new Array();
    var widths:Array = new Array();
    //Een function waarmee de toetsen ook met een muisklik gespeeld kunnen worden gespeeld
    //Het slaat ook de oorspronkelijke afmetingen van de toetsen op van de bovengenoemde arrays
    function setupPiano() {
    	for (i=60; i<=72; i++) {
    		piano["key"+i].onRelease = function() {
    			var num:Number = this._name.substring(3, this._name.length)
    		heights[i] = piano["key"+i]._height;
    		widths[i] = piano["key"+i]._width;
    //Een function die de oorspronkelijke afmetingen van de toetsen reset
    function resetSize(num:Number){
    		piano["key"+num]._height = heights[num];
    		piano["key"+num]._width = widths[num];
    //Een key listener waarmee je doormiddel van het toetsenbord ook kan spelen
    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {
    	var theCode:Number = Key.getCode();
    //Hiermee wordt aangegeven dat de piano ook met de muis bespeeld kan worden

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Here, no there
    I would first change this line, removing piano.
    PHP Code:
    var mySound:Sound = new Sound(); 
    Second, I'm assuming your mp3 files are labeled "60short.mp3" to "71short.mp3" and they are located in the same directory as the SWF file. Makes it a little hard since I don't read Dutch. Can you add a trace command in the playNote function just before it attaches the sounds. What happens when you press a key?
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center