event listener "tap" gets executed more then once?

Hello,

Im using a “tap” event listener, but it looks like when i tap something in my game it gets executed multiple times. When i print in the listener I will get around 10 prints in my terminal. Why does it not only do this once? I only tap it once, so it should only execute this once right?

Thanks in advance

Alexander

EDIT: Sorry I think there is something wrong the way I create the listener

So now I want to ask something about that. I create the listeners in a for loop, depending on whether or not the object in the loop has a quest availeble or not. This is running in the enterFrame event. So i think it was adding extra listeners to the object (Or something like that)

Now I want to check if the object has a listener already before applying the listener, I found the following way on the forums. But it does not seem to work, it says the _functionListeners is nil.

Summary of my code:(quests is a table which parallels to the characters table, to check if the character has any available quests or not(true or false) ) Normally this for loop has more statements but i summarized it to make it easier to read
[lua]function EnterFrame(event)
for o=1,#characters do
if quests[o] then
if questIsStarted == false then
if characters[o]._functionListeners.tap==nil then
characters[o]:addEventListener( “tap”, onTapNPC )
end
end
end
end
end[/lua]

Does anyone have tips how to do this [import]uid: 118839 topic_id: 33770 reply_id: 333770[/import]

Hello,
First thing, I would remove this whole process from the “EnterFrame” event. I assume that is managed by a Runtime “enterFrame” listener, and thus executes many times over? I know you’re using conditional checking to determine if a tap listener should be added or not, but there doesn’t appear to be any reason why this process is in an enterFrame routine.

On the conditional checks, I’d suggest adding a flag to each character instead of trying to query the _functionListeners table. So, just do something like “character.hasListener” and set it true or false, and swap its value depending on whether you’ve added a listener or not.

Does this help?
Brent
[import]uid: 200026 topic_id: 33770 reply_id: 134205[/import]

Thanks Brent, that helped alot. I took the eventlistener out of the enterFrame function. Already had a feeling that wasnt the best idea. I guess i put it there in the first place because I did not want to be able to tap the characters when they are not close enough to the player, and thus checking with the enterFrame event whether or not a listener should be added to the specific character. A better way (what i did now) is offcourse to add the evenlistener like any other eventlistener, and have a boolean for each character, which is true or false depending on how far the player is from the character. And then checking inside the listener whether or not the boolean is true or false :slight_smile:

I did not try the flag, because I do not need that anymore, but thanks anyways because it might come in handy sometime :slight_smile:

Alexander [import]uid: 118839 topic_id: 33770 reply_id: 134215[/import]

Hello,
First thing, I would remove this whole process from the “EnterFrame” event. I assume that is managed by a Runtime “enterFrame” listener, and thus executes many times over? I know you’re using conditional checking to determine if a tap listener should be added or not, but there doesn’t appear to be any reason why this process is in an enterFrame routine.

On the conditional checks, I’d suggest adding a flag to each character instead of trying to query the _functionListeners table. So, just do something like “character.hasListener” and set it true or false, and swap its value depending on whether you’ve added a listener or not.

Does this help?
Brent
[import]uid: 200026 topic_id: 33770 reply_id: 134205[/import]

Thanks Brent, that helped alot. I took the eventlistener out of the enterFrame function. Already had a feeling that wasnt the best idea. I guess i put it there in the first place because I did not want to be able to tap the characters when they are not close enough to the player, and thus checking with the enterFrame event whether or not a listener should be added to the specific character. A better way (what i did now) is offcourse to add the evenlistener like any other eventlistener, and have a boolean for each character, which is true or false depending on how far the player is from the character. And then checking inside the listener whether or not the boolean is true or false :slight_smile:

I did not try the flag, because I do not need that anymore, but thanks anyways because it might come in handy sometime :slight_smile:

Alexander [import]uid: 118839 topic_id: 33770 reply_id: 134215[/import]