Hi Again,
Sorry for spamming the forum today 
Can i pass an object to enterFrame function??? I am getting error in doing so :S
Thanks Again!!!
Hi Again,
Sorry for spamming the forum today 
Can i pass an object to enterFrame function??? I am getting error in doing so :S
Thanks Again!!!
No, you can’t
The parameters into system supported event functions are all sent from the system, not your code - they wouldn’t do what you want if they didn’t
Why do you need to pass it in?
You probably need to post a snippet of code for us to diagnose from
Can’t you just add this object as listener to enterFrame? You will have to create enterFrame method for object.
This method will be called on eachFrame with access to self.
Read more about table listeners if you don’t understand what I am talking about.
I came across this problem again 
i tried something like this but it doesn’t work…
local button = display.newCircle(100,100,35) button.name = "IamButton" function button:enterFrame(event) print(self.name) end button:addEventListener( "enterFrame", button )
The enterFrame event never fires up???
Somebody please…I need a solution :(
I believe it’s because enterFrame event can be only added for Runtime
so what is the solution? i want to access the object in runtime?
PS: i can’t make object global cause the object is created in runtime and there are lots of them created and destroyed during runtime.
Let’s say you have objectA. You can do:
function objectA:enterFrame() -- put your enterFrame code here end Runtime:addEventListener("enterFrame", objectA)
or something like that.
Rob
It works…but not in my case.
Because in my case…ObjectA is a local parameter of a function.
In theory i can make a global variable and assign the object to it so i can access it in runtimes but as i said i would be creating and destroying lots of them, so wouldn’t it interfer with each other???
I think i should show my code here so u can better understand my situation…
it is a code from my multiplayer game.
--Player/Enemy jump in zero gravity, in the following function i want to replace player with self... local function hitEnterFrame(event) --function obj:enterFrame() --print (player.ground .. " ".. player.y) print("I am inside hitEnterFrame") if (player.y \>= player.ground) then --print("player.y \<= player.ground") player.y = player.ground player.gravityScale = 0 player:setLinearVelocity(0, 0) Runtime:removeEventListener("enterFrame", hitEnterFrame) player:setSequence( "hit4" ) player:play() player:addEventListener( "sprite", playerHitAnim ) end --Sck.sendUpdate(player.x, player.y, player.xScale, player.sequence, player.frame) end local gotHit = function(obj, hit, d, s, f) if(s == "hit4" and obj.sequence ~= "hit2") then --print ("hit3 received") obj.ground = obj.y --temporarily activate gravity obj.gravityScale = 1 print (d) if(d == 1 or d == -1)then if(d \< 0) then obj:setLinearVelocity(100, -200) --This value must be adjusted, it depends on animation speed else obj:setLinearVelocity(-100, -200) --This value must be adjusted, it depends on animation speed end else if(d \> obj.x) then obj:setLinearVelocity(100, -200) --This value must be adjusted, it depends on animation speed else obj:setLinearVelocity(-100, -200) --This value must be adjusted, it depends on animation speed end end --obj:applyForce(15, -5, obj.x, obj.y) obj.gettingHit = hit obj:setSequence("hit3") --obj:setFrame(f) obj:play() audio.play(soundHit4) --problem is here...I want to access this obj in runtime :s obj:addEventListener ( "enterFrame", hitEnterFrame ) --Runtime:addEventListener ( "enterFrame", hitEnterFrame ) elseif (s == "hit1" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence(s) obj:setFrame(f) obj:play() audio.play(soundHit2) elseif (s == "hit2" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence(s) obj:setFrame(f) obj:play() audio.play(soundHit1) elseif (s == "hit3" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence("hit2") obj:setFrame(f) obj:play() audio.play(soundHit3) elseif (s == "stand" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence(s) obj:setFrame(f) obj:play() --Sck.sendUpdate(obj.x, obj.y, obj.xScale, obj.sequence, obj.frame, 1) end end T.gotHit = gotHit
To replace player with self player would have to be listener do enterFrame listener
In that case if you put ‘self’ in that function you are referring to ‘obj’ (whatever it is).
Actually this closures might work fro you. Here is excelent tutorial by AdrentKid
Wow! this was your first post and you gave the right solution 
Thanks!
You’re welcome
I was logged on wrong (work) account 
No, you can’t
The parameters into system supported event functions are all sent from the system, not your code - they wouldn’t do what you want if they didn’t
Why do you need to pass it in?
You probably need to post a snippet of code for us to diagnose from
Can’t you just add this object as listener to enterFrame? You will have to create enterFrame method for object.
This method will be called on eachFrame with access to self.
Read more about table listeners if you don’t understand what I am talking about.
I came across this problem again 
i tried something like this but it doesn’t work…
local button = display.newCircle(100,100,35) button.name = "IamButton" function button:enterFrame(event) print(self.name) end button:addEventListener( "enterFrame", button )
The enterFrame event never fires up???
Somebody please…I need a solution :(
I believe it’s because enterFrame event can be only added for Runtime
so what is the solution? i want to access the object in runtime?
PS: i can’t make object global cause the object is created in runtime and there are lots of them created and destroyed during runtime.
Let’s say you have objectA. You can do:
function objectA:enterFrame() -- put your enterFrame code here end Runtime:addEventListener("enterFrame", objectA)
or something like that.
Rob
It works…but not in my case.
Because in my case…ObjectA is a local parameter of a function.
In theory i can make a global variable and assign the object to it so i can access it in runtimes but as i said i would be creating and destroying lots of them, so wouldn’t it interfer with each other???
I think i should show my code here so u can better understand my situation…
it is a code from my multiplayer game.
--Player/Enemy jump in zero gravity, in the following function i want to replace player with self... local function hitEnterFrame(event) --function obj:enterFrame() --print (player.ground .. " ".. player.y) print("I am inside hitEnterFrame") if (player.y \>= player.ground) then --print("player.y \<= player.ground") player.y = player.ground player.gravityScale = 0 player:setLinearVelocity(0, 0) Runtime:removeEventListener("enterFrame", hitEnterFrame) player:setSequence( "hit4" ) player:play() player:addEventListener( "sprite", playerHitAnim ) end --Sck.sendUpdate(player.x, player.y, player.xScale, player.sequence, player.frame) end local gotHit = function(obj, hit, d, s, f) if(s == "hit4" and obj.sequence ~= "hit2") then --print ("hit3 received") obj.ground = obj.y --temporarily activate gravity obj.gravityScale = 1 print (d) if(d == 1 or d == -1)then if(d \< 0) then obj:setLinearVelocity(100, -200) --This value must be adjusted, it depends on animation speed else obj:setLinearVelocity(-100, -200) --This value must be adjusted, it depends on animation speed end else if(d \> obj.x) then obj:setLinearVelocity(100, -200) --This value must be adjusted, it depends on animation speed else obj:setLinearVelocity(-100, -200) --This value must be adjusted, it depends on animation speed end end --obj:applyForce(15, -5, obj.x, obj.y) obj.gettingHit = hit obj:setSequence("hit3") --obj:setFrame(f) obj:play() audio.play(soundHit4) --problem is here...I want to access this obj in runtime :s obj:addEventListener ( "enterFrame", hitEnterFrame ) --Runtime:addEventListener ( "enterFrame", hitEnterFrame ) elseif (s == "hit1" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence(s) obj:setFrame(f) obj:play() audio.play(soundHit2) elseif (s == "hit2" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence(s) obj:setFrame(f) obj:play() audio.play(soundHit1) elseif (s == "hit3" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence("hit2") obj:setFrame(f) obj:play() audio.play(soundHit3) elseif (s == "stand" and obj.sequence ~= "hit3") then obj.gettingHit = hit obj:setSequence(s) obj:setFrame(f) obj:play() --Sck.sendUpdate(obj.x, obj.y, obj.xScale, obj.sequence, obj.frame, 1) end end T.gotHit = gotHit
To replace player with self player would have to be listener do enterFrame listener
In that case if you put ‘self’ in that function you are referring to ‘obj’ (whatever it is).
Actually this closures might work fro you. Here is excelent tutorial by AdrentKid
Wow! this was your first post and you gave the right solution 
Thanks!