OddCore
OddCore

Reputation: 1584

transition.to( ) doesn't work within a function and with a Runtime:addEventListener( "enterFrame", method) listener in Corona / Lua

So here's my problem. I am working on some Lua code using Corona SDK, for an iPhone app. I have narrowed the problem down to a few lines of code, that, if made to work, can point me in the right direction for the actual code. ( Plus, posting the original code will only make this post longer :P). So here goes:

local square = display.newRect( 0, 0, 100, 100 )
square:setFillColor( 255,255,255 )

local function move(event)
    transition.to( self, { time=1500, alpha=0, x=100, y=100 } )
end

Runtime:addEventListener("enterFrame", move)

Basically, the problem is that the transition.to( ) method does not work in a function that has an "enterFrame" listener. If I change the listener on the last line to

timer.performWithDelay( )

or even

Runtime:addEventListener( "touch", method)

it suddenly works.

I have no idea what I am missing. Can anyone help?

Upvotes: 1

Views: 1271

Answers (1)

JeffK
JeffK

Reputation: 3039

The enterFrame listener is called by the Corona runtime every time the screen is refreshed--probably 30 frames per second. So every 33 ms, you start an animation on the square, which takes 1500 ms to complete. Just as it gets started, you add yet another animation. And so on. You should use this event to handle animations that you have to manage directly because the movement or changes are beyond the capabilities of the transition functions. The whole point of using the transition functions is to avoid the need for this kind of direct control for most animations.

The touch listener is called only when the user touches the screen, so you don't get repeated invocations of the move event. The performWithDelay method also calls the move event just once. You probably want to use one of these approaches.

Upvotes: 2

Related Questions