11 Replies Latest reply on May 12, 2009 6:09 AM by kglad

    create cartoon using AS2 code only. WOW but...

    codeBeastAdobe Level 1
      Hi guys.
      Some lovely code I picked up to create cartoons. My character is sunny. We create multiple empty mcs as holders for a) Sounds and b) Attached or loaded mcs.
      BUT the damn sound object doesn't work.
      sound_1.start(); diesn't work - however it works it I move it right up under the attachsound code.
      Then the next sound_2.start definately work.
      I DID get to work in other code I had even though it was even inside a function. I will paste it under this code.

       


      numOfSounds = 5;
      numOfObjects = 5;
      objectPath = "images";

       


      _root.createEmptyMovieClip("soundLib", 1);
      for (var i:Number = 1; i<=numOfSounds; i++) {
      soundLib.createEmptyMovieClip("holder_"+i, i);
      _root["sound_"+i] = new Sound(soundLib["holder_"+i]);
      }

       

      sound_1.attachSound("1");
      sound_2.attachSound("2");
      sound_3.attachSound("3");
      sound_4.attachSound("4");
      sound_5.attachSound("5");
      //sound_1.start(0, 999);// only for bg music

       


      _root.createEmptyMovieClip("objectLib", 1);
      for (var i:Number = 1; i<=numOfObjects; i++) {
      objectLib.createEmptyMovieClip("holder_"+i, i);
      _root["object_"+i] = (objectLib["holder_"+i]);
      }

       

      object_1.attachMovie("sunny", "sunny", 1);// object1 is the empty mc and sunny is the linkage name and sunny is the instance name - usually called something different.
      loadMovie(objectPath+"/cat_a.swf", object_2);
      loadMovie(objectPath+"/cow_a.swf", object_3);
      loadMovie(objectPath+"/bird_a.swf", object_4);
      loadMovie(objectPath+"/pig_a.swf", object_5);

       

      object_1._x=500// Sunny
      object_1._width=50
      object_1._height=50
      object_2._x=200
      object_3._x=300
      object_4._x=400
      object_5._x=500

       

      object_1._y=100// loop and use spacing
      object_2._y=200
      object_3._y=300
      object_4._y=400
      object_5._y=500

       

      sound_1.start();
      object_1.sunny.gotoAndPlay("speak

       

      sound_1.onSoundComplete = function() {
      _root.object_1.sunny.gotoAndPlay("static");

       

      sound_2.start();

       

      }
      sound_2.onSoundComplete = function() {
      sound_3.start();
      _root.sunny.gotoAndPlay("speak");
      }

       

       

       


      The sound code on its own DID work - I paste it here

       

      numOfSounds = 5;
      _root.createEmptyMovieClip("soundLib", 1);
      for (var i:Number = 1; i<=numOfSounds; i++) {
      soundLib.createEmptyMovieClip("holder_"+i, i);
      _root["sound_"+i] = new Sound(soundLib["holder_"+i]);
      }
      // use sound_1.loadSound to load externally.
      //For my dialogues only need one movie clip and load into as you need - remember to unload sound as welll
      sound_1.attachSound("1");
      sound_2.attachSound("2");
      sound_3.attachSound("3");
      sound_4.attachSound("4");
      sound_5.attachSound("5");
      //sound_1.start(0, 999);
      sound_1.start();
      _root.sunny.gotoAndPlay("speak");
      sound_1.onSoundComplete = function() {
      _root.sunny.gotoAndPlay("static");
      sound_2.start();
      _root.r.nextFrame();
      }
      sound_2.onSoundComplete = function() {
      _root.r.gotoAndStop(2);
      sound_3.start();
      _root.sunny.gotoAndPlay("speak");
      };
        • 1. Re: create cartoon using AS2 code only. WOW but...
          kglad Adobe Community Professional & MVP

          you're removing _root.soundLib (all all its children that your sounds use) when you create _root.objectLib.

           

          to remedy, create objectLib at depth 2.

          • 2. Re: create cartoon using AS2 code only. WOW but...
            codeBeastAdobe Level 1

            BRILLIANT. Works - I can't believe how much you have just helped me and in record time. I am close to having the code to develop cartoons in just AS2 whereas everybody I know uses the timeline and hundreds of frames.

             

            I am using the onSoundComplete event to move to next animation BUT I don't know what to use when there is no sound. I mean if I want my character to laugh after walking - I can't see any EVENT to trigger - is there anythng like onAnimationCompleted??? If there isn't I would have to invent a blank sound lasting the length of the laugh or walk or any animation. All of these behaviours are nested inside my main mc "Sunny".

            • 3. Re: create cartoon using AS2 code only. WOW but...
              kglad Adobe Community Professional & MVP

              you can use any of a few timer functions to trigger periodic actions.  check setInterval() function to see how to call a function after a certain amount of time.  and then use setTimeout() if you only want to call the function once, instead of repeatedly.

               

              if you want to trigger some action after your character has moved some distance or beyond a certain location, you can use an if-statement in the loop that updates the characters position.

              1 person found this helpful
              • 4. Re: create cartoon using AS2 code only. WOW but...
                codeBeastAdobe Level 1

                sound_2.onSoundComplete = function() {
                //sound_3.start();
                _root.object_1.sunny.gotoAndPlay("happy");
                var my_timedProcess:Number = setTimeout(my_delayedFunction, 2000);
                function my_delayedFunction() {
                  _root.object_1.sunny.gotoAndPlay("laugh");
                }
                };

                 

                Good - after 2 seconds it calls the next behavour ie: she's happy the she laughs.

                 

                BUT - This means that everytime I change from one behaviour to another I MYSELF have to tell flash when to change. This is no event triggered at the end of a nested animation I take it (like the onSoundCompleted for sounds)

                 

                So I will have to calculate via frames per second example a 48 frames behaviour with frame rate of 24ps would be 2 seconds ie: 2000. BUT I have been told there can be a lag in flash movies on the internet therefore I didn't want to rely on his method. If there is a lag I could possibly call the next function too early cutting off for example the walk cycle.

                By the way if I do use this method I would not need to use the clearTimeout would I?

                XHEERS for all your wonderful help - I really do appreciate it.

                • 5. Re: create cartoon using AS2 code only. WOW but...
                  codeBeastAdobe Level 1

                  sound_2.onSoundComplete = function() {
                  //sound_3.start();
                  _root.object_1.sunny.gotoAndPlay("happy");
                  var my_timedProcess:Number = setTimeout(my_delayedFunction, 2000);
                  function my_delayedFunction() {
                    _root.object_1.sunny.gotoAndPlay("walk");
                    var my_timedProcess2:Number = setTimeout(my_delayedFunction2, 3083);
                    function my_delayedFunction2() {
                     _root.object_1.sunny.gotoAndPlay("laugh");
                     var my_timedProcess2:Number = setTimeout(my_delayedFunction2, 2083);
                     function my_delayedFunction2() {
                      _root.object_1.sunny.gotoAndPlay("static");
                     }
                    }
                  }
                  };

                   

                  YES - This what I have done in the end by calculating the frames length of a behaviour like "laugh" and divide it by the frame rate of movie.

                  Is this a foolproof way of doing it - ie Are they REAL seconds or subject to the frames - I mean if the movie slows down on the internet it shouldn't affect my code.

                  I was also thinking that I could put some action code on the last frame of the nested mc (behaviour) which would trigger something and catch that as it were.

                  • 6. Re: create cartoon using AS2 code only. WOW but...
                    kglad Adobe Community Professional & MVP

                    your swf's framerate will be somewhat inconsistant.  for many things, the frame rate is consistant enough to be acceptable.  for things like lip-sync'ing it's unacceptable.

                     

                    also, if you use any of the tween classes to animate your objects, they all have complete events that can be used to trigger code to execute.

                    • 7. Re: create cartoon using AS2 code only. WOW but...
                      codeBeastAdobe Level 1

                      Lip synching - No problem as onSoundComplete is used.

                      Tween class - fine.

                       

                      However, I do need to be more accurate with other behaviours I have. How about my idea of writng AS in the last frame of the nested mc and "checking" for it. When I "see" it then call another function.

                      Is this possible as it would be the most accurate way for triggering an event???

                      You are a very busy man, I have seen the amount of posts you have made tonight.

                      • 8. Re: create cartoon using AS2 code only. WOW but...
                        kglad Adobe Community Professional & MVP

                        you can add code to the last frame but that has some drawbacks.  mainly, it makes it tougher to debug your application because code is not centralized in one location.

                         

                        so, if you do that, place a comment near the function that's called by the script and specify the location of the code that's calling that function.

                        • 9. Re: create cartoon using AS2 code only. WOW but...
                          codeBeastAdobe Level 1

                          That's INCREDIBLE news as it's EXACT ie: frame based not time based. Non centralized code is OK as these are small animations not big apps and of course a comment by the function calling is perfectly understandable.

                          However I don't know how to do this. I've tried various ways of doing it but I can't get them to work. Is it possible to write me out that function quickly and pop it in my code. Also, would I put a global variable on the last frame of the target nested mc.  eg: _global.walkCompleted =true;

                          • 10. Re: create cartoon using AS2 code only. WOW but...
                            codeBeastAdobe Level 1

                            Sorry. Just clarifying the las comment.

                            I need the control of the movie to be on the AS in the main timeline NOT in the nested mc. The technique of putting code on the last frame eg: to gotAndPlay another "behaviour" eg: walk or play is NOT my idea BUT to put a boolean in ie: onlastFrame=true on the last frame.

                            This would be checked for in a function on the main timeline. Here is where I get lost. How can I check for that boolean in the maintimeline if the nested mc is running. It would have to be checking every frame of the nested clip.

                            • 11. Re: create cartoon using AS2 code only. WOW but...
                              kglad Adobe Community Professional & MVP

                              the code in your nested movieclip will need to use the path to the main timeline (if it calls a function on the main timeline) or the looping function on the main timeline will need to use the path to your movieclip (if you use a main timeline loop to check if a nested movieclip variable has been changed or initialized).