# Global Variable?

**URL:** <https://forum.kirupa.com/t/global-variable/264461>\
**Category:** flash\
**Created:** [June 26, 2008, 11:10am UTC](https://forum.kirupa.com/t/global-variable/264461 "2008-06-26T11:10:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thedaveyb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thedaveyb](https://forum.kirupa.com/u/thedaveyb)\
**Post date:** [June 26, 2008, 11:10am UTC](https://forum.kirupa.com/t/global-variable/264461/1 "2008-06-26T11:10:12Z")

</div>

Hey kirupians, really struggling with this - have got so far - think have cracked it and SLAM brick wall… _groan_…

I have an array holding ‘image’ and ‘name’

name loops fine and populates a load of movie clips underneath each other…  
i need for when this name is clicked to load an image in another movie clip (image name comes from the array)…

the code i have works but it only pulls throught the last item of the array which points to a problem with ‘i’ not being carried across the functions…?

that is the result of my investigation _smokes pipe_ - any help would be appreciated!.

```php

function loadceleb() {
 for (i=0; i<total; i++) {
  this.contentMain.attachMovie("chold", "hold"+i, i, {_x:xPos, _y:yPos});
  yPos += this.contentMain["hold"+i]._height+5;
  this.contentMain["hold"+i].text_hold = clname*;
 
// at this point textboxes are populated and set up on the stage

  imagename = image*;
 
// tried tying imagename to the image array so i can then use in the function below

  this.contentMain["hold"+i].onPress = function() {
   _root.image_cont.gotoAndPlay(109);
   this.celeb.img.loadMovie(imagename);
 
// this works fine but only pulls through the last image in the array - argh!

   trace(imagename);
   _root.image_cont.celeb.lineani.gotoAndPlay(1);
  };
 }
}
stop();

```

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 26, 2008, 11:43am UTC](https://forum.kirupa.com/t/global-variable/264461/2 "2008-06-26T11:43:54Z")

</div>

for some stupid reason I feel like helping you

search the forums! 😛

---

<div class="post-metadata">

**Author:** ![thedaveyb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thedaveyb](https://forum.kirupa.com/u/thedaveyb)\
**Post date:** [June 26, 2008, 11:50am UTC](https://forum.kirupa.com/t/global-variable/264461/3 "2008-06-26T11:50:45Z")

</div>

i have… and the site … and the net… hence the forum post -

ive tried defining ‘i’ before any function so its always there - no joy

have tried splitting the same then splitting it into two functions - still no joy…

its not like im not trying…:asleep:

---

<div class="post-metadata">

**Author:** ![thedaveyb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thedaveyb](https://forum.kirupa.com/u/thedaveyb)\
**Post date:** [June 26, 2008, 12:05pm UTC](https://forum.kirupa.com/t/global-variable/264461/4 "2008-06-26T12:05:13Z")

</div>

this is the version that works perfectly expects only pulls the last image from the array

```php

unction loadceleb() {
	for (i=0; i<total; i++) {
		this.contentMain.attachMovie("chold", "hold"+i, i, {_x:xPos, _y:yPos});
		yPos += this.contentMain["hold"+i]._height+5;
		this.contentMain["hold"+i].text_hold = clname*;
		imagename = image*;
		this.contentMain["hold"+i].onPress = function() {
			changeImage();
		};
	}
}
function changeImage() {
	_root.image_cont.gotoAndPlay(109);
	this.celeb.img.loadMovie(imagename, 1);
	trace(imagename);
	_root.image_cont.celeb.lineani.gotoAndPlay(1);
}
stop();

```

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 26, 2008, 12:06pm UTC](https://forum.kirupa.com/t/global-variable/264461/5 "2008-06-26T12:06:33Z")

</div>

so you are saying you have an array of buttons, incremented with i

but they are only picking up the last array\* element…

* * *

yerh, you’ve looked real hard for that one

---

<div class="post-metadata">

**Author:** ![thedaveyb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thedaveyb](https://forum.kirupa.com/u/thedaveyb)\
**Post date:** [June 26, 2008, 12:24pm UTC](https://forum.kirupa.com/t/global-variable/264461/6 "2008-06-26T12:24:26Z")

</div>

only the image side of things, the buttons and text work perfectly - which makes me think its an error on my part on how im trying to get at the image array…

if i trace the whole array trace (image); it works fine…

---

<div class="post-metadata">

**Author:** ![pantas](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pantas](https://forum.kirupa.com/u/pantas)\
**Post date:** [June 26, 2008, 1:53pm UTC](https://forum.kirupa.com/t/global-variable/264461/7 "2008-06-26T13:53:58Z")

</div>

t is loading the last image because the var imagename should be inside each movieclip.

when you say image name = 1 and the the cicle runs trhough, the image name will become the last one. and all btns are going to get that last name.

you should create vars inside the dynamic movieclips…

something like this:

function loadceleb() {  
for (i=0; i\<total; i++) {  
[COLOR=“Red”]var BTN:MovieClip[/COLOR] = this.contentMain.attachMovie(“chold”, “hold”+i, i, {\_x:xPos, \_y:yPos});  
yPos += this.contentMain[“hold”+i].\_height+5;  
this.contentMain[“hold”+i].text\_hold = clname\*;  
[COLOR=“red”]BTN[/COLOR].imagename = image\*;  
[COLOR=“Red”]BTN[/COLOR].onPress = function() {  
changeImage([COLOR=“red”]this.imagename[/COLOR]);  
};  
}  
}  
changeImage = function ([COLOR=“red”]Link[/COLOR]) {  
\_root.image\_cont.gotoAndPlay(109);  
this.celeb.img.loadMovie([COLOR=“red”]Link[/COLOR], 1);  
trace([COLOR=“red”]Link[/COLOR]);  
\_root.image\_cont.celeb.lineani.gotoAndPlay(1);  
}

hope this helps…  
any question…just post it

cheers

---

<div class="post-metadata">

**Author:** ![randomagain](https://avatars.discourse-cdn.com/v4/letter/r/35a633/32.png) [@randomagain](https://forum.kirupa.com/u/randomagain)\
**Post date:** [June 26, 2008, 9:57pm UTC](https://forum.kirupa.com/t/global-variable/264461/8 "2008-06-26T21:57:20Z")

</div>

he’ll be back soon enough 😛

then again I don’t get your example loll

---

<div class="post-metadata">

**Author:** ![thedaveyb](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@thedaveyb](https://forum.kirupa.com/u/thedaveyb)\
**Post date:** [June 27, 2008, 8:14am UTC](https://forum.kirupa.com/t/global-variable/264461/9 "2008-06-27T08:14:44Z")

</div>

no its ok, i sorted it…

it was \_global.i i needed to declare above my functions then use this throughout as opposed to just ‘i’ that way it holds the value of ‘i’ throughout the functions and picks the right image from the array when clicking on the button…

oh im good _wiggles_
