Skip to main content
michaelt87073209
Participant
July 19, 2016
Answered

Passing Array value in EventListener

  • July 19, 2016
  • 1 reply
  • 382 views

Hi All, been at this for a while now and can't see the wood for the trees. Is there a way to pass a value through an EventListener so that the array referenced within the function changes value?

var i=int;

btn_US.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler,i=3);

function mouseOverHandler(e:MouseEvent, i):void {

txt_CountryName.text = country;

txt_GoverningBody.text = governingbody;

}

I simply can't see to pass the i value...

Any help would be greatly appreciated.

Mike

This topic has been closed for replies.
Correct answer robdillon

From your description it sounds like you are attempting to use one function to work with a bunch of buttons to give you a unique result on each rollover. If that's correct then you may get the result that you want by using an array that contains the instance names of each of the rollover buttons. Then you can use the event target to give you the values from the other two arrays.

Something like this:

var buts:Array = new Array(but1,but2,but3);

var cName:Array = new Array("A","B","C");

var cBody:Array = new Array("D","E","F");

for(var i in buts) {

  buts.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);

}

function mouseOverHandler(e:MouseEvent):void {

  var thisOne:int = buts.indexOf(e.target);

  txt_CountryName.text = country[thisOne];

  txt_GoverningBody.text = governingbody[thisOne];

}

1 reply

robdillon
robdillonCorrect answer
Participating Frequently
July 19, 2016

From your description it sounds like you are attempting to use one function to work with a bunch of buttons to give you a unique result on each rollover. If that's correct then you may get the result that you want by using an array that contains the instance names of each of the rollover buttons. Then you can use the event target to give you the values from the other two arrays.

Something like this:

var buts:Array = new Array(but1,but2,but3);

var cName:Array = new Array("A","B","C");

var cBody:Array = new Array("D","E","F");

for(var i in buts) {

  buts.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);

}

function mouseOverHandler(e:MouseEvent):void {

  var thisOne:int = buts.indexOf(e.target);

  txt_CountryName.text = country[thisOne];

  txt_GoverningBody.text = governingbody[thisOne];

}

michaelt87073209
Participant
July 19, 2016

Thanks robdillon, this actually streamlined the whole process!

robdillon
Participating Frequently
July 19, 2016

You're welcome.