5 Replies Latest reply on Aug 13, 2010 2:12 PM by Ned Murphy

    Need help in arrays

    Erika Malthus

      Hi, I'm trying to do something in flash but since I'm a beginner I can't do it alone.


      So the thing is, I'm trying to move the mouse and check how many pixels I "walked" by comparing the positions every frame, my idea was doing it with arrays, but I really don't know how to make it. Could anybody help me? Or could make a simple .fla where I can learn?


      Thanks !

        • 1. Re: Need help in arrays
          Ned Murphy Adobe Community Professional & MVP

          Can you explain a little more about what/why you want this data?  It may not be necessary to store anything in an array unless you have some need to recall the different values you may have moved from the start.  Also, please divulge which version of actionscript you are planning to use.

          1 person found this helpful
          • 2. Re: Need help in arrays
            Erika Malthus Level 1

            Well, one of the objectives is to learn how to work with arrays, but i have no specific reason to do this, it's like an exercise.

            I use cs4 with Action Script 2.0

            Thanks for replying!

            • 3. Re: Need help in arrays
              Ned Murphy Adobe Community Professional & MVP

              What values are you planning to store in the array?  Think before you answer... it's as important as learning how to work with arrays.

              • 4. Re: Need help in arrays
                Erika Malthus Level 1

                the values?.. of course the coordinates, and compare to the previous frame. i know i can do it in this away, but don't know how

                • 5. Re: Need help in arrays
                  Ned Murphy Adobe Community Professional & MVP

                  I know you'll probably have more questions, but maybe with a little playing around you'll be able to start discovering things yourself.  Here are some basics about arrays...  To create an array you need to declare it...


                  var coordinatesArray = new Array();


                  and to add values to it you can use the push() method, which will add whatever you indicate to the end of the array.


                  corrdinatesArray.push( something );


                  You should look up the Array class in the Flash AS2 help documents to see all of the properties and methods that are available to use with an array.


                  This is where it will get a little more complicated than you probably intended because you said you wanted to store the coordinates.  There are two coordinates related to the mouse/cursor position, _xmouse and _ymouse.  So if you want to store them it is probably best done as a set.


                  Flash has a special class for coordinates called the Point class.  You should read up on that as well because it includes methods and properties that may come in useful for what you say you want to do, such as determining the distance between two points.


                  So, to add a new set of points to the array you could use...


                  coordinatesArray.push(new Point(_xmouse, _ymouse));


                  but to make use of the Point class you need to import it into the file.


                  Below is a little bit of code that you should experiment with and try to understand what is going on.  Just open a new file and place this code in the timeline.  Then run the file and start clicking anywhere around the stage... try to see what each line is doing for you...


                  import flash.geom.Point;


                  var coordinatesArray:Array = new Array();


                  this.onMouseDown = function(){
                        coordinatesArray.push(new Point(_xmouse, _ymouse));
                        for(i=0; i<coordinatesArray.length; i++){
                              trace("x: "+coordinatesArray[i].x+"   y: "+coordinatesArray[i].y);
                        trace("  ");