A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: actionscript to control the sounds ?

  1. #1
    Senior Member
    Join Date
    May 2005
    Posts
    238

    actionscript to control the sounds ?

    hi, I have a script to measure how much time has passed. If it's over 5 seconds, play sound1. If it's over 10 seconds, play sound2, and reset the timer. If the user clicks on button, play a sound and reset the timer again.

    the only thing is, when
    s1.attachSound("sound1"); plays, it's reapeting all the time until the 10th second and it works fine after that ,


    s1 = new Sound();
    s1.attachSound("sound1");
    s2 = new Sound();
    s2.attachSound("sound2");
    buttonSound = new Sound();
    buttonSound.attachSound("buttonSound");
    t1 = getTimer();

    this.onEnterFrame = function(){
    if (getTimer() - t1 > 5000){
    if (getTimer() - t1 > 10000){
    s2.start();
    t1 = getTimer();
    }
    else{
    s1.start();
    }
    }
    ========================
    can anyone help ? thanks in advance...

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    The problem lies in your onEnterFrame . Because onEnterFrame is continously running, s1.start() is constantly run when the time is greater than 5 secs. For e.g when the timer is 6 seconds the condition getTimer()-t1 > 5000 is STILL true and hance s1.start() is run again.

    Instead of using an onEnter i would use a setInterval as follows:

    Code:
    s1 = new Sound();
    s1.attachSound("sound1");
    s2 = new Sound();
    s2.attachSound("sound2");
    buttonSound = new Sound();
    buttonSound.attachSound("buttonSound");
    
    
    var nTimer = setInterval(timer, 1000);
    var secondsElapsed = 0;
    
    function timer() {
    	secondsElapsed++;
    	if (secondsElapsed == 5) {
    		s1.start();
    	}
    	if (secondsElapsed == 10) {
    		s2.start();
    	}
    }
    P.S u shud clear the interval when u no longer need it!!
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Senior Member
    Join Date
    May 2005
    Posts
    238
    hey, friend in SilentWeed , friend indeed :-),
    thank you so much, it's working perfect :-) , nice one dude ...

    what do you mean by "u shud clear the interval when u no longer need it!!"
    where would I need to do that , and how can I do it, incase I'd need to,

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    no problem mate..well basically the function timer is called every second. Now suppose the last sound that needs to be played is the sound that plays on the 10 second mark. Now after this you dont need the timer function to be called anymore, so instead of flash continously running the function every second even when not needed (hence wasting processor power) we should just clear the interval so the function is not run anymore.

    hence we can change the code to

    Code:
    function timer() {
    	secondsElapsed++;
    	if (secondsElapsed == 5) {
    		s1.start();
    	}
    	if (secondsElapsed == 10) {
    		s2.start();
    		clearInterval(nTimer); // this clears the interval so the function is not run anymore
    	}
    }
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  5. #5
    Senior Member
    Join Date
    May 2005
    Posts
    238
    that's magic dude, yep, I understand now and it's working good, in this case I probably won't be using it, but it was good to learn it

    I also have a button on stage
    If the user clicks on button, how can I play a sound and reset the timer again

  6. #6
    Senior Member
    Join Date
    May 2005
    Posts
    238
    dude, I've just noticed, the above code works nice, but it's not on a loop, it plays the sounds once and stops, I haven't used ClearInterval,
    any idea

  7. #7
    The Flash AS Newb
    Join Date
    May 2006
    Location
    Australia
    Posts
    179
    wich sound do you want to play more then once and how many times do you want to play it?

  8. #8
    Senior Member
    Join Date
    May 2005
    Posts
    238
    If it's over 5 seconds, play sound1. If it's over 10 seconds, play sound2, and reset the timer and starts again on a loop, and I have a button on stage, If the user clicks on button, play a sound and reset the timer again.

    I have this code, provided by silentweed (coolguy)

    s1 = new Sound();
    s1.attachSound("sound1");
    s2 = new Sound();
    s2.attachSound("sound2");
    buttonSound = new Sound();
    buttonSound.attachSound("buttonSound");


    var nTimer = setInterval(timer, 1000);
    var secondsElapsed = 0;

    function timer() {
    secondsElapsed++;
    if (secondsElapsed == 5) {
    s1.start();
    }
    if (secondsElapsed == 10) {
    s2.start();
    }
    }

    it's working good, but it's not on a loop,

    and I still haven't figured it out how to do it on the button itself , so it resets the timer on a loop again if you know what I mean,
    thanks dude

  9. #9
    Member
    Join Date
    Apr 2004
    Posts
    78
    You can make the sound loop as so:

    s1.start(0,1000);

    The first number is the secondsOffset, which gives the number of seconds into the sound before the sound actually starts playing. The second number is the amount of times you want the sound to loop.

    ...have fun!

    Len

  10. #10
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    whenever you want the timer to reset just call resetTimer();

    The following will make your sounds loop continuosly

    Code:
    s1 = new Sound();
    s1.attachSound("sound1");
    s2 = new Sound();
    s2.attachSound("sound2");
    buttonSound = new Sound();
    buttonSound.attachSound("buttonSound");
    
    
    var nTimer = setInterval(timer, 1000);
    var secondsElapsed = 0;
    function timer() {
    	secondsElapsed++;
    	if (secondsElapsed == 5) {
    		s1.start();
    	}
    	if (secondsElapsed == 10) {
    		s2.start();
    		
    		resetTimer();
    		
    	}
    }
    
    
    function resetTimer(){
    	secondsElapsed = 0;
    }
    
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  11. #11
    Senior Member
    Join Date
    May 2005
    Posts
    238
    thanks again, silentweed, its cool and working very well now,
    the only thing is,
    I still need to figure out, when a user clicks on the button (on stage)
    it plays the sounds and resets the timer
    I did this but it doenst work ;

    on (release){
    buttonSound.start();
    resetTimer(){

    }

  12. #12
    Senior Member
    Join Date
    May 2005
    Posts
    238
    I think I have worked it it now, I did this, and it seems to work fine now
    ///on the button itself;

    on (release){
    buttonSound.start();

    resetTimer();

    }

    Thanks a million silentweed for all your help, and other trying to help too...
    nice one dude

  13. #13
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    np mate yw!!
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

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