A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: 2-State Button - Need Help

  1. #1
    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

  2. #2
    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

  3. #3
    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);
    };



  4. #4
    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.

  5. #5
    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 12:51 AM.

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    It blew mine. You're an artist, jbum!

    gparis

  7. #7
    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.

  8. #8
    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.

  9. #9
    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 Attached Files
    // rocking flash cs4,as3

  10. #10
    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.

  11. #11
    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 Attached Files
    // rocking flash cs4,as3

  12. #12
    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

  13. #13
    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

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