flash actionscript:
How do I script a button to play one sound, whilst fading out the current sound playing? I have 6 buttons to play 6 audio clips, but I want the current sound playing (if there is one) to fade out when the user clicks to hear another clip. I've tried messing around with controllers that do sound.attachSound and then fade out using sound.setVolume, but whatever I try, I cannot do it for 6 different sounds, bearing in mind that any sound could be playing at any one time and so I need to script something that will fade out any sound that's playing and then playing the sound clip corresponding to the button pressed! Please help!
for (var i = 1; i<=6; i++) { _root.createEmptyMovieClip("mc"+i, i); /* you can have, at most, one Sound() per timeline. so, i used one movieclips for each Sound(). we could get away with 2 movieclips, because you probably won't have any more than 2 playing at any one time, but i don't really know that so i made 6. */ _root["s"+i] = new Sound(_root["mc"+i]); // sounds are instantiated: _root.s1,s2,s3 etc _root["s"+i].attachSound("sound"+i); // id's are used to attach sounds _root["btn"+i].ivar = i; // the ith button must remember that it's the ith button _root["btn"+i].onPress = function() { clearInterval(fadeI); // any previous fade is cleared. fadeI = setInterval(fadeF, 50, this.ivar); /* fade is started. this.ivar is passed to fadeF() so it knows the ith button was pressed and that after fade-out is complete it should start() the ith sound ( _root["s"+i] ). */ }; } function fadeF(i) { if (prevSound == undefined || prevSound.getVolume()<=0) { /* if their is no prevSound (ie, when the first button is pressed) or the prevSound has volume at 0 or less, we can start _root["s"+i] */ clearInterval(fadeI); prevSound.stop(); /* prevSound has volume zero, but it's still playing until this line */ prevSound.setVolume(100); /* prevSound has its volume reset to 100 so the next time its button is pressed it starts play with full volume. we're now finished with prevSound and ready to define the "next" prevSound in the line below. */ prevSound = _root["s"+i]; _root["s"+i].start(); /* and finally (in this branch of the conditional) the sound corresponding to button i is started. */ } else { /* prevSound's current volume is prevSound.getVolume(). the line below decreases this by 5 and sets that as the new volume. we repeat that 20 times and prevSound's volume will be zero and we're ready to jump into the conditional branch above */ prevSound.setVolume(prevSound.getVolume()-5); } }
Thanks, kglad, I shall be looking over that explanation in more detail later - I'm still not 100% with the understanding of the script, but I'm grasping bits of it as I go along. I assume I'll be attaching that script to each button??? Forgive me, I'm just new to this stuff. That's why I need to ask - seeing as though you seem to be really good at this Flash thing, what does it take to get where you are? I mean, what the best method for me to learn by? My husband reckons its studying books like 'Actionscript for Flash MX 2004' by Sham Bhangal or the likes, but I more go for starting a project and learning as I go along - i.e. lots and lots of practice and fun! What do you advise?
no, that code should be attached to a frame. it contains the coding for the onPress handlers of all six buttons. your movie just needs to have those button names and linkage id's. or you need to change the code so the button names used in the code match your button instance name and the linkage id's used in the code match your linkage id's.
correct, no even onPress handlers. they are already in the code. if you want to add more to the onPress handlers, or add onRelease, onRollOver etc handlers that can be done in the same for-loop that's used to define the onPress handler.
No, you don't have to attach code to your buttons. You can put the code on the frame (name it actions). You may declare inline function for you button where myButton is instance name of a movie clip. Paste the code in to your first frame on actions layer. myButton.onPress = function():Void{//mybutton is movie clip,onPress(could be ..onRelease, :Void when your function doesn't need to return anything gotoAndStop(10);//go to and stop to frame 10 }; good luck
if you have buttons with instance names _root.btn1, _root.btn2 and you want them to plays sounds with linkage id sound1, sound2,.., resp, you can use: for (var i = 1; i<=6; i++) { _root.createEmptyMovieClip("mc"+i, i); _root["s"+i] = new Sound(_root["mc"+i]); _root["s"+i].attachSound("sound"+i); _root["btn"+i].ivar = i; _root["btn"+i].onPress = function() { clearInterval(fadeI); fadeI = setInterval(fadeF, 50, this.ivar); }; } function fadeF(i) { if (prevSound == undefined || prevSound.getVolume()<=0) { clearInterval(fadeI); prevSound.stop(); prevSound.setVolume(100); prevSound = _root["s"+i]; _root["s"+i].start(); } else { prevSound.setVolume(prevSound.getVolume()-5); } }
aaah, kglad, sooooo glad you came to my rescue, but errrrm... can you just explain that to me please - coz I have nooooo IDEA what that is about??? I've made an attempt to understand some of it, but if you could just help me grasp the meaning of that script I would be so grateful. Thanks again!
Thanks alot, guys, especially kglad - even though he's not freeing up any tips on becoming a great flashie like himself! Never mind, I'll just get my own back on some other newb when I'm there... so watch this space!!!
hasty, hasty me. Ermm, its actually not working! When I click the button no sound plays? I've taken off all my naff code from the buttons and I pasted the code you mentioned into the first frame of my actions layer (which also has a stop by the way - hope that 's ok). But it aint working... any ideas???
its ok. I worked it out. I was supposed to name my button instance names: btn1, btn2, etc., not: _root.btn1, _root.btn2, etc. as I thought you said before. Silly me! Never mind. Thanks so much anyway - you guys are life-savers!
yes, prevSound always contains the code for the currently playing sound, if there is one. you could define a new function to fade the current sound and not use any code to start a new sound or you could just use the current function and not pass a parameter i (or pass a bogus parameter): flash will still fade the current sound and will try and initiate the bogus sound but won't balk when it finds no such sound object. for example: stopSoundBtn.onPress=fadeF
nice one. ok, so (I know I should use my own brain more, but...) does that mean I call my button instance 'stopSoundBtn'? And do I just put that script you mentioned onto the end of my other script (that you done for me)? Sorry about all this...
kglad, bringing you back to this script you wrote for me a few months ago, I've been trying to explain it to my husband (who is a newer newb than me!) and he'd like to know exactly why on the fifth line of your code: _root["btn"+i].ivar = i; has to be written. You said its because the ith button must remember its the ith button. He wants to know, is this done because the button might not remember the value of i or something?
that's correct. i changes with each iteration of your for-loop. by the time any of your ith movieclip buttons are pressed your for-loop has completed execution and i is the last value of that for-loop. if you ask any of your buttons about i, it will return the last value of the for-loop and NOT the value of i when that button was created. if you ask any button whether it's the 1st, 2nd,..., 6th button created, it will have no way to determine that unless you decompose its name using string methods (which is doable but more difficult) or just define a variable that each movieclip button can always access that will inform the button that it's the ith button. each button has a variable (ivar) defined on its timeline that it can always retrieve.
no, the button instances were already created and placed on-stage and given instance names of btn1, btn2, ..., btn6. if you read my original response, the first sentence makes clear this assumption. the for-loop defines the mouse handlers for those buttons in the following lines of code: _root["btn"+i].onPress = function() { clearInterval(fadeI); fadeI = setInterval(fadeF, 50, this.ivar); }; you want to have some way to retrieve that "i". we could use string methods like: _root["btn"+i].onPress = function() { i=this._name.substr(-1); // this would fail with more than 9 buttons }; so, btni can determine that it's the ith button, but it's easier to define ivar.
ok, so that means I completely don't understand how for-loops work then. I thought that the for-loop runs each time a button is pressed, and depending on which button was actually pressed, the for-loop will assign the value of the button to the i variable. Do I understand correctly then that what is actually happening is the for-loop runs when the button is pressed and the last value (in this case, 6) is assigned to the i variable. Then that value is given to the button by the ivar variable... So how then do buttons that need a value of 1-5 get that value assigned to them via the ivar variable if the ivar variable is always 6?
no for loop process is not related to onPress handler .When you do onPress two things happen you have clear interval ie if you have some previous fade that stops .and you have setInterval which calls ur function fadeF at those given interval of time which leads to new fading of next sound.The for loop create i no of movieclips and btns which are referenced each with_root[ "btn"+i].ivar = i ex) btn1 is 1st btn2 is 2nd and so on.
ok. So that means that in each iteration of the for-loop, 6 movie clips are created (this I understand) and 6 sound containers are placed in those movie clips with 6 sounds attached (this I also understand), and then 6 buttons are created, each given a value of 1-6 depending on which creation it is (i.e. 1st, 2nd, etc.). This I do not understand. When was flash told to created 6 buttons? What command initiated this? I thought that _root["btn"+i].ivar=i, just referred to the 6 buttons I have already created and placed on the stage, and was assigning values to each of the ivar variables on those buttons' timelines. But in that case, what would give i a value in the first place? It can only be that values are generated by those dynamically created buttons you speak of, but then again, how comes those buttons get created in the first place? Please explain this to me - I am so baffled!
Don't see what you're looking for? Try a search.
|