-
Shortening the code
Trying to reduce the size of this AS3 Code, can someone point me in the right direction. I have multiple http urls, I need a function that determines which button is being clicked and send user to the correct url. Code works fine like this, but I want it shorter.
l1.addEventListener(MouseEvent.CLICK, lClick1);
l2.addEventListener(MouseEvent.CLICK, lClick2);
l3.addEventListener(MouseEvent.CLICK, lClick3);
l4.addEventListener(MouseEvent.CLICK, lClick4);
l5.addEventListener(MouseEvent.CLICK, lClick5);
l6.addEventListener(MouseEvent.CLICK, lClick6);
l7.addEventListener(MouseEvent.CLICK, lClick7);
l8.addEventListener(MouseEvent.CLICK, lClick8);
var link1 = "http://apd.myflorida.com";
var link2 = "http://waiverprovider.com/";
var link3 = "http://ucf-card.org";
var link4 = "http://delmarvafoundation.org/";
var link5 = "http://ahca.myflorida.com/MCHQ/Health_Facility_Regulation/Home_Care";
var link6 = "http://advocacycenter.org";
var link7 = "http://freewebs.com/supportcoordinators/index.htm";
var link8 = "http://fddc.org";
function lClick1 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link1);
navigateToURL(request,"_blank");
}
function lClick2 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link2);
navigateToURL(request,"_blank");
}
function lClick3 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link3);
navigateToURL(request,"_blank");
}
function lClick4 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link4);
navigateToURL(request,"_blank");
}
function lClick5 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link5);
navigateToURL(request,"_blank");
}
function lClick6 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link6);
navigateToURL(request,"_blank");
}
function lClick7 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link7);
navigateToURL(request,"_blank");
}
function lClick8 (event:MouseEvent):void
{
var request:URLRequest = new URLRequest(link8);
navigateToURL(request,"_blank");
}
-
Please wrap your code in [code] or [php] tags.
Code:
var ls:Array = [l1, l2, l3, l4, l5, l6, l7, l8];
var links:Dictionary = new Dictionary();
links[l1] = "http://apd.myflorida.com";
links[l2] = "http://waiverprovider.com/";
links[l3] = "http://ucf-card.org";
links[l4] = "http://delmarvafoundation.org/";
links[l5] = "http://ahca.myflorida.com/MCHQ/Health_Facility_Regulation/Home_Care";
links[l6] = "http://advocacycenter.org";
links[l7] = "http://freewebs.com/supportcoordinators/index.htm";
links[l8] = "http://fddc.org";
for (var i:int = 0; i < ls.length; i++){
ls[i].addEventListener(MouseEvent.CLICK, lClick);
}
function lClick(event:MouseEvent):void{
var request:URLRequest = new URLRequest(links[event.currentTarget]);
navigateToURL(request,"_blank");
}
Note that if your l1..l8 objects are MovieClips or other dynamic classes, you could set the destination property directly on each rather than building a separate Dictionary. Also, if they were an instance of a custom class, you could define that property and do the same thing.
-
wow
I don't fully understand, but I will study the code u put
Thanks
-
Dictionary is a class which allows you to associate arbitrary objects with other objects. In this case, I used the l1..l8 objects as keys into the dictionary, mapped to the strings for their respective URLs.
event.currentTarget is the object which is currently processing the event. It is always the object that the listener was added to. In this case, it will be the l object which was clicked. We take that object and look it up in the dictionary, which gives us the url associated with that object.
-
ahhhhh, understood
So much learn
Thanks bro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|