A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] Play sound on keydown with timer

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    3

    resolved [RESOLVED] Play sound on keydown with timer

    Hey! :-)

    I would like to play a sound when a key is pressed, but then wait a second before playing the sound again. I've fiddled around with if (timer == 0), do the code, however anything I've tried hasn't worked.

    I'm using the ActionScript 2 shown below:
    Actionscript Code:
    var sounds:Array = ["laser1","laser2"];//the identifiers
    var n:Number=sounds.length;

    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() {

    if ((Key.isDown(16)))
        {
                            _root.mySound = new Sound();
                            _root.mySound.attachSound(sounds[random(_root.n)]);
                            _root.mySound.start();
        }

    else if ((Key.isDown(32)))
        {
                            _root.mySound = new Sound();
                            _root.mySound.attachSound(sounds[random(_root.n)]);
                            _root.mySound.start();
                           
        }
                           

     
         }
    ;
    Key.addListener(keyListener);

    I am both new to Flash and the Flashkit board, so if I haven't provided all the information you need, please tell me so!

    Many thanks in advance,
    Hellomynameis99

  2. #2
    Registered User
    Join Date
    May 2014
    Posts
    3
    Sorry for replying to my own thread, but how do you edit a thread? I have a bit more information that I'd like to add.

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    You can alose use a setTimeout feature like so,
    PHP Code:
    var sounds:Array = ["laser1""laser2"];//the identifiers
    var n:Number sounds.length;

    var 
    keyListener:Object = new Object();
    keyListener.onKeyDown = function()
    {
        if ((
    Key.isDown(16)))
        {
            
    _root.mySound = new Sound();
            
    _root.mySound.attachSound(sounds[random(_root.n)]);
            
    _root.mySound.start();
            
    setTimeout(pauseSound,2000);
            
    Key.removeListener(keyListener);
        }
        else if ((
    Key.isDown(32)))
        {
            
    _root.mySound = new Sound();
            
    _root.mySound.attachSound(sounds[random(_root.n)]);
            
    _root.mySound.start();
            
    setTimeout(pauseSound,2000);
            
    Key.removeListener(keyListener);
        }

    };
    Key.addListener(keyListener);

    function 
    pauseSound()
    {
        
    Key.addListener(keyListener);

    2000 = 2 seconds, 500 = half a second so so so on
    the code could probably be condensed too, I'm kind of busy so made it quickly.

    Good luck

  4. #4
    Registered User
    Join Date
    May 2014
    Posts
    3
    Thanks, worked a treat!

    The code is fine, I might condense it myself later. Thanks for taking the time to construct your reply.

Tags for this Thread

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