Skip to main content
Participating Frequently
September 5, 2009
Question

function question

  • September 5, 2009
  • 1 reply
  • 986 views

Hi,

I'm kind of learning action script by myself and there are some things about functions  that are still dark to me. My question is how do i pass a parametre  to a function through an event listener.

i've got this array

var tablTitre:Array = new Array("Spirale //2009", "Stresss //2009", "Eye Candy //2009", "La face du vice //2008", "Le Coeur //2009", "Worse than Agony Logo //2009", "La ville du future //2009");

i've got a few of these:


this.con.img1.addEventListener(MouseEvent.CLICK, texte);    // i'd like to know how to pass the parametre here

that call this function

function texte(e:Event):void     //and to know the syntax to use the parametre
    {
        //titre=true;
        var choisi:MovieClip = MovieClip(e.currentTarget);
        if (affichage==false)
            {
               
                this.titre.txt_titre.text=tablTitre[1];       //i'd like to use the parametre here to indicate what element of the array to show
                affichage==true;
            }


     if (affichage==true)
            {
                this.titre.txt_titre.text="choisir un oeuvre";
                affichage=false;
            }
     }
    }

It's quite simple really i just need some syntax help. I realy dont get the (e:Event):void thing.

thanks in advance for those generous souls!

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
September 5, 2009

You'll do yourself a favor by avoiding the color scheme you picked.... italic orange on blue makes for a hard read.  In any case, as far as I know, you cannot pass parameters to event handler functions... I have seen people show code they say does, but it's usually shot down from my recollection.  What parameter is it that you want to pass?  IF it is some property of the object that was clicked, then it should be available via the "e" argument that the function receives.  'e.currentTarget' is the object that has the event listener assigned to it.

The e:Event thing is simply an argument sent by the listener.... e could have been called anything, but when you specify an argument to a function you have to specify the Class, which in this case would actually be MouseEvent, rather than Event.  The :void part is an indication of the class of object that the function returns.  In many cases a function will not return anything, which is why :void is used.  So if you had a function that calculated and returned an integer value, then the :void would instead be :int

var someInt:int = someFunction(arg);

function someFunction(arg:int):int {

     return arg + 3;

}

MikesNameIsMike
Inspiring
September 6, 2009

I'm probably going to confuse you here, but it is possible to pass your own paramets to a method through an event.

Everything Ned sid is absolutly right, BUT you can acomplish what you are trying to do by creating your out event class, whos constructor should accept a parameter that you specify, and save it as a class variable.  Then you manualy dispatch this event with the argument you want, and it will be available to you as a property of the event within the function that your listeners calls when it gets triggered. 

I have a feeling this may be beyond your scope, so I'll offer a simpler, yet less elegent solution: store the text you want to display in the button itself using the name attribute.

i.e.: 

this.con.img1.name = "Spirale //2009";

this.con.img2.name = "Stresss //2009";

...

How your code is set up will determin how and when you set this value for each button, but once it is done, you can use that "e" variable that gets passed to your event function to access that name attribute like this:

this.titre.txt_titre.text = e.target.name;

mouse events have a "target" attribute that points to the object that triggered the event (i.e. - the object that was clicked, rolledover, rolled out of... whatever)

also, as Ned said, you need to change the argument type of that function to be a MouseEvent, not an Event:

function texte ( e: MouseEvent ): void { ...

thy_dudeAuthor
Participating Frequently
September 6, 2009

Thnaks you too for the answer but you are indeed kind of loosing me on this first explanation but i could give it a try. Has for the second suggestion i'ts a pretty good idea but eventually i'm going to use the same principle to show the thex text conrresponding to the title ( titre means title in french ) so it could becom inconvinient  since you can only define one name.