• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

replace eventlistener with another

Contributor ,
May 11, 2013 May 11, 2013

Copy link to clipboard

Copied

I added a EventListener for a MouseEvent.CLICK which runs the MyInstanceAction function.

clipArray.addEventListener(MouseEvent.CLICK, MyInstanceAction);

Later on I want to have that function replaced so I added this later in the code:

clipArray.addEventListener(MouseEvent.CLICK, MySelectedInstanceAction);

I thought that would automatically replace the old function with the new, but found out now both functions MyInstanceAction and MySelectedInstanceAction ran when clicking on the object.

So I had to use this:

clipArray.removeEventListener(MouseEvent.CLICK, MyInstanceAction);

clipArray.addEventListener(MouseEvent.CLICK, MySelectedInstanceAction);

Just wondered if there might be a shortcut that does the same thing? So that clicking on the object it from then on runs the second function instead of the first?

TOPICS
ActionScript

Views

770

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2013 May 11, 2013

Copy link to clipboard

Copied

the 2nd code snippet is the correct way to remove one listener and add another.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 11, 2013 May 11, 2013

Copy link to clipboard

Copied

Now I'm using an array to store movieclips. When I'm using this:

var numberSelected:Number = selectedArray.length;

selectedArray[numberSelected].removeEventListener(MouseEvent.CLICK, MyInstanceAction);

selectedArray[numberSelected].addEventListener(MouseEvent.CLICK, MySelectedInstanceAction)

the event listener from selectedArray[numberSelected) is changed from its first function to the second. When the selectedArray variable is filled with more and more movieclip references however, each one has the event listener, pointing to the second function. Which every array-movieclip added however I would only like the last one pushed to it to have the event listener with the 2nd function. All previously added array-movieclips should just jave their old event listeners removed.

I tried:

var numberSelected:Number = selectedArray.length;

selectedArray[numberSelected-1].removeEventListener(MouseEvent.CLICK, MyInstanceAction);

selectedArray[numberSelected].addEventListener(MouseEvent.CLICK, MySelectedInstanceAction)

But that doesn't work as it's first value would then be selectedArray[-1].

I also tried:

var numberSelected:Number = selectedArray.length;

selectedArray.removeEventListener(MouseEvent.CLICK, MyInstanceAction);

selectedArray[numberSelected].addEventListener(MouseEvent.CLICK, MySelectedInstanceAction)

as to remove all event listners on all array elements first and then add the new event listener to the last array element, but that gave an error too.

What should I do?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2013 May 11, 2013

Copy link to clipboard

Copied

at the start, do all array elements have an event listener and listener function MyInstanceAction?

after a particular member is clicked (eg, the 5th array element) what is that you want to happen?  all listeners removed and the last array element (or the clicked array element) to have a new listener with listener function MySelectedInstanceAction?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 12, 2013 May 12, 2013

Copy link to clipboard

Copied

At the start the original array is filled with movieclip instances which have the MyInstanceAction event listener function added to it:

clickArray[selectedArray.length].addEventListener(MouseEvent.CLICK, MyInstanceAction)

When that clickArray/movieclip is clicked the movieclip is moved to the selectedArray array. I remove the original listener function and add the new one:

selectedArray[selectedArray.length].removeEventListener(MouseEvent.CLICK, MyInstanceAction);

selectedArray[selectedArray.length].addEventListener(MouseEvent.CLICK, MySelectedInstanceAction)

This happens with every clip I click in the clickArray array. It is moved to the selectedArray array, the MyInstanceAction function is removed and replaced with the MySelectedInstanceAction function.

But when I've clicked every clip in the clickArray array, and the selectedArray array is filled with all clips, each one of those clips has the MySelectedInstanceAction listener function. What I want is only to have the last clip added to it to have the MySelectedInstanceAction listener function. All previous clips added to it should have their listener function removed.

I can't use selectedArray.length-1:

selectedArray[selectedArray.length-1].removeEventListener(MouseEvent.CLICK, MyInstanceAction);

selectedArray[selectedArray.length].addEventListener(MouseEvent.CLICK, MySelectedInstanceAction)

since when selectedArray doesn't have a clip that would result to selectedArray[-1] and throw an error. selectedArray would be empty when no clip was added to it yet through clicking on a clickArray-clip, or when clicking on a selectedArray-clip, which moves the clip back to clickArray again, so when all selectedArray clips have been clicked it would be an empty array again and selectedArray.length-1 would return -1 again.

I also couldn't use a for loop to remove MyInstanceAction fro selectedArray[0] to selectedArray[selectedArray.length], cause when selectedArray is still empty (or becomes empty again), it's first run would throw an error.

There isn't a way to remove all mouse click listener events in an array in one go?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2013 May 12, 2013

Copy link to clipboard

Copied

LATEST

assuming you're using the push method to add elements to selectedArray and you're doing that only when MyInstanceAction executes and you're not removing elements from clickArray, use:

if(selectedArray.length==clickArray.length){

for(var i:int=0;i<selectedArray.length-1;i++){

selectedArray.removeEventListener(MouseEvent.CLICK,MySelectedInstanceAction);

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines