enterFrame listener problem please

In the following code i want each object to have it’s own enterframe listener.

But the problem is that…

Runtime:addEventListener ( “enterFrame”, hitEnterFrame )                 works!!!
obj:addEventListener ( “enterFrame”, hitEnterFrame )                         doesn’t work!!!

local gotHit = function(obj, hit, d, s, f) if(s == "hit4" and obj.sequence ~= "hit2") then obj.ground = obj.y obj.gravityScale = 1 print (d) if(d == 1 or d == -1)then if(d \< 0) then obj:setLinearVelocity(100, -200) else obj:setLinearVelocity(-100, -200) end else if(d \< obj.x) then obj:setLinearVelocity(100, -200) else obj:setLinearVelocity(-100, -200) end end --obj:applyForce(15, -5, obj.x, obj.y) obj.gettingHit = hit obj:setSequence("hit3") --obj:setFrame(f) obj:play() audio.play(soundHit4) obj.health = obj.health - mediumHurt function hitEnterFrame(event) if(obj ~= nil and obj.ground ~= nil)then if (obj.y \>= obj.ground) then --print("obj.y \<= obj.ground") obj.y = obj.ground obj.gravityScale = 0 obj:setLinearVelocity(0, 0) Runtime:removeEventListener("enterFrame", hitEnterFrame) obj:setSequence( "hit4" ) obj:play() if(obj == player)then obj:addEventListener( "sprite", playerHitAnim ) end end else Runtime:removeEventListener("enterFrame", hitEnterFrame) end end Runtime:addEventListener ( "enterFrame", hitEnterFrame ) --obj:addEventListener ( "enterFrame", hitEnterFrame ) end

PS: this function achieves jump in zero gravity.

This should work for you. This is an example that uses a table listener.

In your case, it seems “obj” is a display object, in which case you would remove the first line of code in my example below.

local obj = {} -- This could also be a display object etc function obj:enterFrame( event ) print( "hello" ) end Runtime:addEventListener( "enterFrame", obj )

Man you saved me from hours of headache…i am very thankful to you!!!

Your welcome. :slight_smile:

This should work for you. This is an example that uses a table listener.

In your case, it seems “obj” is a display object, in which case you would remove the first line of code in my example below.

local obj = {} -- This could also be a display object etc function obj:enterFrame( event ) print( "hello" ) end Runtime:addEventListener( "enterFrame", obj )

Man you saved me from hours of headache…i am very thankful to you!!!

Your welcome. :slight_smile: