Features demonstrated:
A simple way to perform animation, using an "enterFrame" listener to trigger updates.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| local background = display.newImage( "grass.png" )
local radius = 40
local xdirection = 1
local ydirection = 1
local xspeed = 7.5
local yspeed = 6.4
local xpos = display.contentWidth * 0.5
local ypos = display.contentHeight * 0.5
local fruit = display.newImage( "fruit.png", xpos, ypos )
-- Get current edges of visible screen (accounting for the areas
-- cropped by "zoomEven" scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX
local function animate(event)
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if ( xpos > screenRight - radius or xpos < screenLeft + radius ) then
xdirection = xdirection * -1;
end
if ( ypos > screenBottom - radius or ypos < screenTop + radius ) then
ydirection = ydirection * -1;
end
fruit:translate( xpos - fruit.x, ypos - fruit.y)
end
Runtime:addEventListener( "enterFrame", animate );
|