A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Issues with simple trace of array

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2

    Issues with simple trace of array

    I'm working through some tutorials from my instructor to eventually build a simple e-com website for a state park. Basically, I'm using the two as3 files shown below and trying to get "bob" in my output with an array function and trace. Load store function is intentionally coded out. I've checked my code with my instructor's and I have no idea why it isn't working. When I test project, nothing comes up in output, and I have no errors.

    Main012.as:

    package {
    import flash.display.MovieClip;

    public class Main012 extends MovieClip {

    public function Main012() {
    //loadStore();
    var tempArray:MovieClip = new TestArray();
    this.addChild( tempArray );
    }

    function loadStore():void {
    var store:MovieClip = new Store();
    this.addChild( store );
    }
    }
    }


    TestArray.as:

    package {

    import flash.display.MovieClip;



    public class TestArray extends MovieClip {
    var myArray = new Array();



    public function TestArray() {
    myArray[0] = "bob";
    trace(myArray);
    }
    }

    }


    Could it be attributed to some other as file in my project? Any advice is greatly appreciated.

    Frustrated,
    -mike

  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Mikki*
    (so stressed I can't remember how to spell my own name :/ )

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Please use [code] tags to format your code.

    I don't see any reason that you wouldn't get a trace with "bob" in it, but I do see several minor problems.

    Unless Main012 has frames or frame script, it doesn't need to extend MovieClip. It could extend Sprite.

    Your tempArray variable could be typed as TestArray rather than MovieClip since you're assigning it a TestArray.

    TestArray is not an Array, so that's a pretty bad name.

    Like Main012, unless TestArray has frames or frame script, it does not need to extend MovieClip. Unlike Main012, you probably don't need it to be a display object at all, so you wouldn't have to extend anything. You would need to remove the addChild line, but since TestArray has no visual appearance, it doesn't really change anything.

    You should declare "public" or "private" before the myArray variable. And you should give it a type. And you should probably instantiate it within the constructor. And it's more efficient to use the empty array literal [] rather than new Array().

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    51
    There is no reason to not getting it work, unless you don't have the asset Store in your library.
    Actionscript Code:
    package {
        import flash.display.MovieClip;
        public class Main012 extends MovieClip {
            public var tempArray:TestArray;
            public function Main012() {
                loadStore();
                tempArray= new TestArray();
                trace(tempArray.myArray.length);
                trace(tempArray.myArray);
                tempArray.myArray.push("foo","lee");
                trace(tempArray.myArray.length);
                trace(tempArray.myArray);
            }
            public function loadStore():void {
                var store:MovieClip = new Store();
                this.addChild(store);
            }
        }
    }

    Actionscript Code:
    package {
        import flash.utils.getQualifiedClassName;
        public class TestArray {
            public var myArray = new Array();
            public function TestArray() {
                myArray[0] = "bob";
                trace("This is from "+getQualifiedClassName(this)+" : "+myArray);
            }
        }
    }



    FFA

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