Pretty much any action that’s “constant” (i.e. constantly following) needs to be tied into the system’s game timer… basically the internal timer that starts when the game starts, and keeps going until the game ends. This timer is firing off rapidly during any game, something like 30+ times per second if I’m not mistaken.
For a follow routine, I think that’s a bit too fast to constantly update two objects’ trajectories. In my current game, I slowed things down somewhat by implementing a timer loop which checks itself every 500 milliseconds or so… half a second per “update” seems plenty, but you could make it faster.
Here’s a sample piece of code… hopefully it helps!
local prevTime = 0 --this is just the "starting" timer loop number
local function timeLoop( event )
--this compares the system timer against the loop timer
--if 500 milliseconds have elapsed since the previous check, perform an action!
if ( system.getTimer()-prevTime \>= 500 ) then
-- update the object(s) trajectories to make them follow each other
prevTime = system.getTimer() --set prevTime to system time for looping purposes
end
return
end
Runtime:addEventListener( "enterFrame", timeLoop );
That’s excellent!
For people that looking for the same thing this is how I solved it:
[code]
local function timeLoop( event )
–this compares the system timer against the loop timer
–if 500 milliseconds have elapsed since the previous check, perform an action!
if ( system.getTimer()-prevTime >= 1500 ) then
– update the object(s) trajectories to make them follow each other
transition.to( b2, { time=1500, x=b1.x, y=b1.y, transition = easingx.easeInOut} )
prevTime = system.getTimer() --set prevTime to system time for looping purposes
end
return
If you are wanting to make one object follow the other, exactly I would put the code for that object’s movement in the same function that controls the movement of the master object.
That was you only have one listener [import]uid: 5354 topic_id: 3241 reply_id: 9661[/import]
so what was the easingx.easeInOut thing about? I’m trying to do the same thing that you’re doing. [import]uid: 54001 topic_id: 3241 reply_id: 35445[/import]
How do you get the chaser to turn towards the chasee in this case the Master Object? I could use this and thanks to IgnisDesign and rickwhy for sharing.
if() then
-- object.xScale = -1
elseif() then
-- object.xScale = 1
end
Can anyone post a working sample of how to get the chaser object to always look at the object being chased…
I changed time with this
timeTaken = distance/speed * 1000 with speed a fixed value
time = timeTaken
distance is based on the chaser and chasee x and y and the only value that changes constantly
but in my case the chaser only move once to the first position of the chasee once the chasse moves the chaser does not follow
Is there a reason why it only works when time = “fixed number” [import]uid: 43696 topic_id: 3241 reply_id: 38525[/import]
Oh my gosh. I think you guys over think some things a little too much. The objective should be to do things in few lines of code as possible. All you have to do is look into the joint API for corona.
Here is what the distant joint example looks like:
myJoint = physics.newJoint( “distance”, crateA, crateB, crateA.x,crateA.y, crateB.x,crateB.y )
No matter where the crateA goes, the crateB will follow.
It has all the stuff you need for adding joints and joint details.
If you wanted the joint to get closer then just remove the joint and decrease distance every 2 seconds with multiple timers. This is just an idea and I guess you can expand on it. [import]uid: 54001 topic_id: 3241 reply_id: 41057[/import]
Good stuff. I posted this code in another thread with the disclaimer that there may be a way to do it with the API, but that I am new to Corona so I wasn’t sure. Thanks for the info. [import]uid: 58455 topic_id: 3241 reply_id: 41063[/import]