Game Development Reference
In-Depth Information
the smoother the movement. Most software uses the terminology frames per second (fps) , which is
used to describe the frame rate of animated objects on the screen.
Think of this as the heartbeat of the app. For example, you can set the heartbeat to either 30 fps or
60 fps, the latter providing a smoother animation. However, a higher frame rate will be harder on the
CPU, especially in an app that triggers some processing, so in certain cases a lower frame rate is a
better choice.
When you create animations in Lua, Corona SDK allows you to set your app to either 30 or 60 fps.
For simplicity's sake, we'll write a handler to catch an event and simply print a message.
function ticker( event )
print ( "tick" )
end
--
Runtime:addEventListener ( "enterFrame", ticker )
When run, this will output a lot of lines that display “tick” in the terminal window.
The enterFrame event is dispatched prior to the screen render taking place.
Note
Making a Health Bar
If you have played any real-time strategy or tower-defense games, you know that each character has
some amount of health, and every time the character is hit, the health bar reduces or changes color.
In this example, we'll create a bar using a rectangle, as shown in Figure 8-13 . Then we can modify the
size of the rectangle by manipulating the height of the rectangle at the enterFrame event.
local bar = display.newRect ( 100, 300, 20, 70 )
bar:setFillColor (100,100,200,200)
local theText = display.newText( "value", 140, 300, nil, 20)
theText:setTextColor( 255, 255 ,255 )
--
local time = 20
local counter = 0
local direction = 1
local isStopped = false
--
function toggleMove ( event )
isStopped = not isStopped
end
--
function move ( event )
if isStopped == true then return end
counter = counter + direction
if counter>=time or counter<=0 then
direction = -direction
end
 
 
Search WWH ::




Custom Search