You will want to change the code to better fit your needs but this should hopefully help:
[code]
local angleBetween = function( pos1, pos2 )
local distance = { x = pos2.x - pos1.x, y = pos2.y - pos1.y }
if distance then
local angleBetween = math.atan( distance.y / distance.x )
if ( pos1.x < pos2.x ) then
angleBetween = angleBetween + math.rad( 90 )
else
angleBetween = angleBetween + math.rad( 270 )
end
if angleBetween == math.pi or angleBetween == ( math.pi * math.pi ) then
angleBetween = angleBetween - math.rad( 180 )
end
angleBetween = math.deg( angleBetween )
return angleBetween
end
return nil
end
local critter = display.newRect( 0, 0, 15, 15 )
critter:setFillColor( 255, 0, 0 )
critter.x = display.contentCenterX
critter.y = display.contentCenterY
local onTap = function( event )
if not critter.turnTransition and not critter.moveTransition then
local food = display.newCircle( 0, 0, 5 )
food:setFillColor( 0, 255, 0 )
food.x = event.x
food.y = event.y
food:toBack()
critter.currentFood = food
local onMoveComplete = function()
critter.moveTransition = nil
if critter.currentFood and critter.currentFood[“removeSelf”] then
critter.currentFood:removeSelf()
critter.currentFood = nil
end
end
local onTurnComplete = function()
critter.moveTransition = transition.to( critter, { time = 1000, x = event.x, y = event.y, onComplete = onMoveComplete } )
critter.turnTransition = nil
end
– Turn the critter to face the food
local angle = angleBetween( critter, event )
critter.turnTransition = transition.to( critter, { time = 500, rotation = angle, onComplete = onTurnComplete } )
end
end
Runtime:addEventListener( “tap”, onTap )
[/code] [import]uid: 5833 topic_id: 10137 reply_id: 36981[/import]