View Full Version : stop and play buttons
apgguy
10-17-2008, 11:26 AM
Hi,
Ive got a stop and a play button both working. But what I really want is just the stop button displayed and when you click the button it changes to the play one.
How would this be done, please help.
I would make them a clickable movieclip, and then create a variable called something like "playToggle" and make it a boolean. When it is true, the button (and whatever it is controlling) is in "play mode," and displays the play triangle. When it is false, the button (and whatever it is controlling) is in "pause/stop mode" and displays the square or double-line, as appropriate.
There are about a zillion ways to do it, but this is the general idea I would probably adopt.
apgguy
10-17-2008, 12:31 PM
Right ok, well im new to actionscript so not sure how to do what u say,
ive got 2 buttons play_btn and stop_btn how would I form the code for this?
crispyDuck
10-17-2008, 12:48 PM
Try this
GamezR2EZ
10-17-2008, 06:26 PM
i didnt have time to view crispyDucks code
but all you need to do is this
assuming play button has an instance name of play_btn and stop, stop_btn, of course you can change those
before anything gets underway, line up your buttons so they are directly over each other then and this code
play_btn.visible = false;
stop_btn.visible =true;
in the function for play button have the above code in the funtion
in the stop function have
play_btn.visible = true;
stop_btn.visible = false;
your code would be as follows...
play_btn.addEventListener(MouseEvent.CLICK, playButton);
stop_btn.addEventListener(MouseEvent.CLICK, stopButton);
play_btn.visible = true;
stop_btn.visible = false;
//Play Button
function playButton(e:Event):void{
/*playbutton does something....*/
play_btn.visible = false;
stop_btn.visible = true;
}
//Stop Button
function stopButton(e:Event):void{
/*stopbutton does something....*/
play_btn.visible = true;
stop_btn.visible = false;
}
adding the "visible" statement in both functions and the beginning is vital, but other than that you can change anything
Not certain, gamez, but I think your code would always fire both play button and stop button, in that order, every time. You need to toggle the "playToggle" or "isPlaying" variable to determine which code to fire.
In addition, you can do both buttons as a single button, based on the toggle state. Usually, this type of variable is called a flag, and is used to determine the course of action when one of two states is true (or false). The example I use is two students sharing an apartment. They leave at the same time, and turn off the porch light.
If the porch light is on, then the returning student cannot enter the apartment (denotes "I'm here with my date"). If the light is off, the student can enter, and has the option of turning on the porch light.
So, "playToggle" or "isPlaying" or "playFlag" is a boolean. When it is true, you fire your stop code, and make it false. When it is false, you fire your play code, and make it true. This way, you need only one button, with two graphics, and only one function call or event dispatcher.
Like I said, there are approximately one zillion ways of doing this.
crispyDuck
10-19-2008, 01:34 PM
Hi, My apologies, I uploaded the AS2 version. Here it is in AS3 & a sample of the code. For this I made my button out of a movieclip that had four frames labeled [on][onOver][off][offOver] & gave it an instance name of toggleButton.
toggleBtn.buttonState = "on"; //Initial stae of button
toggleBtn.gotoAndStop(toggleBtn.buttonState);
toggleBtn.addEventListener(MouseEvent.ROLL_OVER,ro llOverToggle);
toggleBtn.addEventListener(MouseEvent.ROLL_OUT,rol lOutToggle);
toggleBtn.addEventListener(MouseEvent.CLICK, toggleClick);
function rollOverToggle(e:MouseEvent) {
toggleBtn.gotoAndStop(toggleBtn.buttonState+"Over");
}
function rollOutToggle(e:MouseEvent) {
toggleBtn.gotoAndStop(toggleBtn.buttonState);
}
function toggleClick(e:MouseEvent) {
if (toggleBtn.buttonState == "on") {
toggleBtn.buttonState = "off";
trace("OFF STATE - CLICK TO PLAY")
}
else {
toggleBtn.buttonState = "on";
trace("ON STATE - CLICK TO STOP")
}
toggleBtn.gotoAndStop(toggleBtn.buttonState+"Over");
}
papillon68
10-19-2008, 09:15 PM
Easy.
1) make a movie clip with two frames: frame 1 has the play button and frame 2 has the stop button.
3) when you click on play (frame1), move the timeline to the second frame (use gotoAndStop(2).
4) when you click on stop (frame2), move the timeline to the first frame (use gotoAndStop(1).
Does this make sense ?
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.