Maybe create a table of all of the food that is available for placement.
Then, when the food appears, pass the name of the food and use those results in the transition.to statement.
You are also using some other logic I never saw before (scanning?). I would assume there are obstacles in the way or no? If there are you may need to implement something like A* or other pathfinding…
Ok so in this transition.to statement, the result will be the critter will immediately go towards the food.
transition.to(critter, { time=400, x = food.x, y =food.y } )
Be warned, I’m really stinky with tables. But I know on transition.to, you can use an object for something to animate towards it (as you are probably aware).
Also, this link has “point to a touch event”. If you run the code at the link (run it in “debug”) you will see the “line” (when in debug) on the square points to wherever you touch. With some magic massaging, I am sure you could alter it to work for you as well.
http://developer.anscamobile.com/code/making-object-point-touch-event
My code, shows a large box, and a small box. Drag the big box around and the small box will rotate a bit and chase after it. I just added this in, trying to help you out but I have to leave now to go run errands, so I think I was on the right track, no sure though. I’m not that good at math, or coding lol.
And now, for my code which may help, or may just make you mad
either way, have at it!
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setScale( 60 )
physics.setGravity( 0, 0 )
physics.setPositionIterations(32)
physics.setDrawMode( "debug" )
local game = display.newGroup();
game.x = 0
game.y = 0
-- (Xlocation,Ylocation, Xsize, Ysize)
local square = display.newRect (0,0,50,50)
square:setFillColor( 0, 0, 255)
physics.addBody( square,"dynamic",{ density = 0.1, friction=0.3, bounce=0.3, } )
game:insert(square)
local box = display.newRect (50,50,150,150)
box:setFillColor( 255, 0, 0 )
physics.addBody( box, "dynamic", { friction=0.3, bounce=0.3} )
box.isBullet = true
game:insert( box)
local function dragsbox(event)
box.x = event.x
box.y = event.y
print "Dragging box"
end
--Function that will
local function followbox(event)
if event.phase == "began" then
print "getting into position"
transition.to( square, { time=500, delay=0, rotation = -event.x, -event.y, } )
elseif event.phase == "ended" then
transition.to(square, { delay=500, x = box.x, y =box.y, rotation = 80 } )
print "going towards box position"
end
end
--Spawn the square on screen so it doesn't spawn off screen :)
square.x= math.random(square.width/2,
display.contentWidth - square.width/2)
square.y= math.random(square.height/2,
display.contentHeight - square.height/2)
box:addEventListener ("touch", dragsbox)
box:addEventListener ("touch", followbox)
[import]uid: 61600 topic_id: 16516 reply_id: 61683[/import]