-
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...
-
Flashmatics
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!!
-
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,
-
Flashmatics
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
}
}
-
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
-
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
-
wich sound do you want to play more then once and how many times do you want to play it?
-
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
-
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
-
Flashmatics
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;
}
-
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(){
}
-
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
-
Flashmatics
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|