A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: my class can't access an array on the timeline

  1. #1
    Senior Member
    Join Date
    Jun 2001
    Posts
    104

    my class can't access an array on the timeline

    Well, I am worn out.. have been working on this for two days and don't know where to turn. I am new to as3, but was pretty good with as2.

    I have a multidimensional array on the main timeline. Its name is myInst. I need to access this array from this class to build a scrolling list of swfs which the path to the swfs is in the myInst array. I have not included the entire code/class because the issue is somewhere in this part. The error is 1009, cannot access a property or method of a null object reference. This fla is not going to be the root fla, it is going to be external, so the use of root probably will not work, but I tried it anyway, to no avail..

    This is not the complete class, but thought it would be enough for someone with more as3 experience could see my problem.. thanks for any help.

    package {
    import flash.display.MovieClip;
    import flash.net.*;
    import flash.events.Event;
    import flash.xml.XMLDocument;
    import flash.display.Loader;
    import flash.errors.IOError;
    import flash.events.IOErrorEvent;
    import flash.geom.Point;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.display.DisplayObject;

    public class BuildThumbPics extends MovieClip {
    private var loader:URLLoader = new URLLoader();
    private var cItems:Array = new Array();
    private var loaders:Array = new Array();
    private var clip:Sprite = new Sprite;
    //var currentItem:myNewItem;
    private var c:int=0;
    //var currentItem:CarouselItem;
    private var myx:Number=21;
    private var myy:Number=15;

    public function BuildThumbPics(){
    //trace("Trying to find the myInst array " + MovieClip(root).myInst[k][5]);
    trace("build thumb pics function has been called " + myx);
    this.addItemsToScreen();
    //thumbpics.addEventListener(Event.ADDED_TO_STAGE, addItemsToScreen);
    }

    public function addItemsToScreen():void {
    trace("add items got called ");
    //for (var k:int = 0; k < MovieClip(parent).myInst.length; k++) {
    for (var k:int = 0; k < 1; k++) {
    loaders[k] = new Loader();
    loaders[k].contentLoaderInfo.addEventListener(Event.COMPLETE , onItemImageLoaded);
    trace("Trying to find the myInst array " + MovieClip(parent).myInst[k][5]);
    var mfile:String=MovieClip(root).myInst[k][5];
    loaders[k].load(new URLRequest(mfile));
    trace("addingItemsToScreen function - loading " + MovieClip(parent).myInst[k][5]);
    }
    }
    Jim Booth

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    I'm fairly new to AS3 too (but trying to learn... by answering questions on here, no less). I ran into the same problem you did when I tried it out. Closest I got was in using Stage.myInst, but that errored with a "possibly undefined property" error... which makes sense, but is annoying. Stage is a static variable.

    It may just be that variables on the timeline are out of scope for class instances. I hope I'm wrong (like I said... I'm new to AS3 also).

    The workaround would be to pass the array into the class when it's created... just change the constructor to require it.

  3. #3
    Senior Member
    Join Date
    Jun 2001
    Posts
    104
    Thanks Marshdabeachy.. i may give that a go.. in the mean time, I thought I would pass along some more info. The line of code that it is erroring on is:

    var mfile:String=MovieClip(root).myInst[k][5];

    The reason I feel this way is all the traces above it work (with the exception of the one directly above it. This is why I feel it doesn't like the Movieclip(root).myInst[k][5];

    BTW, I am initiating the class with this code from the timeline:

    BuildThumbPics:BuildThumbPics=new BuildThumbPics;

    That seems to work.. i tried calling the function "BuildThumbPics()" but that didn't seem to work.

    jim
    Jim Booth

  4. #4
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Like I said, passing it into a c'tor will probably do the trick.

    Code:
    public class BuildThumbPics extends MovieClip {
       public function BuildThumbPics(myArr:Array)
       {
          this.thumbArr = myArr;
       }
       
       private var thumbArr:Array;
       // etc
    }
    And then instead of trying to access MovieClip(root).myInst, just access the local thumbArr.

    And on the timeline, instantiate the class with:

    Code:
    BuildThumbPics:BuildThumbPics = new BuildThumbPics(myInst);
    Since I'm new to AS3, I dunno if Flash will pass that by value or reference... if it's by value and you've got a huge nested array going on, that might be slow. But that's probably unlikely.

  5. #5
    Senior Member
    Join Date
    Jun 2001
    Posts
    104
    that is pretty neat idea.. i will give it a go.. thanks for the help.. i like the solution!
    Jim Booth

  6. #6
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Arrays should pass by reference (which is what you want here), anything except a primitive should pass by reference.

    If you don't want to pass by reference, I don't think array has a .clone method to create a copy, but you can clone them just by calling the Array.concat() with no arguments.

  7. #7
    Senior Member
    Join Date
    Jun 2001
    Posts
    104

    Thumbs up

    This worked!!! amazing.. thanks Marshdabeachy and Alluvian! I have to say, I honestly don't think I would have tried this, at least not for many hours.. never occurred to me to send the array to the class.
    Jim Booth

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