# Referencing a global var within a function

**URL:** <https://forum.kirupa.com/t/referencing-a-global-var-within-a-function/286679>\
**Category:** flash\
**Created:** [April 19, 2009, 2:28am UTC](https://forum.kirupa.com/t/referencing-a-global-var-within-a-function/286679 "2009-04-19T02:28:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![El\_Viejo](https://avatars.discourse-cdn.com/v4/letter/e/74df32/32.png) [@El\_Viejo](https://forum.kirupa.com/u/El_Viejo)\
**Post date:** [April 19, 2009, 2:28am UTC](https://forum.kirupa.com/t/referencing-a-global-var-within-a-function/286679/1 "2009-04-19T02:28:56Z")

</div>

I’m an old C programmer been away from programming for years. Now we have a project requiring Flash and I’ve been elected. Just installed CS4.

I have 16 panels in 4 sets of 4 that rotate left to right. (Big learning curve on that one - fading transitions in the right order…). Each panel must have a unique url when clicked. (16 urls).

I’ve looked through many solutions, most of which seem unduly complex. I think that the simplest short of going to onRelease() with AS2 (Later I have to add mouseover changes in the graphics) is something like this:

function handleLink( pEvent:MouseEvent ):void  
{ switch(pEvent.target)  
{ case btnLifestyleA1:  
var request:URLRequest = new URLRequest(a1Link);  
break;  
case btnLifestyleA2:  
var request:URLRequest = new URLRequest(a2Link);  
break;  
//etc …  
default:  
var request:URLRequest = new URLRequest(“[http://www.defaulturl.com/](http://www.defaulturl.com/)”);  
break;  
}  
if( pEvent.target == btnLifestyleA1 )  
{ navigateToURL( request, “\_blank”); }  
}  
var a1Link:String = “[http://www.someurl.com/](http://www.someurl.com/)”;  
btnLifestyleA1.addEventListener( MouseEvent.CLICK,handleLink );  
var a2Link:String = “[http://www.anotherurl.com/](http://www.anotherurl.com/)”;  
btnLifestyleA2.addEventListener( MouseEvent.CLICK,handleLink );  
//etc …

I haven’t found this solution out there. Is there a gotcha because the a#Link vars are accessed as global within the scope of handleLink? It doesn’t feel right, but it works fine and 16 global variables doesn’t seem too bad.

Is there a better way?
