Delays with transitions updating x/y info?

I’ve been using Corona for about 3-4 months so far. This is one issue I’ve recently come across that perplexed me for a few days and I thought I’d share it with you all:

Scenario:
I have a method that creates a new graphical element and also uses “transition.to” on 3 other graphical elements to slide them across the screen (think of it as a FIFO system of 4 elements constantly being refilled). The transition has a callback that stores updated x/y information for other purposes. If the element that is ready to be used is of a certain type, it is automatically removed and the method is called again to refill the queue. What was happening, is if an element was automatically removed, when it tried to fill the queue again and transition the elements, it would not move some of them to the new locations. It was as if the x/y coordinates were not being updated when the transition was completed.

Non-working code example of what I was doing:

local fill = function()  
 --remove last element  
 table.remove(elements, #elements)  
  
 --transition remaining elements  
 for i = #elements, 1, -1 do  
 transition.to(elements, {time=50, x = elements.x + 10, y = elements.y, onComplete=transitionDone})  
 end  
  
 --add new element  
 table.insert(elements, 1, newElement())  
  
 --check for auto removal  
 if(elements[#elements].type == "bad") then  
 fill() end  
  
end  

Solution:
After much trial and error, I discovered that the transitions do not update the x/y immediately (or so it seems). I decided to test extracting out the “bad” check to a new callback for the last transition run. So basically, I am now doing all the transitions in the fill() method and performing the “bad” check and auto fill in the callback for the last transition. This ensures that the transitions are complete before the queue is updated again.

Example solution:

local fill  
  
local finalTransitionDone = function()  
 --add new element  
 table.insert(elements, 1, newElement())  
  
 --check for auto removal  
 if(elements[#elements].type == "bad") then  
 fill() end  
  
end  
  
fill = function()  
 --remove last element  
 table.remove(elements, #elements)  
  
 --transition remaining elements  
 for i = #elements, 1, -1 do  
 if(1 \> 1) then  
 transition.to(elements, {time=50, x = elements.x + 10, y = elements.y, onComplete=transitionDone})  
 else  
 transition.to(elements, {time=50, x = elements.x + 10, y = elements.y, onComplete=finalTransitionDone})  
 end  
 end  
end  
  

Notes:
Some of this might be because I’m so new with Corona, hopefully it will help someone who has run into a similar problem. If anyone has any better explanations, please respond! [import]uid: 92150 topic_id: 27453 reply_id: 327453[/import]

If I’m understanding your problem correctly, its caused by trying to set x,y on the table of graphical elements, rather than the individual elements themselves.

I think your last function needs to be:

[lua]fill = function()
–remove last element
table.remove(elements, #elements)

–transition remaining elements
for i = #elements, 1, -1 do
local elem = elements[i]
if (i > 1) then
transition.to(elem, {time=50, x = elem.x + 10, y = elem.y, onComplete=transitionDone})
else
transition.to(elem, {time=50, x = elem.x + 10, y = elem.y, onComplete=finalTransitionDone})
end
end
end[/lua] [import]uid: 8271 topic_id: 27453 reply_id: 111532[/import]

My bad. That was an error in transcribing the problem into the post. I meant to put “elements[i].x” and “elements[i].y”.

The solution I posted is a working solution for the problem. I was mostly posting the problem to help others that might have come across this issue and maybe get some insight as to what, if any delays the transition functions might have to updating element x/y values.

As a side note, in trying to diagnose the problem, I modified the transition “time” value to 0 and was still having the problem I stated. One thing that did work is if I updated the element x/y values by hand without doing the transition. That allowed the x/y updates to happen and the extra call to the fill() function to be in the same block. But of course, things look prettier with animation :).

In order to use the transition, I had to extract the extra call to fill() out into the transition callback.

[import]uid: 92150 topic_id: 27453 reply_id: 111534[/import]