A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: declare variables in for loop

  1. #1
    Senior Member
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    326

    declare variables in for loop

    This works and traces '2' five times:

    Code:
    for (var i = 0; i < 5; i++)
    {
    var d:int = 2;
    trace(d);
    }
    I sort of understand why this doesn't:

    Code:
    var d:int = 2;
    trace(d);
    var d:int = 2;
    trace(d);
    var d:int = 2;
    trace(d);
    var d:int = 2;
    trace(d);
    var d:int = 2;
    trace(d);
    Since I get the next error:
    1151: A conflict exists with definition d in namespace internal
    cause I declare the same variable five times.

    The thing I don't understand is why it does work in the for-loop, since in principle I'm doing the same thing there. Why isn't it a problem for actionscript when I let the same variable be declared over and over again when I use a for-loop?

    Illustration | Animation | Web Banners | Graphic Design
    Ducklord Studio

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I personally create a map and create dynamic variables or push all values into an array like this

    PHP Code:
    var0="value1";
    var1="value2";
    arr0:Array=[var0,var1];
    for(var 
    i:int=0;i<2;i++){
    trace(arr0[i]);
    }
    ouputvalue0 value1

    or
    map0:Object ={};
    for(var 
    i:int=0;i<2;i++){
    map0['var'+i]="value"+i.toString();
    trace(map0['var'+i]);
    }

    ouputvalue0 value1 
    ~calmchess~

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