14 Replies Latest reply on Nov 22, 2010 9:19 PM by kglad

    flash classes

    iconofsin14

      with a flash class you assign a class name to an object and created a external AS file which contains

       

      public class PhotoPanelsFINISHED extends MovieClip
      }
      code

      {

       

      However when i have played with it both the public class and the AS file name have to match the class name of the object
      why is this and how are you supposed to put code for multiple classes in the same file?

       

      also in a flash work which i used a class and external AS for It originally had the classname declared in the stage property.
      This was fine until i moved the main object manipulated to the second frame and the program just glitches. I can fix this by putting a copy of the object on frame 1 (i put it in the off stage area)
      why is it doing this?

      I tried deleteing the class name in stage properties and attatching it to the movieclips own class name but this does not work

       

      here is the full code  just incase it helps

       

      ------------------------------------------


      package
      {
      import com.greensock.*;
      import com.greensock.easing.*;
      import flash.display.*;
      import flash.events.*;

      public class PhotoPanelsFINISHED extends MovieClip
      {
        private var inFocus:MovieClip;
       
        public function PhotoPanelsFINISHED():void
        {
         setupClips();
         addEventListener(Event.ENTER_FRAME, loop);
        }
       
        private function setupClips():void
        {
         var len:int = con.numChildren;
        
         for(var i:int=0; i<len; i++)
         {
          var mc:MovieClip = MovieClip(con.getChildAt(i));
          mc.buttonMode = true;
          mc.loc = [mc.x, mc.y];
          mc.addEventListener(MouseEvent.ROLL_OVER, onOver);
          mc.addEventListener(MouseEvent.ROLL_OUT, onOut);
          mc.addEventListener(MouseEvent.CLICK, onClick);
         }
        }
       
        private function onOver(e:MouseEvent):void
        {
         var mc:MovieClip = MovieClip(e.currentTarget);
         if(mc != inFocus)
          TweenLite.to(mc.enlarge, 0.2, {
               alpha:1
              });
        }
       
        private function onOut(e:MouseEvent):void
        {
         var mc:MovieClip = MovieClip(e.currentTarget);
         if(mc != inFocus)
          TweenLite.to(mc.enlarge, 0.2, {
               alpha:0
              });
        }
       
        private function onClick(e:MouseEvent):void
        {
         var mc:MovieClip = MovieClip(e.currentTarget);
        
         if(inFocus == null)
         {
          scaleUp(mc);
         }
        
         else if(inFocus == mc)
         {
          // PUT CON.ADDCHILD BELLOW AND NEUTRAL CON POSITIONING BELLOW
          con.addChild(mc);
          mc.scaleX = 3;
          mc.scaleY = 3;
          mc.x = 0;
          mc.y = 0;
         
          TweenLite.to(inFocus, 0.5, {
             
               scaleX:1,
              scaleY:1,
              rotationY:0,
              rotationX:0,
              x:inFocus.loc[0],
              y:inFocus.loc[1],
              ease:Expo.easeInOut,
              onComplete:function(){inFocus=null;}
             });
         }
        
         else
         {
          // PUT CON.ADDCHILD BELLOW AND NEUTRAL CON POSITIONING BELLOW
          con.addChild(inFocus);
          inFocus.scaleX = 3;
          inFocus.scaleY = 3;
          inFocus.x = 0;
          inFocus.y = 0;
         
          TweenLite.to(inFocus, 0.5, {
               scaleX:1,
              scaleY:1,
              rotationY:0,
              rotationX:0,
              x:inFocus.loc[0],
              y:inFocus.loc[1],
              ease:Expo.easeInOut,
              onComplete:scaleUp,
              onCompleteParams:[mc]
             });
         }
        }
       
        private function scaleUp(mc:MovieClip):void
        {
         inFocus = mc;
         con.addChild(mc);

         TweenLite.to(mc, 0.5, {
         scaleX:3,
         scaleY:3,
         x:0,
         y:0,
         rotationY:1080,
         rotationX:1080,
         ease:Expo.easeInOut,
         onComplete:moveStuff,
         onCompleteParams:[mc]
         });

         function moveStuff(_clip:Sprite):void
         {
         stage.addChild(_clip);
         mc.enlarge.alpha = 0;
         mc.x = stage.stageWidth / 2 + 1;
         mc.y = stage.stageHeight / 2 - 4;
         mc.scaleX = 1.15;
         mc.scaleY = 1.15;
         }
        }
       
        private function loop(e:Event):void
        {
         var distx:Number = mouseX / 650;
         var disty:Number = mouseY / 450;
         TweenLite.to(con, 2, {
               rotationY:(-70 + (140*distx)),
              rotationX:(70 - (140*disty)),
              ease:Expo.easeOut
             });
        }
      }
      }

      -

        • 1. Re: flash classes
          kglad Adobe Community Professional & MVP

          if the class name didn't match the file name, flash would need to open every class file in your class path to find the needed classes.  that's not an effective scheme.  requiring the class name to match the file name is much more sensible and is standard in oop.

           

          and i don't understand your issue.  click file/publish settings/flash and tick "permit debugging".   retest.  copy and paste the error message.

          • 2. Re: flash classes
            iconofsin14 Level 1

            done it


            the error is


            1102; Access of undefined property con
            this repeats 6 times for lines 20, 24,63,85,108,137

             

            con is the instance name of an object called container


            remember this script works fine when the stage class is set to PhotoPanelsFINISHED (rather than container being given this class name) but only if an instance of con is on the first frame.

            • 3. Re: flash classes
              kglad Adobe Community Professional & MVP

              copy and paste the error message and copy and paste the first 24 lines of code in the frame mentioned in the error message.

              • 4. Re: flash classes
                iconofsin14 Level 1

                also

                 

                if i set the stage class name to PhotoPannelsFINISHED and dont have a class name for the objects
                and an instance of container with the instance name of con is NOT present on frame 1 i get this error

                 

                TypeError: Error #1009: Cannot access a property or method of a null object reference.
                at PhotoPanelsFINISHED/setupClips()[C:\websites\mysite\flash\PhotoPanelsFINISHED.as:20]
                at PhotoPanelsFINISHED()[C:\websites\mysite\flash\PhotoPanelsFINISHED.as:14]

                 

                 

                if i set the stage class name to PhotoPannelsFINISHED and dont have a class name for the objects
                and an instance of container with the instance name of con is present on frame 1 i get no errors

                • 5. Re: flash classes
                  kglad Adobe Community Professional & MVP

                  copy and paste the error message and copy and paste the first 24 lines of code in the frame/class mentioned in the error message.

                  • 6. Re: flash classes
                    iconofsin14 Level 1

                    the error messages
                    C:\websites\mysite\flash\PhotoPanelsFINISHED.as, Line 24 1120: Access of undefined property con.
                    C:\websites\mysite\flash\PhotoPanelsFINISHED.as, Line 20 1120: Access of undefined property con.
                    C:\websites\mysite\flash\PhotoPanelsFINISHED.as, Line 63 1120: Access of undefined property con.
                    C:\websites\mysite\flash\PhotoPanelsFINISHED.as, Line 85 1120: Access of undefined property con.
                    C:\websites\mysite\flash\PhotoPanelsFINISHED.as, Line 108 1120: Access of undefined property con.
                    C:\websites\mysite\flash\PhotoPanelsFINISHED.as, Line 137 1120: Access of undefined property con.

                    ------------------------------------------------------------------------------------------ -------------------------------------------------------------

                     

                    The code up to line 31 (enough to cover the private function which line 24 falls in)

                     

                    package
                    {
                    import com.greensock.*;
                    import com.greensock.easing.*;
                    import flash.display.*;
                    import flash.events.*;

                    public class PhotoPanelsFINISHED extends MovieClip
                    {
                      private var inFocus:MovieClip;
                     
                      public function PhotoPanelsFINISHED():void
                      {
                       setupClips();
                       addEventListener(Event.ENTER_FRAME, loop);
                      }
                     
                      private function setupClips():void
                      {
                       var len:int = con.numChildren;
                      
                       for(var i:int=0; i<len; i++)
                       {
                        var mc:MovieClip = MovieClip(con.getChildAt(i));
                        mc.buttonMode = true;
                        mc.loc = [mc.x, mc.y];
                        mc.addEventListener(MouseEvent.ROLL_OVER, onOver);
                        mc.addEventListener(MouseEvent.ROLL_OUT, onOut);
                        mc.addEventListener(MouseEvent.CLICK, onClick);
                       }
                      }


                    ---------------------------
                    Container is the name of a library object
                    con is the instance name it is given
                    container has child objects which are movieclips names photo1, photo2, photo3 ect

                     

                    This will be very helpfull for me and i do apreciate it
                    i have already added frame 1 by having an offscreen instance of con but as the content of the file gets larger i will need preloaders and may need aditional container like movieclips so this does need to be fixed.

                    • 7. Re: flash classes
                      kglad Adobe Community Professional & MVP

                      if that's your document class, flash doesn't find a displayobject with instance name con in frame 1.

                      • 8. Re: flash classes
                        iconofsin14 Level 1

                        If i set the document class to PhotoPannelsFINISHED it does not work if con is not found in

                        frame 1

                         

                        but if i remove the document class and give the container movieclip the class PhotoPannelsFINISHED it still does not work
                        • 9. Re: flash classes
                          kglad Adobe Community Professional & MVP

                          is there anything about either of those two situations you don't understand?

                          • 10. Re: flash classes
                            iconofsin14 Level 1

                            "is there anything about either of those two situations you don't understand?"

                             

                            It would appear that the answer is yes
                            why i am able to put to attatch the class to the document and have it work (but only if con is found on frame 1)
                            but i am not able to attach the class to the container object in the library and have it work

                            How easy would it be to convert that code into code which can simply be inserted into the right frame?

                            • 11. Re: flash classes
                              kglad Adobe Community Professional & MVP

                              why i am able to put to attatch the class to the document and have it work (but only if con is found on frame 1)

                              your document class is invoked as soon as frame 1 of the main timeline loads.  if con doesn't exist at that time, you will see a null object error
                              but i am not able to attach the class to the container object in the library and have it work

                              if a movieclip is assigned that class, con will need to be in the first frame of the container object and you may (or may not) need an addedtostage event listener to ensure con instantiated when that class is invoked

                              How easy would it be to convert that code into code which can simply be inserted into the right frame:

                               


                                 setupClips();
                                 addEventListener(Event.ENTER_FRAME, loop);

                              function loop(e:Event):void{

                              // do something



                              function setupClips():void
                                {
                                 var len:int = con.numChildren;
                                
                                 for(var i:int=0; i<len; i++)
                                 {
                                  var mc:MovieClip = MovieClip(con.getChildAt(i));
                                  mc.buttonMode = true;
                                  mc.loc = [mc.x, mc.y];
                                  mc.addEventListener(MouseEvent.ROLL_OVER, onOver);
                                  mc.addEventListener(MouseEvent.ROLL_OUT, onOut);
                                  mc.addEventListener(MouseEvent.CLICK, onClick);
                                 }
                                }

                               

                              function onOver(e:MouseEvent):void{

                              }

                              function onOut(e:MouseEvent):void{

                              }

                              function onClick(e:MouseEvent):void{

                              }

                               

                              1 person found this helpful
                              • 12. Re: flash classes
                                iconofsin14 Level 1

                                Thanks for that

                                once i get to
                                function onOver(e:MouseEvent):void{

                                Do i just copy and paste the rest of the code as it exists already?

                                • 13. Re: flash classes
                                  iconofsin14 Level 1

                                  Thanks for that


                                  i have managed to get it working

                                   

                                  Now i do not need to worry about that being a nusance if i ever need to add multiple frames of content or a preloader

                                   

                                  tell me was there anything "wrong" with my original solution of having an offscreen version of con?
                                  would this have increased loadtime or CPU usage significantly?
                                  • 14. Re: flash classes
                                    kglad Adobe Community Professional & MVP

                                    there's no significant downside to adding con to frame 1.  but placing it off-stage is problematic if you ever load this swf into another.

                                     

                                    it's preferrable to add it on-stage and assign its visible property to false.

                                     

                                    and, it would be cleaner to not add con to a frame where it's unneeded and instead check for frame 2 of the main timeline to play before trying to reference con (assuming it is in frame 2).

                                    1 person found this helpful