Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
I cannot get rid of some event listeners — Gideros Forum

I cannot get rid of some event listeners

edited August 2014 in General questions
I made my own tweening library (I know about GTween, but I made it a long ago).

It have been working so far, but in one particular place it started to misbehave...

I went to investigate, and found out it is buggy, quite buggy, the fact it has been working so far is just... magic (no better word O.o)

Right now when I create a tween, the function creates a tween table, add a enterframe event listener to a sprite (that is a argument of the tweening function) and points to that tween parameters table.

When the tween gets removed, I do the opposite:

tween.sprite:removeEventListener(Event.ENTER_FRAME, tweenUpdate, tween.sprite);

I think this is wrong, because it is not working, my issue that is presented is that after I delete a tween, it keeps working... in this particular case it is a alpha value, the tween was supposed to set the alpha from 1 to 0 over 5 seconds, then stop.

It works, except after those 5 seconds when I click the object again, it goes from 1 to 0 again, but really quickily (I guess that about 2 or 3 frames).

I am thinking the tween is not gone...

So, is my code to remove the event listener wrong? prints assure me the line with the removeEventListener ran, but the tween does not stop (as soon as I set the alpha to 1, it tweens to 0 again, without me telling it to again).

Likes: Yan

I make games for children: http://www.kidoteca.com
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • Have you set reflect to true or such?
  • @speeder_kidoteca to remove event you need to pass exactly the same parameters as when adding event
    1) in your case, are you sure tweenUpdate is still in the scope and referring to the same function?
    2) Is it possible you are adding events multiple times?
    3) can you add print statement to tweenUpdate function to make sure it is really the one that is executing after removal?
  • ar2rawseen

    tweenUpdate is created on the file scope, it is a local function...

    but in both cases, tween.sprite is a parameter of the function where those lines are inside (and they are not inside the same function).

    I wonder if tween.sprite is breaking it...
    I make games for children: http://www.kidoteca.com
  • Have you set reflect to true or such?
    what reflect, what you are talking about?
    I make games for children: http://www.kidoteca.com
  • Can you post a code sample to test it out?
  • tkhnomantkhnoman Member
    edited August 2014
    Instead of adding and removing an event, i prefer this way:

    1. A one main enterFrame assigned, and never being removed.
    2. Create a table, ( updateTable1 = {} )
    3. Any object that want an enterframe event, would be inserted to the table
    table.insert(updateTable1,object)
    (( On removing, i used table.removeOf to remove. ))
     
    table.indexOf = function( t, object )
    	local result
    	for i=1,#t do
    		if object == t[i] then
    			result = i
    			break
    		end
    	end
    	return result
    end
     
    function table.removeOf(tableGet,object)
    	local indexGet = table.indexOf(tableGet,object)
     
    	if indexGet then
    		table.remove(tableGet,indexGet)
    		return true
    	else
    		 print("removeOf return nil")
    		 print(debug.traceback())
    	end
    end
    5. on EnterFrame function, we add something like :
    if not gamePaused then
    	for k,v in pairs(updateTable1) do
    		v:update() -- function update act like enterframe
    	end
    end
    This way we can control which would be updated first, and the following later.
    For example, updateTable1 => for player, updateTable2 => for enemy, and so on.
    I usually also put camera function on the same enterframe.

    I also did this for touch event, that way we can control which would take priority, and which would act as multi touch, and so on.

    Likes: DungDajHjep

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.