7 Replies Latest reply: Jun 29, 2012 8:00 PM by DKNMSIS RSS

    on mouseclick logic for timeline animation

    DKNMSIS

      So there is a movieclip that goes through certain stages in a Breaking,BrokenLooping,Fixing,brokenPermenant animation cycle

       

      I'm just starting to make the actionscript for it but I'm not sure how to go about it, or if the logic is sound. I am a bit new to actionscript

       

      i assume i can make a function like this

       

      Quote:

       

        function myButtonAction(eventObject:MouseEvent)

      {
      trace("you clicked on the button ");
      }
      myButton.addEventListener(MouseEvent.CLICK, myButtonAction); 

      where myButton is the instance of a button movieclip ontop of the movieclip

       

      __________________________________________________  __________

       

      The general idea is that randomly things will break and stick in a broken loop, then you have to start taping the screen to repair it until fixed or else it permanently breaks

       

      *note that the fixing animation cycle is backwards so that positive on the timeline is it slowly breaking - you tap to reverse frames and "repair"

       

       

      the logic goes like this (at the start of the looping broken animation)

      on user input (mouseclick)

      -goto frame (xx) (end of fixing animation)

      -stop() for 1 second  (stopping so it doesnt just enter next animation cycle)

      .....     -if user input yes (tap/click)

      .........          -go back one frame [currentframe -1]

      .........          -stop() for 0.100 seconds then play()

      .........          - or if time between taps is less than 0.100, then play

      .....     -if user input is no (no tap/click)

      .........          -play()

       

       

      I'm not sure if that handles all the possibilities but i guess ill find out as i go.

       

      whats the best way to write what I want to happen

        • 1. Re: on mouseclick logic for timeline animation
          Ned Murphy MVP

          The best way to write it is to start with the outline you just gave and figure out each aspect of it in code.  If necessary, build each step of it one step at a time, testing as you go.

          • 2. Re: on mouseclick logic for timeline animation
            DKNMSIS Community Member

            Thanks for the advice, just a few questions though

             

            *there are 6 animations in total so would I start each of these functions from mouse events on those individual buttons?

             

            *Would it be a good idea to use enter_frame events to track where the timeline is and to control other things, or can i just use the mouse events on its own to tell the animation to go back 2 frames on a click etc..

             

            *also since i'm relativly new at actionscript I'm having trouble with my document class, maybe im entering functions wrong or just not understanding how i need to write actionscript

            but i am unsure where to put my new functions and listeners and such.

             

            currently this is the line i want to put in

             

            levelMC.childmc.childmc.childmc.button1.addEventListener(MouseEvent.CLICK, animationcycle);

                    public function animationcycle(event:MouseEvent):void

                        {

                            trace("when i click on the button this text should show up")

                        }

             

             

            I would assume i can put it in here

             

            Document class

             

             

            package

            {

                 Imports

             

                 public class main extends movieclip

                 {

                       public function main()

                       {

                       caching bitmaps and other things i want done on startup

                      eventlistener called Listener

                      }

             

                      public function Listener ()

                      {

                           trace("various lines of code");

                      }

             

            This is where i assume I can just drop

                      levelMC.childmc.childmc.childmc.button1.addEventListener(MouseEvent.CLICK, animationcycle);

                      public function animationcycle(event:MouseEvent):void

                        {

                            trace("when i click on button1 this text should show up")

                        }

             

                   }

            }

             

             

            When I do that I'm getting a acess of undefined propery "levelMC" and "animationcycle"

            • 3. Re: on mouseclick logic for timeline animation
              Amy Blankenship Community Member

              http://active.tutsplus.com/articles/general/flashs-underrated-graphic-symbol/I wouldn't try to add an event listener on something that deeply nested in the constructor of its great-great-great-grandparent. I wouldn't want to count on the fact that the distant decendent would both exist and be on stage when you run that code.

               

              Instead, I'd just add a listener on the document class for a bubbling event. This event could either be a click, and look like this:

               

              protected function onSomethingClicked(e:MouseEvent) {

                 var something = e.target as DisplayObject;

                 if (something && something.name=='button1') {

                   //do something

                }

              }

               

              or, your button could dispatch a more descriptive event, and you could listen for that. Note that you don't need to create a custom event if all you want to know is that something specific happened.

               

              For example:

               

              //in main document in some function like the constructor

              addEventListener('repair', onRepairRequest);

               

              //else where in main document

              protected function onRepairRequest(e:Event):void {

                 //do something

              }

               

              //in childmc.childmc.childmc

              button1.addEventListener(MouseEvent.Click, requestRepair);

               

              protected function requestRepair(e:MouseEvent) {

                 dispatchEvent(new Event('repair'));

              }

               

              Note that Movie Clip Symbols in the library can also have document classes, so you might find it useful or convenient to create this logic closer to the button (in childmc or childmc.childmc etc.).  Also note that if you're going to listen for a bubbling click, you need to set button1's mouseChildren property to false so that you don't miss clicks on its label.

               

              One thing you might want to consider is that a lot of logic really flattens out if you can take advantage of Graphic symbols instead of Movie Clips http://active.tutsplus.com/articles/general/flashs-underrated-graphic-symbol/.

               

              Good luck!

              • 4. Re: on mouseclick logic for timeline animation
                DKNMSIS Community Member

                wow thats a really helpful answer, I'll take the next few days to start implementing that, thanks a ton!

                • 5. Re: on mouseclick logic for timeline animation
                  Amy Blankenship Community Member

                  You're welcome. I noticed you mentioned bitmap caching. You might also enjoy these posts from my blog http://flexdiary.blogspot.com/search/label/Bitmap

                  • 6. Re: on mouseclick logic for timeline animation
                    Amy Blankenship Community Member

                    I noticed there was a mistake in my code.

                     

                    protected function requestRepair(e:MouseEvent) {

                       dispatchEvent(new Event('repair'));

                    }

                     

                    should be

                     

                    protected function requestRepair(e:MouseEvent) {

                       dispatchEvent(new Event('repair', true));//so it will bubble

                    }

                    • 7. Re: on mouseclick logic for timeline animation
                      DKNMSIS Community Member

                      awesome, you are an actionscript goddess!   thanks a ton, i will definatly check out your blog, cheers!