To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-21-2004, 03:11 PM   #1
exuk
Flash or die
 
Join Date: May 2004
Location: San Francisco, California
Posts: 127
2-State Button - Need Help

I want to have one button that has two states. For example a play button, when pressed it plays a movie, but change to a pause button. Then you can click the pause button and it returns to the original state.

So far, i have created a moiveclip, inside i have my button. On frame 1 i have the play button and on frame 5 i have my pause button. I want to have a action on release, so that the button change from one frame to the next. Does this make sense?

Here is my code to call this action:
Code:
this.movieClip.play_btn.onRelease = function() {
	gotoAndStop(5)
};
I have posted a bunch of theads this week and no one replies, i hope i can get some help. Thanks.
__________________
// rocking flash cs4,as3
exuk is offline   Reply With Quote
Old 10-21-2004, 03:26 PM   #2
parkjin
Senior Member
 
Join Date: Aug 2000
Location: Vermont
Posts: 269
i imagine that your code of:

this.movieClip.play_btn.onRelease = function() {
gotoAndStop(5)
};

is nested inside some other object creating function - if so you may want to post the entire function.

if not try this:

movieClip.play_btn.onRelease = function() {
movieClip.gotoAndStop(5);
};

i have a feeling it is a targeting issue.. post your fla if want
parkjin is offline   Reply With Quote
Old 10-21-2004, 06:07 PM   #3
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
This adds a toggle behavior to the button.
The variable isPaused flips back and forth between true and false when this line executes:

isPaused = ! isPaused;

Then you use that variable to change the behavior or look of the button.


code:


isPaused = false;

this.movieClip.play_btn.onRelease =

function()
{
isPaused = ! isPaused;
if (isPaused) gotoAndStop(1);
else gotoAndStop(5);
};


__________________
jbum is offline   Reply With Quote
Old 10-21-2004, 07:33 PM   #4
parkjin
Senior Member
 
Join Date: Aug 2000
Location: Vermont
Posts: 269
hey jbum, very interesting, never seen this usage of toggling -> isPaused = ! isPaused; -> learn something new everyday

sorry exuk, i think i need to read people's posts more thoroughly.. long day for me.
parkjin is offline   Reply With Quote
Old 10-21-2004, 10:04 PM   #5
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
This one'll blow your mind. You can do the same thing in one line.

code:

isPaused = false;

this.movieClip.play_btn.onRelease =

function()
{
gotoAndStop(5 - (isPaused ^= 1)*4);
}



I don't recommend this method, however, since it makes the code hard to understand.

Here's what is going on - I'll build it up bit by bit.

isPaused ^= 1;

This toggles the 1 bit in isPaused by using the XOR operator. The result is the new value of isPaused, which will be 0 or 1. So we get 0,1,0,1,0,1 etc.


(isPaused ^= 1)*4

We multiply that by 4, which gives us 0,4,0,4,0,4, etc.

5 - (isPaused ^= 1)*4

5-0 = 5
5-4 = 1

so this toggles back and forth between frame 5 and frame 1.

Now, here's a 2-line version which I would use. It uses the conditional operator ? :

code:

isPaused = !isPaused;
gotoAndStop(isPaused? 1 : 5);

__________________

Last edited by jbum; 10-22-2004 at 01:51 AM.
jbum is offline   Reply With Quote
Old 10-21-2004, 10:09 PM   #6
gparis
Super Moderator
 
Join Date: Aug 2000
Location: Montréal
Posts: 13,430
It blew mine. You're an artist, jbum!

gparis
__________________


AS 2.0 Forum Guidelines || Use P H P tags for code samples || Flash LiveDocs || AS 2.0 Language reference (CS4)
gparis is offline   Reply With Quote
Old 10-22-2004, 12:42 AM   #7
parkjin
Senior Member
 
Join Date: Aug 2000
Location: Vermont
Posts: 269
i feel like a newb again.

haven't had time to play around with your AS yet.. but out of curiosity what minimum version of flash does the movie need to be published as?

thanks.
parkjin is offline   Reply With Quote
Old 10-22-2004, 01:52 AM   #8
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Most of the scripts I post here are Flash 6 (Flash MX), and work with actionscript 1.0. Many of them also work in Flash 5, and this particular one probably does.

I don't do much coding in actionscript 2.0 yet.
__________________
jbum is offline   Reply With Quote
Old 10-22-2004, 11:36 PM   #9
exuk
Flash or die
 
Join Date: May 2004
Location: San Francisco, California
Posts: 127
Thanks for all the help from both of you. I tried the code without yeilding results. I am going to post the fla file. Can someone help me with this?
Attached Files
File Type: fla twostatebutton.fla (30.5 KB, 196 views)
__________________
// rocking flash cs4,as3
exuk is offline   Reply With Quote
Old 10-23-2004, 02:28 PM   #10
parkjin
Senior Member
 
Join Date: Aug 2000
Location: Vermont
Posts: 269
this works

this seemed to do the trick, added some trace actions so you can see what is going on:

Code:
isPaused = true;
this.movieClip.play_btn.onRelease = function() {
	isPaused = !isPaused;
	trace ("Value of isPaused: " + isPaused);
	_root.movieClip.gotoAndStop(isPaused? 1 : 5);
	trace ("MovieClips Current Frame: " + _root.movieClip._currentFrame);
}
this line of code allows it to work:

Code:
_root.movieClip.gotoAndStop(isPaused? 1 : 5);
but this didn't:

Code:
this.movieClip.gotoAndStop(isPaused? 1 : 5);
the result was an undefiined object. you would have to write a new constructor function in order for this.movieClip.gotoAndStop(isPaused? 1 : 5) line of code to be applicable.
parkjin is offline   Reply With Quote
Old 10-23-2004, 02:46 PM   #11
exuk
Flash or die
 
Join Date: May 2004
Location: San Francisco, California
Posts: 127
OK Thanks!. Sometimes i mess up on the paths, but the _root was the fix.

For all reading this thread i am going to post the working fla.
Attached Files
File Type: fla twostatebutton.fla (45.0 KB, 385 views)
__________________
// rocking flash cs4,as3
exuk is offline   Reply With Quote
Old 10-23-2004, 03:06 PM   #12
exuk
Flash or die
 
Join Date: May 2004
Location: San Francisco, California
Posts: 127
I have another questions about calling actions to the button. In the two different states i want to have different actions called upon the button, but the button has the same instance name. How do i get around this?
__________________
// rocking flash cs4,as3
exuk is offline   Reply With Quote
Old 02-11-2005, 06:35 AM   #13
Dardoz
Junior Member
 
Join Date: Dec 2000
Posts: 4
maybe i'm not getting something really simple here but, i added an mc to the main timeline of that file and it doesnt pause it
Dardoz is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:18 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.