A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: How to I make it play a sound on button press?

  1. #1
    Junior Member
    Join Date
    Jan 2007
    Posts
    4

    How to I make it play a sound on button press?

    Hi, basically I want it so that when I press the ctrl button it will play a short sound, how do I do this?

    I have the sound file (laserSound)

    (Basically in context I am trying to follow a tutorial to make a game and ctrl makes the laser gun fire, and I would like to attach a sound to this action)

  2. #2
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    //function that checks for key presses
    function myOnKeyDown() {
    //if the pressed key is control
    if (Key.isDown(Key.CONTROL)) {
    //make a new sound object, attach the laser sound to it and play the sound
    var my_sound:Sound = new Sound();
    my_sound.attachSound("laserSound");
    my_sound.start();
    }
    }
    //listener object for checking key presses
    var myListener:Object = new Object();
    myListener.onKeyDown = myOnKeyDown;
    Key.addListener(myListener);

    hope this helps
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  3. #3
    Junior Member
    Join Date
    Jan 2007
    Posts
    4
    thanks buddy! I'll try that ASAP, really appreciate it

  4. #4
    Junior Member
    Join Date
    Jan 2007
    Posts
    4
    Hi, I have tried your method but unfortunately it does not seem to work for me, perhaps I am not fitting it into the code correctly? I was wondering if you could be so kind as to show me how it would fit into my existing code which I have posted below (this code goes within the space ship). At present the sound file is in my library and has the name as stated above!


    onClipEvent(load){

    moveSpeed=10;
    moveSpeed2=20;
    _root.laser._visible=false;
    laserCounter=1;

    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.CONTROL)) {
    laserCounter++;
    _root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
    _root["laser"+laserCounter]._visible=true;

    }

    if (Key.isDown(Key.RIGHT)) {
    this._x+=moveSpeed2;
    } else if (Key.isDown(Key.LEFT)) {
    this._x-=moveSpeed2;
    }


    if (Key.isDown(Key.DOWN)) {
    this._y+=moveSpeed;
    } else if (Key.isDown(Key.UP)) {
    this._y-=moveSpeed;
    }
    }

  5. #5
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    Could try:
    onClipEvent(load){

    moveSpeed=10;
    moveSpeed2=20;
    _root.laser._visible=false;
    laserCounter=1;
    //----------------<new code>--------------//
    my_sound = new Sound();
    my_sound.attachSound("laserSound");
    //----------------<end : new code>--------------//
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.CONTROL)) {
    laserCounter++;
    _root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
    _root["laser"+laserCounter]._visible=true;
    //----------------<new code>--------------//
    my_sound.start();
    //----------------<end : new code>--------------//
    }
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  6. #6
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    bTW the sound in the library must have the name "laserSound" set as the identifier in the Linkage - with "export as Actionscript" selected!
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  7. #7
    Junior Member
    Join Date
    Jan 2007
    Posts
    4
    Quote Originally Posted by Boris the Frog
    bTW the sound in the library must have the name "laserSound" set as the identifier in the Linkage - with "export as Actionscript" selected!
    works like a dream , I hadn't done this bit, and the extra code you posted was very helpful too, thankyou!!

  8. #8
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    cool - my work here is done
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

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