# Global Variables

**URL:** <https://forum.kirupa.com/t/global-variables/4981>\
**Category:** flash\
**Created:** [August 27, 2002, 5:57pm UTC](https://forum.kirupa.com/t/global-variables/4981 "2002-08-27T17:57:40Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![sintax321](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@sintax321](https://forum.kirupa.com/u/sintax321)\
**Post date:** [August 27, 2002, 5:57pm UTC](https://forum.kirupa.com/t/global-variables/4981/1 "2002-08-27T17:57:40Z")

</div>

I have been trying to set up and use global variables  
and I just can’t see to get it to work. I have a  
scene with 2 layers  
in the first frame of the first layer I have this  
action script:

var ta1;  
\_global.ta1;  
ta1 = 0;

On the second layer I have an MC called “tabtop” with  
the instance name “top”. Inside this movie clip I have  
I have a MC named “tab1motion” with an inscatnce name  
of “tab1”. I have this MC tweened to move in and out a  
certain distance on the stage. In side of the “tab1motion”  
MC I have my graphic and a button. The button  
has an action script of:

on (rollOver) {  
if (ta1 == 0) {  
tellTarget ("/top") {  
gotoAndPlay (“rollstart”);  
}  
}  
}

If I comment out the if statment the animation works  
but if I try to use the variable if dosn’t do a thing.  
I’am using the variable because I would like to have  
4 actions on this button. A rollOver, rollOut, and 2  
differnt onRelease actions using the “if statments”  
and the global variable. I know I’am probably way off  
with the code but any help anyone can give would be  
great.(p.s. I’m using flash 5)  
:-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 1:08am UTC](https://forum.kirupa.com/t/global-variables/4981/2 "2002-08-28T01:08:46Z")

</div>

in the first frame, just:

\_global.ta1 = 0;

i think your creating a local ta1 and a \_global.ta1, then setting the local ta1 to 0.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 10:10am UTC](https://forum.kirupa.com/t/global-variables/4981/3 "2002-08-28T10:10:14Z")

</div>

[http://www.kirupa.com/developer/actionscript/as\_tricks.asp](http://www.kirupa.com/developer/actionscript/as_tricks.asp)  
Check the global variables article.

pom :asian:

And Supra, are you also the sbeener at layer51?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 1:56pm UTC](https://forum.kirupa.com/t/global-variables/4981/4 "2002-08-28T13:56:43Z")

</div>

So if I want to manipulate the global variable I have to put \_global. in fornt of it.

on (rollOver) {  
if (\_global.ta1 == 0) {  
tellTarget ("/top") {  
gotoAndPlay (“rollstart”);  
}  
}  
}  
I tried that but still my action script does nothing when I rollOver. I have attached the FLA if any one would like to take a look at it and see what the can figure out with it.  
:-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 2:04pm UTC](https://forum.kirupa.com/t/global-variables/4981/5 "2002-08-28T14:04:17Z")

</div>

```auto
on(rollOver){
	if(ta1 == 0){
		_root.top.gotoAndPlay('rollstart')
	}
}

```

you have to put this code on the button that you want to rollOver, but it works for me.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 2:58pm UTC](https://forum.kirupa.com/t/global-variables/4981/6 "2002-08-28T14:58:49Z")

</div>

How come tellTarget dosen’t work? Isn’t the same? Does the “If” statment not work with a tellTarget becasue the tellTarget works until the “If” is added?

I’m so confused:(

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 4:42pm UTC](https://forum.kirupa.com/t/global-variables/4981/7 "2002-08-28T16:42:40Z")

</div>

to set a global variable, you must use \_global, thereafter you don’t need to use \_global (that’s kinda the point).

however, there is (as always) an exception. flash will still look in a local namespace first, so if you’ve created a variable by the same in the local space, you’ll have to use \_global if you want the global variable because otherwise flash will default to the local one.

make sense?

ily - yup. i guess there can’t be too many sbeeners out there, can there? those prototypes were written ages ago while i was working on the depth sorting for that 3d app.

the regular sort is pretty useless, but i can’t delete it. the sort a multi dimensional array by a give index is much handier. it’s faster using utilising the built in sort() method, but mine will sort 5 as lower than 15, where sort() just uses the first character. can be handy.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 5:10pm UTC](https://forum.kirupa.com/t/global-variables/4981/8 "2002-08-28T17:10:04Z")

</div>

don’t use tellTarget. tellTarget and the use of “/” are old syntax from the Flash 4 days. Now Flash uses the dot-syntax method.

\_root.gotoAndPlay(“whatever”)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 7:37pm UTC](https://forum.kirupa.com/t/global-variables/4981/9 "2002-08-28T19:37:56Z")

</div>

Thanks for your help guys. I’m going to try this new information as soon as I get off work. I’ll post the results.

![](http://www.kirupa.com/forums/smileys_files/bigthumb.gif)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 28, 2002, 11:55pm UTC](https://forum.kirupa.com/t/global-variables/4981/10 "2002-08-28T23:55:32Z")

</div>

Still can’t get anything to happen.

I tried:  
on (rollOver) {  
if (\_root.ta1 == 0) {  
\_root.top.gotoAndPlay(“rollstart”);  
}  
}

Also tried:  
on (rollOver) {  
if (\_global.ta1 == 0) {  
\_root.top.gotoAndPlay(“rollstart”);  
}  
}

And  
on (rollOver) {  
if (ta1 == 0) {  
\_root.top.gotoAndPlay(“rollstart”);  
}  
}

Jubba you said that you had the button working I think. Could you Post that FLA so I could take a look.☹

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 12:00am UTC](https://forum.kirupa.com/t/global-variables/4981/11 "2002-08-29T00:00:27Z")

</div>

how are you defining your variable?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 12:31am UTC](https://forum.kirupa.com/t/global-variables/4981/12 "2002-08-29T00:31:07Z")

</div>

in the first frame of your animation you should have

\_global.ta1 = 0

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 12:50am UTC](https://forum.kirupa.com/t/global-variables/4981/13 "2002-08-29T00:50:41Z")

</div>

Yep thats what I have. I have even tried putting the global variable in the MC action with a onload event still nothing. Here is the current FLA take a look if you want to check it out.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 3:16am UTC](https://forum.kirupa.com/t/global-variables/4981/14 "2002-08-29T03:16:42Z")

</div>

that file worked. the MovieClip ‘top’ went to and Played “rollstart”

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 7:54am UTC](https://forum.kirupa.com/t/global-variables/4981/15 "2002-08-29T07:54:18Z")

</div>

I’m not sure, Supra.

> \*Originally posted by sbeener \*  
> \*\*to set a global variable, you must use \_global, thereafter you don’t need to use \_global (that’s kinda the point).

however, there is (as always) an exception. flash will still look in a local namespace first, so if you’ve created a variable by the same in the local space, you’ll have to use \_global if you want the global variable because otherwise flash will default to the local one.

make sense?  
\*\*  
You don’t have to use \_global to **access** the variable, but if you want to modify it, you have to use it. Otherwise Flash will create a new variable in the local space that will hide the global variable. At least, that’s what my tests showed me.

And about the prototypes, I’ll try and read them when I get the chance 😛 .

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 2:21pm UTC](https://forum.kirupa.com/t/global-variables/4981/16 "2002-08-29T14:21:21Z")

</div>

Jubba that exact file that I posted worked for you? If it works for you without changing it is there a reason you can think of why it won’t run for me? I’m using that exact FLA and when I rollOver the button nothing happens. I’m running flash 5 will this script only work in MX?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 7:52pm UTC](https://forum.kirupa.com/t/global-variables/4981/17 "2002-08-29T19:52:54Z")

</div>

yeah, \_global is mx only.

sheesh… how long did that take to work out?

ily - good point about modifying \_globals.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 29, 2002, 8:31pm UTC](https://forum.kirupa.com/t/global-variables/4981/18 "2002-08-29T20:31:07Z")

</div>

Supra: took too long, I actually thought I asked that in one of my first posts, but I guess not.

Sin: yes, \_global is only MX

Pom: 😛
