10 Replies Latest reply: May 7, 2012 8:58 AM by brendan7979 RSS

    Timer to alternately show/hide 2 objects

    saratogacoach Community Member

      Hi,

       

      I'm trying to learn how to use a Timer and AS3 to set up 2 objects (for example, 2 squares, each with a separate instance name) that would show/hide alternately for 500 ms each for 30 seconds (30000 ms) total. Easy enough to do using a timeline, but instead, I need to set this up to include using AS3 to add to a companion .as script file (Main.as), which loads and all runs on Frame 1. I first looked at using setTimeout or setInterval, but these don't seem to have enough properties to do what is needed. So, Timer is probably the best choice. I've read the online documents and examples for a Timer, but still can't figure out how for example to alternate one object's 500 ms show with the other's 500 ms hide, then alternate/switch them, and stop the alternating sequence after 30 seconds.

       

      Any help, example AS3 script would be great help.

       

      Regards,

        • 1. Re: Timer to alternately show/hide 2 objects
          Ned Murphy CommunityMVP

          To control showing and hiding just use the visible properties of your objects, setting one to be the opposite of the other.  As far as controlling the timing goes, study/experiment with the Timer to see how you can set the delay and the repeat count to meet your needs

          • 2. Re: Timer to alternately show/hide 2 objects
            saratogacoach Community Member

            Hi Ned,

             

            Unfortunately that's what I have been trying, without success. Actually, have gotten fairly stuck in experimenting and am hoping that someone can provide a simple example. The following is some of the "gibberish" that I tried without success.

             

            private var c1On: uint = 500
            private var c1Off: uint = 500
            private var c2On:uint = 500
            private var c2Off:uint = 500
            private var c1OnTimer:Timer
            private var c2OnTimer:Timer

             

            c1OnTimer = new Timer(c1On, 30);
            c1OnTimer.addEventListener(TimerEvent.TIMER, c1OnF);
            public function c1OnF(event:TimerEvent) {
            mcsquare1.visible = true
            mcsquare2.visible = false
            }

            c2OnTimer = new Timer(c2On, 30);
            c2OnTimer.addEventListener(TimerEvent.TIMER, c2OnF);
            public function c2OnF(event:TimerEvent) {
            mcsquare2.visible = true
            mcsquare1.visible = false
            }


            c1onTimer = new Timer(c1On, 30);
            c1onTimer.addEventListener(TimerEvent.TIMER, c1onF);  
            //onTimer.start();
            }//end
            public function c1onF(event:TimerEvent) {
            mcsquare1.visible = true
            mcsquare2.visible = false
            }//end c1onF

            c1offTimer = new Timer(c1Off, 30);
            c1offTimer.addEventListener(TimerEvent.TIMER, c1offF);  
            //onTimer.start();
            }//end
            public function c1offF(event:TimerEvent) {
            mcsquare1.visible = false
            mcsquare2.visible = true
            }//end c1onF

            • 3. Re: Timer to alternately show/hide 2 objects
              Ned Murphy CommunityMVP

              Yeah, it looks like you are over-thinking it.  One timer should be all you need. 

               

              The problem with giving you a simple example is that the simplest example is the solution to what you say you are trying to learn how to do.  Doing it for you is not you learning how.

               

              As I said, you just need to make the one object's visible property the oppostite of what the other is each time you change the other...

               

              mcsquare1.visible = !mcsquare1.visible;    // switches the visible state to the opposite

              mcsquare2.visible = !mcsquare1.visible;    // ditto

              • 4. Re: Timer to alternately show/hide 2 objects
                saratogacoach Community Member

                Yes, I've been coming up with far too complex script (that doesn't work), when it should be simple.

                 

                If I set up a simple Timer with your suggestion,

                 

                mcsquare1.visible = !mcsquare1.visible; // switches the visible state to the opposite

                mcsquare2.visible = !mcsquare1.visible; // ditto

                 

                would I need one or two timers? One for the visible square1 and not visible square2, alternating with one for the visible square2, not visible square1? How do I get the second timer (assuming two timers needed) to fire 500ms after the first, then repeat this pattern for 30 seconds? I've been lost trying to figure out both how to create a simple script and then implement it in AS3.

                 

                Regards,

                • 5. Re: Timer to alternately show/hide 2 objects
                  Ned Murphy CommunityMVP

                  You need to reread my reply.

                  • 6. Re: Timer to alternately show/hide 2 objects
                    saratogacoach Community Member

                    OK, I think it means that one timer can do the job.

                     

                    So, something like?:

                     

                    private var c1On: uint = 500

                    private var c1OnTimer:Timer

                     

                    c1onTimer = new Timer(c1On, 30);

                    c1onTimer.addEventListener(TimerEvent.TIMER, c1onF);

                    }

                    public function c1onF(event:TimerEvent) {

                    mcsquare1.visible = !mcsquare1.visible;

                    mcsquare2.visible = !mcsquare1.visible;

                    }

                     

                    Not sure this will work, how to trigger in a button.

                     

                    Regards,

                    • 7. Re: Timer to alternately show/hide 2 objects
                      Ned Murphy CommunityMVP

                      Use the button to start the timer.

                      • 8. Re: Timer to alternately show/hide 2 objects
                        saratogacoach Community Member

                        OK, I was able to get it working. And, one timer! :-)

                         

                        Thank you very much for your help.

                         

                        Regards,

                        • 9. Re: Timer to alternately show/hide 2 objects
                          Ned Murphy CommunityMVP

                          You're welcome ( looks like they are literally "messing" around with the forums )

                          • 10. Re: Timer to alternately show/hide 2 objects
                            brendan7979

                            Thanks a ton for the posting's on this. Fishing through all the random link's on the Google's, Yahoo's, Bing's, etc's... Of the world was killing me. Now I know... And if I can remember correctly... Knowing is half the battle

                             

                            Thanks again!

                             

                            B