The code below will display a blue ball, and have it spiral outward until it gets near the edge of the screen, then spiral back in until it gets near the center.
Enjoy!
[lua]display.setStatusBar( display.HiddenStatusBar )
local originX = display.contentCenterX – origin x of the movement path
local originY = display.contentCenterY – origin y of the movement path
local radius = 10 – Radius of the movement path
local angle = 0 – current angle along the movement path
local spiralStep = 60/30 – move twenty pixels over 1 second
local radialStep = 180/30 – move 180 degrees around our origin every second
local direction = 1 – 1 means spiral out, -1 means spiral in
local ball = display.newCircle( originX+radius, originY, 20 )
ball:setFillColor( 0,0,255 )
function updateBallPos()
ball.x = originX + math.sin( angle )*radius
ball.y = originY - math.cos( angle )*radius
end
– Called 30 FPS
function moveBall( event )
radius = radius + spiralStep*direction
angle = angle + radialStep
updateBallPos()
if ( direction == 1 ) then
if ( ball.x < 0.1*display.contentWidth or
ball.x > 0.9*display.contentWidth or
ball.y < 0.1*display.contentHeight or
ball.y > 0.9*display.contentHeight ) then
direction = -1
end
else
if ( ( ball.x >= 0.4*display.contentWidth and ball.x <= 0.6*display.contentWidth ) and
( ball.y >= 0.4*display.contentHeight and ball.y <= 0.6*display.contentHeight ) ) then
direction = 1
end
end
end
– Set the initial position
updateBallPos()
Runtime:addEventListener( “enterFrame”, moveBall )[/lua] [import]uid: 16734 topic_id: 22079 reply_id: 87724[/import]