-
Streamlining complex button links - a challenge
AS 2
I need help streamlining some of my button code, I have buttons that need to perform multiple functions: On rollover, they send their link to the browser's status bar. On roll out they clear the status bar. On press they send tracking code to the html. And on release they send getURL with the link. I've put in some variables so that I don't need to copy the links over and over, but I'd really like to streamline button functions so that I don't need to make a whole new set of onRollover onRollout onPress onRelease functions for each and every link. I'd love any ideas about how to simplify button functions so that they can work for any button. Thanks!
Here's what I'm working with:
Code:
//IMPORT THE JAVASCRIPT EXTERNAL
import flash.external.*;
//THE FUNCTION THAT WILL CALL THE JAVASCRIPT FUNCTION IN THE HEAD OF THE HTML DOC
function jsStatus(link) {
// The name of a JavaScript function to call
//IN QUOTES, IN THIS CASE callStatusBar
var callJasFunction:String = "callStatusBar";
//parameter
//TEMPORARY VAR STORES THE PASSED VARIABLE, EXTERNAL INTERFACE SENDS THE VARIABLE AND THE FUNCTION NAME
var msg:String = link;
var returnValue:String = ExternalInterface.call(callJasFunction, msg).toString();
}
//LINKS LIST
//A VARIABLE MUST BE DECLARED FOR EACH LINK, AND THEN THE ADDRESS STORED IN THAT VAR
var firstLink:String;
var secondLink:String;
firstLink = "http://www.me.com/1";
secondLink = "http://www.me.com/2";
//BUTTON FUNCTIONS
//1
//FIRST LINKS
//CALLS THE STATUS BAR ON ROLLOVER WITH THE LINK STORED ABOVE
this.btn_first.onRollOver = function() {
jsStatus(nowLink);
}
//LINKS TO SEND THE TRACKING FUNCTIONS
this.btn_first.onPress = function() {
getURL("javascript:cmCreateManualLinkClickTag(\"" + nowLink + ",\"FlashTest\")");
trace("javascript:cmCreateManualLinkClickTag(\"" + nowLink + "\",\"FlashTest\")");
}
//LINKS TO THE LINK STORED IN THE VARIABLE
this.btn_first.onRelease = function() {
getURL(firstLink, "_self");
}
//CLEARS THE LINK IN THE STATUS BAR, JSSTATUS CALLED WITH AND EMPTY STRING
this.btn_first.onRollOut = function() {
jsStatus(" ");
}
//END FIRST LINKS
//2
//SECOND LINKS
//CALLS THE STATUS BAR ON ROLLOVER WITH THE LINK STORED ABOVE
this.btn_second.onRollOver = function() {
jsStatus(secondLink);
}
//LINKS TO SEND THE TRACKING FUNCTIONS
this.btn_second.onPress = function() {
getURL("javascript:cmCreateManualLinkClickTag(\"" + secondLink + ",\"FlashTest\")");
trace("javascript:cmCreateManualLinkClickTag(\"" + secondLink + "\",\"FlashTest\")");
}
//LINKS TO THE LINK STORED IN THE VARIABLE
this.btn_second.onRelease = function() {
getURL(secondLink, "_self");
}
//CLEARS THE LINK IN THE STATUS BAR, JSSTATUS CALLED WITH AND EMPTY STRING
this.btn_second.onRollOut = function() {
jsStatus(" ");
}
//END SECOND LINKS
-
Flash/Flex Developer
This is what I would do
Code:
import flash.external.*;
var i:Number;
var buttonMCs:Array = ["button1", "button2", "button3"];
var firstLink:String = "http://www.me.com/1";;
var secondLink:String = "http://www.me.com/2";
function jsStatus(link:String) {
var returnValue:String = ExternalInterface.call("callStatusBar", link).toString();
}
function buildButton(btnObj:MovieClip)
{
btnObj.onRollOver = function() {jsStatus(nowLink);}
btnObj.onRollOut = function() {jsStatus(" ");}
btnObj.onPress = function() {getURL("javascript:cmCreateManualLinkClickTag(\"" + nowLink + ",\"FlashTest\")");}
btnObj.onRelease = function() {getURL(firstLink, "_self");}
}
for(i = 0; i < buttonMCs.length; i++)
{
buildButton(eval(buttonMCs[i]));
//buildButton(this[buttonMCs[i]]); //SOME LIKE THIS...PERSONALLY BOTH WORK FOR ME
}
//FOR THIS TO WORK, YOU NEED TO HAVE THREE BUTTONS ON THE STAGE LABELED 'BUTTON1', 'BUTTON2', and 'BUTTON2', SO MODIFY THE CODE TO WORK FOR YOU MCs.
You'll need to modify this code to work for the elements and count you have. This code adds three buttons which are already on the stage.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
Thanks so much, that's a great solution. I also added another array for the links and passed both the link and the button to the build function.
Code:
for(i = 0; i < buttonMCs.length; i++)
{
buildButton(eval(buttonMCs[i]), urlLinks[i]);
trace(urlLinks[i]);
//buildButton(this[buttonMCs[i]]); //SOME LIKE THIS...PERSONALLY BOTH WORK FOR ME
}
I dropped the eval for the second array. From the flash docs, it looked like eval can only return references to movie clips and objects, and I wanted the links as strings so I could link to them. It seems to be working, but maybe someone who know more about coding standards knows the appropriate way to call strings from an array.
Tags for this Thread
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
|