15 Replies Latest reply on Sep 22, 2011 9:52 AM by kglad

    Creating Objects Class

    McbainGames11 Level 2

      Hey,

       

      I need some help with this code it makes and error when i put it into my project but when its in a blank project it works fine?

       

      code:

      package

      {

       

          import flash.display.MovieClip;

       

       

          public class newchars extends MovieClip

          {

       

              public function newchars()

              {

       

                              for (var i:int = 0; i < 1; i++)

                  {

                     

                      var newchar1:MovieClip = new mychar();

                     

                      addChild(newchar1);

       

                      newchar1.x = Math.random() * 500;

       

                      newchar1.x = Math.random()*(stage.stageWidth - newchar1.width);

       

                      newchar1.y =  Math.random()*(stage.stageHeight - newchar1.height);

                     

       

                  }

              }

       

          }

       

      }

       

       

      error:

      TypeError: Error #1009: Cannot access a property or method of a null object reference.

          at newchars()

          at IceMountain_fla::MainTimeline/frame2()

          at flash.display::MovieClip/gotoAndPlay()

          at IceMountain_fla::MainTimeline/fl_MouseClickHandler_2()

       

      as soon as i click my play button on the start menu it interfieres with my class!

      but in a blank project it works fine :S

       

      Class's can make some unusual errors lol

       

       

      Thanks.

        • 1. Re: Creating Objects Class
          smitchell94 Level 2

          Personally i would not do this "var newchar1:MovieClip = new mychar();"

           

          I would do this: var character:MyCharacter = new MyCharacter;

          • 2. Re: Creating Objects Class
            kglad Adobe Community Professional & MVP

            1.  is the newchars class a document class? 

            if not,  that's the problem.  you're not waiting until newchars() has been added to the display list when you try and access the stage.  to remedy, use the addedtostage event.

             

            2.  is mychar the class of a movieclip in your document's library?

            if not, that's a problem.

            1 person found this helpful
            • 3. Re: Creating Objects Class
              McbainGames11 Level 2

              Problem is number 1

              What if i needed to add more than one document class? Because there is only room for one as i see in the program!

              1 person found this helpful
              • 4. Re: Creating Objects Class
                McbainGames11 Level 2

                error:

                #1046:Type was not found or was not a compile-constant: MouseEvent.

                #1046:Type was not found or was not a compile-constant: Event.

                 

                code lines 6 and 7:

                Play_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

                 

                function fl_MouseClickHandler_2(event:MouseEvent):void

                {

                    gotoAndPlay(2);

                }

                • 5. Re: Creating Objects Class
                  kglad Adobe Community Professional & MVP

                  if it's not a document class, use:

                   

                   

                  code:

                  package

                  {

                   

                      import flash.display.MovieClip;

                  import flash.events.Event;

                   

                      public class newchars extends MovieClip

                      {

                   

                          public function newchars()

                          {

                  this.addEventListener(Event.ADDED_TO_STAGE,init);

                          }

                   

                  private function init(e:Event):void{

                   

                                          for (var i:int = 0; i < 1; i++)

                              {

                                 

                                  var newchar1:MovieClip = new mychar();

                                 

                                  addChild(newchar1);

                   

                                  newchar1.x = Math.random() * 500;

                   

                                  newchar1.x = Math.random()*(stage.stageWidth - newchar1.width);

                   

                                  newchar1.y =  Math.random()*(stage.stageHeight - newchar1.height);

                                 

                   

                              }

                  }

                   

                      }

                   

                  }

                   

                   

                  • 6. Re: Creating Objects Class
                    McbainGames11 Level 2

                    I made it a document class but got those nouse event errors from frame 1 that i posted in the previous post , why could that be?

                    • 7. Re: Creating Objects Class
                      kglad Adobe Community Professional & MVP

                      because you're using mouseevents in that or a linked class.

                      • 8. Re: Creating Objects Class
                        McbainGames11 Level 2

                        I am using my mouseevent in frame 1 to navigate to frame 2 from the actions layer, how is this bad? :S

                        • 9. Re: Creating Objects Class
                          kglad Adobe Community Professional & MVP

                          it's not an issue of bad or good.  you asked a question and i answered it.

                           

                          you started this thread asking a different quesiton and i answered it.

                          • 10. Re: Creating Objects Class
                            McbainGames11 Level 2

                            I appreciate help but i followed what you said about document class now it comes with mouseevent error and i am wondering why the error occurs when everything else is good? The mouse event has nothing to do with the class but it still has an error!

                            • 11. Re: Creating Objects Class
                              kglad Adobe Community Professional & MVP

                              if you haven't changed newchars class since your original post or you used the code i suggested, you wouldn't see that error.

                               

                              if you think you're doing one or the other above, you should create a new directory, save your fla to that directory and save your document class file to that directory.  then retest.

                              • 12. Re: Creating Objects Class
                                McbainGames11 Level 2

                                I have been doing your code now this is all the code now please tell me my error it is weird and i cannot work it out

                                 

                                Frame 1:

                                 

                                stop();

                                 

                                Play_1.addEventListener(Event.ENTER_FRAME, fl_MouseClickHandler_2);

                                 

                                function fl_MouseClickHandler_2(event:Event):void

                                {

                                    gotoAndPlay(2);

                                }

                                 

                                 

                                Frame 2:

                                 

                                stop();

                                 

                                var enemy1:newchars = new newchars();

                                 

                                addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

                                 

                                function fl_EnterFrameHandler(event:Event):void

                                {

                                    Bird_1.x = mouseX;

                                }

                                 

                                 

                                newchars class:

                                 

                                package

                                {

                                 

                                    import flash.display.MovieClip;

                                    import flash.events.Event;

                                 

                                    public class newchars extends MovieClip

                                    {

                                 

                                        public function newchars()

                                        {

                                 

                                            this.addEventListener(Event.ADDED_TO_STAGE, addtostage);

                                 

                                        }

                                 

                                        private function addtostage(e:Event):void

                                        {

                                 

                                 

                                            for (var i:int = 0; i < 10; i++)

                                            {

                                 

                                                var newchar1:MovieClip = new mychar();

                                 

                                                addChild(newchar1);

                                 

                                                newchar1.x = Math.random()*(stage.stageWidth - newchar1.width);

                                 

                                                newchar1.y = 28.35;

                                 

                                            }

                                        }

                                    }

                                }

                                 

                                 

                                This doesn't have a document class used in settings because i am using add to stage event as you said

                                 

                                Now please look over all the code in my .fla and tell me why i get this error:

                                 

                                Error: Error #2136: The SWF file file:///C|/Users/Brendon%20Battye/Desktop/Ice%20Mountain/Ice%20Mountain.swf contains invalid data.

                                    at newchars/frame2()

                                    at flash.display::MovieClip/gotoAndPlay()

                                    at newchars/fl_MouseClickHandler_2()

                                • 13. Re: Creating Objects Class
                                  kglad Adobe Community Professional & MVP

                                  use:

                                   

                                   

                                   

                                  Frame 1:

                                   

                                  stop();

                                   

                                  Play_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

                                   

                                  function fl_MouseClickHandler_2(event:Event):void

                                  {

                                      gotoAndStop(2);

                                  }

                                   

                                   

                                  Frame 2:

                                   

                                  stop();

                                   

                                  var enemy1:newchars = new newchars();

                                  addChilld(enemy1);

                                   

                                  addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

                                   

                                  function fl_EnterFrameHandler(event:Event):void

                                  {

                                      Bird_1.x = mouseX;  // assuming Bird_1 is on-stage

                                  }

                                  • 14. Re: Creating Objects Class
                                    McbainGames11 Level 2

                                    Yay!

                                     

                                    Omfg i cannot believe i would not see this LOL i feel really bad now you gave me right answer and even i knew everything seemed to be ok with the class but i tested heaps of diffrent ways before this thread and must of got it mixed up

                                     

                                    THANKYOU FOR THIS HAHA

                                    • 15. Re: Creating Objects Class
                                      kglad Adobe Community Professional & MVP

                                      you're welcome.