Creating a function with a click event and a variable passed.

In actionscript 2 you could easily pass 2 variables to a function, even if one was a click event.

In actionscript 3 now when you call a function from a click event I can only figure out how to get the click event passed but no additional variables.

So in a nutshell I want to write a function that will accept events and variables.
//  Actionscript 3  - i want to pass the link variable to this function
 
 
function clicker( e:MouseEvent ):void {
	try {
		navigateToURL(new URLRequest(link), '_self'); // second argument is target
	} catch (e:Error) {
  		trace("Error occurred!");
	}
}

Open in new window

FredericGAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

julianopolitoCommented:
you can't do that, because the component who generates the event calls a function with only one parameter in it's signature.

though you can make your listener function call another function, wich usually is the case:

function clicker( e:MouseEvent ):void {
         openURL("http://www.gmail.com","_self");
}
function openURL(link:String, target:String):void{
           try {
      navigateToURL(new URLRequest(link), target);
            } catch (e:Error) {
        trace("Error occurred!");
            }
}


From the click event it is not possible to pass arguments. Even in AS 2 you can't pass a direct argument through a native onPress event. If this is not what you want, explain what you used to do in AS 2, so I can "translate " it into AS 3, ok?
0
FredericGAuthor Commented:
Okay, so here is what I am trying to do.


If you see the example below of what I am using It works at calling all four separate links through the use of 4 separate function calls for navigateToURL.

What I want to have is a more  efficient function where i would only have 1 function call to access a link which has a link variable attached to it instead of 4 separate ones.  Bu i woul like to be able to trigger this function with a mouse event.  

I may hav been constrewing things with the AS2 reference, but i have been able to do this before in as2,  by using  the function example after the as3 one

/////  Current actionscript 3 function
 
// Initial Vars
 
var link1:String =  "http://www.digg.com";
var link2:String =  "http://www.digg.com";
var link3:String =  "http://www.digg.com";
var link4:String =  "http://www.digg.com";
 
 
/// Main Buttons Setup
 
function butSetup(but, func):void{
	but.buttonMode = true;
	but.addEventListener(MouseEvent.CLICK, func);
	but.addEventListener(MouseEvent.MOUSE_OVER, uptween);
	but.addEventListener(MouseEvent.MOUSE_OUT, downtween);
	function uptween(event:MouseEvent){
		Tweener.addTween(but.butBase.hl, {alpha:0, time:.5});
	}
	function downtween(event:MouseEvent){
		Tweener.addTween(but.butBase.hl, {alpha:100, time:1});
	}
}
 
butSetup(main.but_projets, linkClick1);
butSetup(main.but_espaces, linkClick2);
butSetup(main.but_champlain, linkClick3);
butSetup(main.but_dieppe, linkClick4);
 
 
function linkClick1( e:MouseEvent ):void {
	try {
		navigateToURL(new URLRequest(link1), '_self'); // second argument is target
	} catch (e:Error) {
  		trace("Error occurred!");
	}
}
 
function linkClick2( e:MouseEvent ):void {
	try {
		navigateToURL(new URLRequest(link2), '_self'); // second argument is target
	} catch (e:Error) {
  		trace("Error occurred!");
	}
}
 
function linkClick3( e:MouseEvent ):void {
	try {
		navigateToURL(new URLRequest(link3), '_self'); // second argument is target
	} catch (e:Error) {
  		trace("Error occurred!");
	}
}
 
