Passing Objects Through Timers, Clarification Needed

Hi guys, I need some help with the following code.  What I need to happen is each object to move over when its timer completes.  However, the only object that ever moves is the most recently created one.

[lua]

objectTable = {}

function moveObject(tempObject)

     tempObject.x = tempObject.x + 100

end

function createWall(_x, _y)

     id = #objectTable + 1

     objectTable[id] = display.newImage(“IMAGE.png”)

     --Image Properties and Physics Properties and Whatever else

     objectTable[id].timer = timer.performWithDelay(1000, function() moveObject (objectTable[id]) end,1)

end

createWall(100,1000)

createWall(100,1200)

createWall(100,1400)

[/lua]

I assume this is because “objectTable[id]” been changed.  Does anyone know how to properly reference an object in a table when a timer completes.  Thanks!!!

Try this:

objectTable = {} function moveObject( self ) self.x = self.x + 100 end function createWall(\_x, \_y) local tmp = display.newImage("IMAGE.png") objectTable[#objectTable+1] = tmp tmp.timer = moveObject -- You don't need to store the handle if you're not going to cancel it later. -- Also, don't use the field 'timer' that is special as you can see from this code. tmp.timerHandle = timer.performWithDelay(1000, tmp , 1) end createWall(100,1000) createWall(100,1200) createWall(100,1400)

Explanation:

  1. I modified the function moveObject to use the argument self (that wasn’t necessary but I wanted to make it clearer).

  2. I assigned the ‘moveObject’ function to the object in the field ‘timer’.

  3. I passed the object directly to performWithDelay().  Later, when it executes, it will see we’ve passed an object and look for a function in the field ‘timer’.  It will then execute that function and pass in the handle of the calling object.

Note: I don’t see you using _x and _y for anything?  Is that because you shortened the code for your question?

Ah I see what your doing.  I’m going to re-write this one more time for everyone else.  I’ve been looking for this answer for a while and never come across this.  THANKS for the answer!

[lua]

objectTable = {}

 

function moveObject(self)

     self.x = self.x + 100

end

 

function createWall(_x, _y)

     local id = #objectTable + 1 – I always save the value from #object+1 as a variable so I don’t make the engine do the same math operation multiple times.

     objectTable[id] = display.newImage(“IMAGE.png”)

     objectTable[id].x = _x

     objectTable[id].y = _y

     objectTable[id].timer = moveObject

     objectTable[id].timerHandle = timer.performWithDelay(1000, objectTable[id],1) --When the timer completes it looks at objectTable[id].timer for what it needs to do.

end

 

createWall(100,1000)

createWall(100,1200)

createWall(100,1400)

[/lua]

This is just sample code to show how timers work, its the same concept that’s used when handling collision.  For more information Jay offers a good explanation in this video https://www.youtube.com/watch?v=NJtmRwyU_J8

Does corona have a list of the “Specials” somewhere in the API library?

No I don’t think there is a list of ‘specials’, but they are pretty straightforward to figure out.

“touch” ==> obj.touch

“collision” ==>  obj.collision

“preCollision” ==> obj.preCollision

“onComplete” ==> obj.onComplete (useful for transitions, file downloads, and other cases with ‘onComplete’ event)

etc.

Got it just wanted to make sure that I wasn’t overlooking anything else.

Just thought of something.  What would you do in the situation where two timers are assigned to one object that reference different functions.  The timers might need to be canceled, so I need to keep track of both of them.

you can only assign one function to the ‘timer’ field.  It merely holds the handle to the function.  So if you need multiple timers touching an object, you’ll need to get creative.

Try this:

objectTable = {} function moveObject( self ) self.x = self.x + 100 end function createWall(\_x, \_y) local tmp = display.newImage("IMAGE.png") objectTable[#objectTable+1] = tmp tmp.timer = moveObject -- You don't need to store the handle if you're not going to cancel it later. -- Also, don't use the field 'timer' that is special as you can see from this code. tmp.timerHandle = timer.performWithDelay(1000, tmp , 1) end createWall(100,1000) createWall(100,1200) createWall(100,1400)

Explanation:

  1. I modified the function moveObject to use the argument self (that wasn’t necessary but I wanted to make it clearer).

  2. I assigned the ‘moveObject’ function to the object in the field ‘timer’.

  3. I passed the object directly to performWithDelay().  Later, when it executes, it will see we’ve passed an object and look for a function in the field ‘timer’.  It will then execute that function and pass in the handle of the calling object.

Note: I don’t see you using _x and _y for anything?  Is that because you shortened the code for your question?

Ah I see what your doing.  I’m going to re-write this one more time for everyone else.  I’ve been looking for this answer for a while and never come across this.  THANKS for the answer!

[lua]

objectTable = {}

 

function moveObject(self)

     self.x = self.x + 100

end

 

function createWall(_x, _y)

     local id = #objectTable + 1 – I always save the value from #object+1 as a variable so I don’t make the engine do the same math operation multiple times.

     objectTable[id] = display.newImage(“IMAGE.png”)

     objectTable[id].x = _x

     objectTable[id].y = _y

     objectTable[id].timer = moveObject

     objectTable[id].timerHandle = timer.performWithDelay(1000, objectTable[id],1) --When the timer completes it looks at objectTable[id].timer for what it needs to do.

end

 

createWall(100,1000)

createWall(100,1200)

createWall(100,1400)

[/lua]

This is just sample code to show how timers work, its the same concept that’s used when handling collision.  For more information Jay offers a good explanation in this video https://www.youtube.com/watch?v=NJtmRwyU_J8

Does corona have a list of the “Specials” somewhere in the API library?

No I don’t think there is a list of ‘specials’, but they are pretty straightforward to figure out.

“touch” ==> obj.touch

“collision” ==>  obj.collision

“preCollision” ==> obj.preCollision

“onComplete” ==> obj.onComplete (useful for transitions, file downloads, and other cases with ‘onComplete’ event)

etc.

Got it just wanted to make sure that I wasn’t overlooking anything else.

Just thought of something.  What would you do in the situation where two timers are assigned to one object that reference different functions.  The timers might need to be canceled, so I need to keep track of both of them.

you can only assign one function to the ‘timer’ field.  It merely holds the handle to the function.  So if you need multiple timers touching an object, you’ll need to get creative.