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]