# Individual vars in loop?

**URL:** https://forum.kirupa.com/t/individual-vars-in-loop/265539
**Category:** flash
**Created:** [July 10, 2008, 12:53am UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539 "2008-07-10T00:53:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![peartart](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@peartart](https://forum.kirupa.com/u/peartart)
#### Post date: [July 10, 2008, 12:53am UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/1 "2008-07-10T00:53:43Z")

</div>

I’ve created a loop to define onPress functions for a group of movieclips. The code is here:

```auto
var select:String = "none";
var elist:Array = ["dappled", "panda", "red", "blue", "green", "gray", "fire", "earth", "water", "garden"];
for (i=0;i<elist.length;i++) {
    var cur:String = elist*;
    [cur]onPress = function(){
        select = cur;
        trace(select);
    };
}

```

All the mcs now have an onPress function. However, they all define the “select” variable as the last item in the array. Is this because every mc is referencing the new curs? I’m not really sure… Is there a way I can fix this so select is given the correct (unique) value every time?

---

<div class="post-metadata">

### Author: ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)
#### Post date: [July 10, 2008, 5:21pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/2 "2008-07-10T17:21:02Z")

</div>

What exactly do you want ?

---

<div class="post-metadata">

### Author: ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)
#### Post date: [July 10, 2008, 5:36pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/3 "2008-07-10T17:36:13Z")

</div>

very common problem. you have to store a variable on the object of the appropriate id

```auto
var select:String = "none";
var elist:Array = ["dappled", "panda", "red", "blue", "green", "gray", "fire", "earth", "water", "garden"];
for (i=0;i<elist.length;i++) {
    var cur:MovieClip = this[elist*];
    cur.id = i;
    cur.onPress = function(){
        select = cur;
        trace(elist[this.id]);
    };
}

```

---

<div class="post-metadata">

### Author: ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)
#### Post date: [July 10, 2008, 6:13pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/4 "2008-07-10T18:13:08Z")

</div>

well your this code seems to be workign fine i guess

[quote=nathan99;2356339]very common problem. you have to store a variable on the object of the appropriate id ActionScript Code:  
[LEFT][COLOR=#000000] **var** [/COLOR] select:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“none”[/COLOR];  
[COLOR=#000000] **var** [/COLOR] elist:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#FF0000]“dappled”[/COLOR], [COLOR=#FF0000]“panda”[/COLOR], [COLOR=#FF0000]“red”[/COLOR], [COLOR=#FF0000]“blue”[/COLOR], [COLOR=#FF0000]“green”[/COLOR], [COLOR=#FF0000]“gray”[/COLOR], [COLOR=#FF0000]“fire”[/COLOR], [COLOR=#FF0000]“earth”[/COLOR], [COLOR=#FF0000]“water”[/COLOR], [COLOR=#FF0000]“garden”[/COLOR][COLOR=#000000]][/COLOR];  
[COLOR=#0000FF]for[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] cur:[COLOR=#0000FF]MovieClip[/COLOR] = [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR]elist[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR][COLOR=#000000]][/COLOR];  
cur.[COLOR=#000080]id[/COLOR] = i;  
cur.[COLOR=#0000FF]onPress[/COLOR] = [COLOR=#000000] **function** [/COLOR]COLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
select = cur;  
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]

[/quote]

---

<div class="post-metadata">

### Author: ![peartart](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@peartart](https://forum.kirupa.com/u/peartart)
#### Post date: [July 10, 2008, 9:22pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/5 "2008-07-10T21:22:47Z")

</div>

hmmm… That seems more logical but it’s returning an error for the select = cur line saying “type mismatch: movieclip where string is required” basically… I tried changing select to a movieclip just as a test but then the loop only executed once…

---

<div class="post-metadata">

### Author: ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)
#### Post date: [July 10, 2008, 10:04pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/6 "2008-07-10T22:04:41Z")

</div>

Actually i think it will be select = cur.\_name;

---

<div class="post-metadata">

### Author: ![peartart](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@peartart](https://forum.kirupa.com/u/peartart)
#### Post date: [July 10, 2008, 10:16pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/7 "2008-07-10T22:16:35Z")

</div>

That gets rid of the syntax error, but the onPress function is only defined for the first movie clip (dappled) now.

---

<div class="post-metadata">

### Author: ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)
#### Post date: [July 10, 2008, 10:18pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/8 "2008-07-10T22:18:55Z")

</div>

create 3 movieclips on stage and name them “dappled”, “panda”, “red” then see the function works on all three clips and traces their names

---

<div class="post-metadata">

### Author: ![peartart](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@peartart](https://forum.kirupa.com/u/peartart)
#### Post date: [July 10, 2008, 10:52pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/9 "2008-07-10T22:52:39Z")

</div>

Alright, that works. thanks!

---

<div class="post-metadata">

### Author: ![sparkdemon](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sparkdemon](https://forum.kirupa.com/u/sparkdemon)
#### Post date: [July 10, 2008, 11:03pm UTC](https://forum.kirupa.com/t/individual-vars-in-loop/265539/10 "2008-07-10T23:03:47Z")

</div>

welcome 🙂

> [@peartart;2356546](#):
>
> Alright, that works. thanks!
