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

Remove eventlistener for multiple movieclips of an array

New Here ,
Mar 14, 2014 Mar 14, 2014

Hello all

Game Scenario:

i am making a road-crossing alphabet-learning game. In the first level there are 10 alphabets say from A to J (movieclips) standing on one side of the road in a randomly shuffled manner. The player has to move the alphabets (through keyboard controls) in the alphabetical order so that it hits the entrance of a kindergarten and gets removed from the stage.

Problem Situation:

If the correct alphabet is clicked the player gets a positive score. And if the wrong alphabet is cliked, a negative score is given. This works well for the first alphabet, but, once the first alphabet hits its target and is removed from stage, it fails to work for the next alphabet since the removeEventListener code set for the case of the first alphabet continues to work for the continuing alphabets.

For eg., once the first alphabet "letA" is removed from stage, and then when i click on "letB" it gives both a positive and negative score. Positive score, since it is the next alphabet that has to be clicked. Negative score, since the "wrongClick" array contains the movieclip "letB", though it was meant for the initial situation, when "letA" was also present on stage.

Code:

var wrongClick:Array = new Array(letB, letC, letD, letE, letF, letG, letH, letI, letJ);

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.addEventListener(MouseEvent.MOUSE_DOWN, negScore);

function negScore(e:MouseEvent):void{

gameScore += pointsForMiss; showGameScore();

wrongClick.removeEventListener(MouseEvent.MOUSE_DOWN, negScore);

                }

}

Solution Requested:

Can someone kindly tell me how i can write the code in such a way that, once an alphabet is removed from stage, the addEventListener for the preceding array of movieclips is completely removed and a new addEventListener starts working for the rest of the alphabets that remain on stage.

thnx in advance

with best regards

shamse tabriz

TOPICS
ActionScript
906
Translate
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

correct answers 1 Correct answer

Community Expert , Mar 14, 2014 Mar 14, 2014

execute this code:

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.removeEventListener(MouseEvent.MOUSE_DOWN, negScore);

}

when the correct letter is clicked.  ie, in the correct letter listener function.

Translate
Community Expert ,
Mar 14, 2014 Mar 14, 2014

first, that function should NOT be in the for-loop.

use:

var wrongClick:Array = new Array(letB, letC, letD, letE, letF, letG, letH, letI, letJ);

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.addEventListener(MouseEvent.MOUSE_DOWN, negScore);

}

function negScore(e:MouseEvent):void{

gameScore += pointsForMiss; showGameScore();

// to remove the clicked letter's listener:

e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, negScore);

// if you want to remove all wrongClick letter listeners when any one of them is clicked, use:

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.addEventListener(MouseEvent.MOUSE_DOWN, negScore);

}

}

Translate
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
New Here ,
Mar 14, 2014 Mar 14, 2014

Ned thanks a lot for your prompt reply. I tried the first solution that you gave but it does not seem to work.eg.,

// if you want to remove all wrongClick letter listeners when any one of them is clicked, use:

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.addEventListener(MouseEvent.MOUSE_DOWN, negScore);

}

As i continue to click on the wrong movieclips, the negative scoring also continues to take place.

Indeed the "e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, negScore);" approach works but then

 

it is not ideal in my case, since unless i press on all the wrong movieclips, traces of addEventListener will still remain.

 

The situation that i really need to handle is: if i press on some wrong movieclip, a negative score will be given. But,

even if i do not press on any wrong movieclip, and if my correct movieclip is selected, moved, and made to disappear from the

stage by hitting its target, then, the addEventListener set for the initial wrongClick array should get removed (altogether), and i

should be able to form a second and a thrid wrongClick array with decreasing number of movieclips as the game proceeds...

is there any way that i can say that:

if(letA.hitTestObject(schoolEntry)){//remove event listeners set for the initial wrong click arrays...and then set a new wrongClick

array for the case of letB}

Also, i did not understand the second reply that you posted. so i could not try it out.

Thanks again

with best regards

arshad

Translate
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 ,
Mar 14, 2014 Mar 14, 2014

that should be

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.removeEventListener(MouseEvent.MOUSE_DOWN, negScore);

}

Translate
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
New Here ,
Mar 14, 2014 Mar 14, 2014

Ned,

it works perfectly, after implementing your correction but then can you please tell me how to modify the situation so that,

instead of all the eventlisteners getting removed with the single cick of a wrong movieclip, let the player have the freedom

to click wrong movieclips multiple times and get continuing negative scores.

Eg., instead of cicking "letA", the player hurriedly clicks letB, letF and then letB again and finally letA. So, the player

get negative scoring three times, and then a positive score for clicking letA. And then, when the player moves letA to its

target and hits the target,  removeChild(letA); happens  and then, and only then, the eventlisteners for the wrong letters

will get removed. After that, i should be able to proceed similary for the advancing letters, letB, letC, letD etc.,

Thanks a lot

with best regards

arshad

Translate
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 ,
Mar 14, 2014 Mar 14, 2014

execute this code:

for (var i = 0; i<wrongClick.length ; i++){

wrongClick.removeEventListener(MouseEvent.MOUSE_DOWN, negScore);

}

when the correct letter is clicked.  ie, in the correct letter listener function.

Translate
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
New Here ,
Mar 14, 2014 Mar 14, 2014

Thank you very much indeed sir,

you really saved my day....it just took some time for me to adapt the solution that you provided to my particular situation and now it works really well...

thankyou very much indeed

pls keep up the good work

Translate
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 ,
Mar 14, 2014 Mar 14, 2014
LATEST

you're welcome.

Translate
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
LEGEND ,
Mar 14, 2014 Mar 14, 2014

I don't really see the need for having a wrong click array.  You should be able to get thru this with only a correct click set.  As you remove a correctly clicked item you advance one index in the correct click array, and the next item in the list becomes the one you focus on.  You can use the Event.currentTarget to determine which item has been clicked, and see if it is the current item of the array.

Translate
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