A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] My array gets reset

  1. #1
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    resolved [RESOLVED] My array gets reset

    This is probably a very newbie question
    Here is my script. Why does groupArray gets reset each time the function addTest is called?
    I want it not to reset.

    Code:
    var groupArray:Array = new Array([],[]);
    stage.addEventListener(MouseEvent.MOUSE_DOWN, addTest);
    function addTest(event:MouseEvent) {
    	groupSelected = 0;
    	groupArray[groupSelected].push("item");
    	groupArray[groupSelected].push("item2");
    	trace("New lenght: "+groupArray[groupSelected].length); // returns 2 each time addTest is called
    }
    returns 2 each time addTest is called


    Similar I could have:
    Code:
    var testVariable:Boolean = false;
    stage.addEventListener(MouseEvent.MOUSE_DOWN, addTest);
    function addTest(event:MouseEvent) {
    	trace (testVariable); // returns false each time function is called
    	testVariable = true
    }
    returns false each time function is called

    But if I say:
    Code:
    var testVariable:Boolean;
    stage.addEventListener(MouseEvent.MOUSE_DOWN, addTest);
    function addTest(event:MouseEvent) {
    	trace (testVariable); // returns false first time, but then returns true
    	testVariable = true;
    }
    returns false first time, then true.


    How do I declare an multidimensional array that does not get reset? Sorry for long explanation.
    Last edited by somlemeg; 06-15-2012 at 03:41 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Put a trace statement outside your function. It looks like your frame script is being run multiple times.

  3. #3
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Yes it is an frame script.
    Copied the trace outside the function, now it works. What a strange behavior!
    Is this another reason why one should "never" use framescripts, but classes instead?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That's... Not what I said. I wanted you to put another trace, outside the function, to see if it got executed more than once. If it did, then your frame script was being executed more than once, and it would reset your variables each time.

  5. #5
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    resolved

    I restarted flash, and now it seems to give a more reliable response.

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