A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: AS3 and Arrays

  1. #1
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59

    AS3 and Arrays

    Hi all

    I'm currently working on what I'll refer to as a "race viewer." Basically, it's something that will show a replay of a horse race that has already taken place in PHP and is stored in a database elsewhere. What I would like to be able to do is run a loop that will move the horses according to their time or predetermined x position on the stage. I had initially planned to do this by using some variables similar to this, which I have working in PHP:

    timedist[1][400]=27.1;

    Essentially, the 1 represents the number of the horse (which will vary from 6 to 12, depending on the race) and the 400 represents the distance segment in the race. I have everything divided up into 400m segments and most races are between 1,609 and 3,200m in length. So, a single horse would be similar to this:

    timedist[1][400]=27.1;
    timedist[1][800]=58;
    timedist[1][1200]=94;

    etc..

    I have since found that multidimensional arrays aren't supported by Flash and I'm truly stumped as to how I can do this. I really need to be able to find the time based upon the horse number and the distance and I'd like to be able to do this without having to make individual variables for each horse, for each distance and then have to have an incredibly huge, not even a loop but something to display the horses accordingly.

    Sorry if this is confusing but I am confusing myself even more with frustration lol

    Thanks in advance for any help!!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Quote Originally Posted by jello_chick View Post
    Hi all

    I have since found that multidimensional arrays aren't supported by Flash...
    Yes they are, you just have to instantiate each row's column array individually.

    Code:
    var twoDArr:Array = [];
    for (var i:int = 0; i < numrows; i++){
      twoDArr[i] = [];
      for (var j:int = 0; j < numcols; j++){
        twoDArr[i][j] = someInitialValue;
      }
    }
    If that's too cumbersome, you could use the a datastructures package which masks the behind the scenes setup for you.
    http://code.google.com/p/polygonal/wiki/DataStructures

  3. #3
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59
    Thank you so much, I'll be sure to give that a try

  4. #4
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59
    I was wondering if you might be able to assist me with the polygonal DataStructures library? I downloaded and have followed the instructions in the Wiki area about installing it for Flash CS4 and have created a Main.as file with their sample code to initialize everything. In my flash file (which has it's class as Main.as) I try to call up the Array2 to create my own array and I receive the following errors:

    1046: Type was not found or was not a compile-time constant: Array2
    1180: Call to a possibly undefined method Array2

    The instructions I followed were here:
    http://code.google.com/p/polygonal/w...gActionScript3

    Thank you very much for your help, I really appreciate it!

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I've actually never used his haxe based DataStructures library. I did use the preceding as3ds library. But not in Flash, just FlashDevelop.

    Are you familiar with using 3rd party SWCs? It should be no different from any other SWC in that respect.

    The error you are getting makes it look like either Flash isn't seeing the classes in the SWC, or you did not import it correctly.

    Do you have an import statement like this?
    Code:
    import de.polygonal.ds.Array2;

  6. #6
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59
    I'm not familiar with SWCs at all, actually. I have copied the code that was on the installation instruction page which is as follows, into my Main.as file (which I have specified in my fla file to be the document class):

    Code:
    package
    {
      import de.polygonal.ds.Array2;
      import flash.display.MovieClip;
      import flash.Boot;
      
      public class Test extends MovieClip
      {
        public function Test() 
        {
          new flash.Boot(this);
    
          var a:Array2 = new Array2(10, 10);
          a.assign(0);
        }
      }
    }
    Perhaps if this doesn't want to cooperate, could you maybe explain your code you gave to me earlier to create arrays? I'm drawing a blank when I look at it to see how I can implement it into what I need so that I can have several arrays like this, which I would populate with data manually:

    timedist[1][400]=26.7;
    timedist[2][400]=27.5;
    timedist[3][400]=28.1;
    timedist[1][800]=54;
    timedist[2][800]=56.3;
    timedist[3][800]=52.1;

    Also, one more question if you don't mind What I would really like to be able to do eventually is to be able to calculate all of these things in PHP and then pass the vars onto Flash. Would I need to go through any extra processes in flash to recognize these multi-dimensional arrays or would it automatically know how to handle them if they were passed to it within the embedding code I'd have on my website?

    I was trying to avoid having to put it on a website to test it initially, but if it would work without having to go through all of these additional steps I may be more inclined to just do it that way.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Okay, to roll your own 2d array, you just have to realize that a 2d array is a 1d array where all the elements are also 1d arrays.
    So, first you need a timedist array:
    Code:
    var timedist:Array = [];
    Now, since there is nothing in timedist[1] yet, you cannot access the 400th element of that nothing. You need to put an array there first.
    Code:
    for (var i:int =0; i < 3; i++){
      timedist[i] = [];  //put a new array as the i'th element of timedist.
    }
    Arrays in AS3 (and most other languages) are naturally 0-based rather than 1-based. The code I have above will initialize timedist[0] through timedist[2] as new empty arrays.

    After it's set up like that, you can just put in your code (adjusted to be 0 based)
    Code:
    timedist[0][400]=26.7;
    timedist[1][400]=27.5;
    timedist[2][400]=28.1;
    timedist[0][800]=54;
    timedist[1][800]=56.3;
    timedist[2][800]=52.1;
    Fortunately for you, arrays in as3 are sparse. That means that you don't have to have the 400 elements that precede timedist[0][400] in order to have that one. In some (most) other languages, arrays are not dynamically sized, and you'd have to have space reserved for the first 400 elements in order to have one at index 400. But that's not a concern here.

    To get data from php to flash, there are 3 main pathways:
    1. flashvars
    2. ExternalInterface
    3. AMFPHP


    Flashvars is the simplest way to set up, but also the most restrictive. By encoding name/value pairs in a parameter to the embedding html tag, you can set variables that flash can access. All variables set up this way will be Strings. If you need other types, you'd have to parse it yourself. Search the forum for 'flashvars' for more details.

    ExternalInterface is pretty easy to set up, and much more flexible. It is a method of communicating from flash to javascript and vice-versa. You can pass variables of different types here, including arrays and multi-dimensional arrays. The communication is turned into xml behind the scenes, and I'm not sure how well it would handle sparse arrays like those you have. It might be very inefficient. If your PHP can output JSON encoded versions of your data, you could use ExternalInterface to get it in the swf.

    AMFPHP is a PHP implementation of the Actionscript Message Format, which is a native object encoding for as3 objects. I've never used it, but if you're comfortable in PHP, you should be able to get it going without TOO much trouble.

  8. #8
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59
    Thank you very much for your help! Your explanations are a big help I will give this all a try and see how it goes. Thanks again!

  9. #9
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59
    Still having some trouble with this. I have tried this code just alone on it's own (as I couldn't pinpoint the issues in the rest of my mess that I have in my fla) and I continue to receive the following error:

    TypeError: Error #1010: A term is undefined and has no properties.

    This is the code I'm using:

    Code:
    var timedist:Array=[];
    for (var i:int =0; i < 3; i++){
      timedist[i] = [];  //put a new array as the i'th element of timedist.
    }
    
    timedist[0][400]=27.8;
    timedist[0][800]=56.4;
    timedist[0][1200]=87.1;
    timedist[1][400]=27.6;
    timedist[1][800]=58.1;
    timedist[1][1200]=92.5;
    timedist[2][400]=29.8;
    timedist[2][800]=58.7;
    timedist[2][1200]=94;
    timedist[3][400]=28.1;
    timedist[3][800]=58;
    timedist[3][1200]=91.6;

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That's because you have 4 indices (0 .. 3), but only initialized the first 3 (0 .. 2)

    Change "i < 3" to "i < 4" to get timedist[3] initialized as well.

  11. #11
    Member
    Join Date
    Jan 2003
    Location
    Ontario
    Posts
    59
    Oops! It works perfectly now. Thank you so much for your help!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center