function linkClick4( e:MouseEvent ):void {
	try {
		navigateToURL(new URLRequest(link4), '_self'); // second argument is target
	} catch (e:Error) {
  		trace("Error occurred!");
	}
}
 
 
/////// Previous actionscript 2 method
 
 
function butter(but, butanim, butscene, buttext){
	
	but.onRollOver =  function(){
		butanim.gotoAndPlay("anim");
		var butxTween:Tween = new Tween(but, "_xscale", Strong.easeOut, but._xscale, 130, 1, true);
		var butyTween:Tween = new Tween(but, "_yscale", Strong.easeOut, but._yscale, 130, 1, true);
		var buttextTween:Tween = new Tween(buttext, "_alpha", Strong.easeOut, buttext._alpha, 100, 1.5, true);
	}
	
	
	but.onRollOut =  function(){
		butanim.gotoAndPlay("noanim");
		var butxTween:Tween = new Tween(but, "_xscale", Strong.easeOut, but._xscale, 100, 1, true);
		var butyTween:Tween = new Tween(but, "_yscale", Strong.easeOut, but._yscale, 100, 1, true);
		var buttextTween:Tween = new Tween(buttext, "_alpha", Strong.easeOut, buttext._alpha, 0, 1.5, true);
	}
	
	but.onPress =  function(){
		if (_root.main.icons.intro_anim._visible == true){_root.main.icons.intro_anim._visible = false;};
		main.scene_container.gotoAndStop(butscene);
	}
 
}
 
 
butter(main.icons.icon_stomach,main.icons.icon_stomach.icon_stomach_anim, "stomach", main.icons.icon_text_eating);

Open in new window

0
julianopolitoCommented:
Well, first in your AS 2 code, the mouse events are not receiving arguments:
but.onPress =  function(){

What you need to do is modifying the button itself so it can keep an extra info. Is your button a custom movieclip or component? I'll give you an example of how to create a custom button component to have an extra info, and then you can use it with a single listener function.

/****** First create the component class extending fl.controls.Button. *****/
//MyButton.as - put in the same folder as the .fla
package{
      import fl.controls.Button;
      public class MyButton extends Button{
            public function MyButton(){
                  public var link:String;//here is the new link property
            }
      }
}

/********* Now drag a button component to the stage
/********* Right click the button component in the library and choose Linkage
/********* In the Class field change fl.controls.Button to MyButton
/********* Now we are saying that the button is actually a MyButton, which does all as button, but has one more property - link


/********* Now let's go to your code in the .fla:

/////  Current actionscript 3 function
 
// Initial Vars
 
var link1:String =  "http://www.digg.com";
var link2:String =  "http://www.google.com";
var link3:String =  "http://www.yahoo.com";
var link4:String =  "http://www.youtube.com";
 
 
/// Main Buttons Setup
 
function butSetup(but, link):void{
      but.buttonMode = true;
      //Here we set the new link property
      but.link = link;
      //all buttons use the same listener function - linkClick
      but.addEventListener(MouseEvent.CLICK, linkClick);
      but.addEventListener(MouseEvent.MOUSE_OVER, uptween);
      but.addEventListener(MouseEvent.MOUSE_OUT, downtween);
      function uptween(event:MouseEvent){
            Tweener.addTween(but.butBase.hl, {alpha:0, time:.5});
      }
      function downtween(event:MouseEvent){
            Tweener.addTween(but.butBase.hl, {alpha:100, time:1});
      }
}
 //Setup your buttons, but I've changed the method signature
butSetup(main.but_projets, link1);
butSetup(main.but_espaces, link2);
butSetup(main.but_champlain, link3);
butSetup(main.but_dieppe, link4);
 
 
function linkClick( e:MouseEvent ):void {
      //e.target is the clicked button. As it is an instance of MyButton it contains a property called link,
      //which holds the target link
      var but:MyButton = MyButton(e.target);
      try {
            //We can then use it here - but.link
            navigateToURL(new URLRequest(but.link), '_self'); // second argument is target
      } catch (e:Error) {
              trace("Error occurred!");
      }
}


I'm sending you a sample code and .fla - change the extension from .pdf, to .zip ok?
Flash CS3


mybutton.zip.pdf
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

FredericGAuthor Commented:
This is exactly what i was looking for.  I am using custom buttons. Im assuming i would just define the link var within the buttons then instead of extending the component.
0
julianopolitoCommented:
you can if you buttons are dynamic. if the button class you are using is not dynamic you cannot create a new property runtime, then you need to extend the component.
0
FredericGAuthor Commented:
I have Opened a new Question regarding the extending of the MovieClip component for this at:

http://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/Q_23122754.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.