4 Replies Latest reply on May 5, 2017 10:21 AM by ClayUUID

    Access of undefined property

    davidb42161787

      I have a MovieClip that I have exported for Actionscript at frame 1.

      movieclip00.png

      The following code opens a text file to retrieve the names of image files listed there between commas ("image00.png,image01.png,image02.png,...").

      It then loads those names into an array named "myArrayOfLines," and loads those images into three separate movieclips:

       

      ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////

      var myTextLoader:URLLoader = new URLLoader();

      function onLoaded(e:Event):void {

          var myArrayOfLines:Array = e.target.data.split(/,/);

          var mc1:MainSquare = new MainSquare();

        var ml1:Loader = new Loader();

        var myImageLocation1:URLRequest = new URLRequest(myArrayOfLines[0]);// "image00.png"

        ml1.load(myImageLocation1);

        mc1.addChild(ml1);

        addChild(mc1);

        mc1.x = 0;

        mc1.y = 0;

        var mc2:MainSquare = new MainSquare();

        var ml2:Loader = new Loader();

        var myImageLocation2:URLRequest = new URLRequest(myArrayOfLines[1]);// "image01.png"

        ml2.load(myImageLocation2);

        mc2.addChild(ml2);

        addChild(mc2);

        mc2.x = 33;

        mc2.y = 0;

        var mc3:MainSquare = new MainSquare();

        var ml3:Loader = new Loader();

        var myImageLocation3:URLRequest = new URLRequest(myArrayOfLines[2]);// "image02.png"

        ml3.load(myImageLocation3);

        mc3.addChild(ml3);

        addChild(mc3);

        mc3.x = 66;

        mc3.y = 0;}

      myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

      myTextLoader.load(new URLRequest("myText.txt"));

      ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////

       

      That portion of the code works fine. But, when I add the following code none of the above works anymore at run time and I receive the error messages about access of undefined properties mc1.x += 5, mc1.y += 5, mc2.x += 5, mc2.y += 5, mc3.x += 5, and mc3.y += 5, as though my movieclips don't have any .x or .y properties to move.

       

      What I want the code below to do is to move MovieClips mc1, mc2, and mc3 to the right 5 pixels and to the left 5 pixels when a user mouses over a button named ButtonA. ButtonA is the instance name and it is already on the stage.

       

      ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////

      ButtonA.addEventListener(MouseEvent.MOUSE_OVER,movethem);

      function putback():void {

        mc1.x -= 5;

        mc1.y -= 5;

        mc2.x -= 5;

        mc2.y -= 5;

        mc3.x -= 5;

        mc3.y -= 5;}

      function movethem(e:Event):void {

        mc1.x += 5;

        mc1.y += 5;

        mc2.x += 5;

        mc2.y += 5;

        mc3.x += 5;

        mc3.y += 5;

        setTimeout(putback,1500);}

      ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////

       

      Any help would be greatly appreciated.

        • 1. Re: Access of undefined property
          maria__ Adobe Employee

          Moving to The specified item was not found. forum

          • 2. Re: Access of undefined property
            ClayUUID Adobe Community Professional

            Troubleshooting 101: When you get an undefined property error, the first thing you should do is verify that the thing you're trying to access actually exists. For example, sticking a trace(mc1) statement in your code.

             

            If I'm reading your code correctly, it appears that mc1/2/3 are local variables that only exist inside the myTextLoader function. That's why they can't be accessed outside of it.  You need to declare shared variables outside the functions that use them. Hmm... or if you're trying to access mc1 etc as display object names instead of variables, maybe they aren't getting added to the object you think they're getting added to. Who knows where those naked addChild() statements are pointing at. You should include an explicit display object reference before it.

             

            Furthermore, your loader code is painfully redundant. You should be iterating over arrays instead of repeating 99% the same code three times in a row. Something like this (not tested):

             

            var mcs:Array = [];

            var mls:Array = [];

            var myImageLocations:Array = [];

            function onLoaded(e:Event):void {

                var myArrayOfLines:Array = e.target.data.split(/,/);

                var i:Number;

                var xCoords:Array = [0, 33, 66];

                for (i = 0; i < 3; i++) {

                    mcs[i] = new MainSquare();

                    mls[i] = new Loader();

                    myImageLocations[i] = new URLRequest(myArrayOfLines[i]);

                    mls[i].load(myImageLocations[i]);

                    mcs[i].addChild(mls[i]);

                    addChild(mcs[i]);

                    mcs[i].x = xCoords[i];

                    mcs[i].y = 0;

                }

            }

            1 person found this helpful
            • 3. Re: Access of undefined property
              davidb42161787 Level 1

              Thank you for your time ClayUUID. I did not know that Arrays could doubly serve as movieclips-arrays in this fashion. I wish that global variables were carried over from Actionscript 2.0 to 3.0. But, I appreciate your direction and guidance. And, I will re-write the code in a way that doesn't define my movieclips locally within the load function. Thanks again.

              • 4. Re: Access of undefined property
                ClayUUID Adobe Community Professional

                The code I posted probably won't work as-is since AS3 is supremely rectal about casting references to the "correct" type before accessing methods and properties.

                 

                On further research it appears a vector would be superior to an array.

                flash - Array of movie clips ActionScript 3 - Stack Overflow

                1 person found this helpful