Game Development Reference
In-Depth Information
In this exercise, we'll have the sun rise like we did in the preceding code, and then after 2 seconds,
we'll make the sun set.
Since Lua is flexible and not very fussy, you can write the code in several ways. To keep it readable
and simple, we'll split the code chunk into a function.
--
local ball = display.newCircle ( 160, 480, 50 )
ball:setFillColor(255,255,0)
ball.alpha = 0.5
ball:scale(0.5,0.5)
--
local theText = display.newText("Hello, hope you are enjoying your journey with Lua so far",0,440)
theText.x = theText.contentWidth
transition.to(theText,{time=8000, x = -theText.contentWidth})
--
function sunset()
transition.to ( ball, {delay=2000, time=2000, y=480, alpha=0.5, xScale = 0.5, yScale = 0.5})
end
--
function sunrise()
transition.to ( ball, {time=2000, y=80, alpha=1, xScale = 1.2, yScale = 1.2, onComplete = sunset})
end
--
sunrise()
Notice that in the
sunset
function, we have a new parameter:
delay
. This tells the transition function
to wait for the duration specified before starting the transition.
Note
Any value assignable to the object can be transitioned over the time specified. If you create your
own custom display objects, like widgets, you can animate them by modifying their values in a transition
function.
The
transition
namespace has a couple of functions. The one that is used the most is
transition.to
,
and the other is
transition.from
.
When transition functions are called, depending on the
to
or the
from
, the object being transitioned
is modified based on the attributes specified to be transitioned. If we use
transition.from
, the
object is at first set to the attributes provided and then transitioned to the current settings of
the object. So, if we were to use
transition.from
as
transform.from(theObject, {time=500,
alpha=0})
, the alpha setting for the
theObject
would be first set to 0, and then the transition would
end by setting the current attributes to
theObject
.